File tree 1 file changed +65
-0
lines changed
1 file changed +65
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Codechef problem code TOURISTS
2
+ #include < bits/stdc++.h>
3
+ #define ll long long
4
+ #define N 200005
5
+ using namespace std ;
6
+
7
+ set<pair<ll,ll> > edges[N];
8
+ vector<pair<ll,ll> > final_edges (N);
9
+ ll deg[N];
10
+ bool visited[N];
11
+ ll cnt = 0 ;
12
+
13
+ void dfs (ll u) {
14
+ visited[u] = 1 ;
15
+ cnt++;
16
+ set<pair<ll,ll> >::iterator it;
17
+ for (it = edges[u].begin (); it != edges[u].end (); it++) {
18
+ if (!visited[it->first ])
19
+ dfs (it->first );
20
+ }
21
+ }
22
+
23
+ int main () {
24
+ ll i,n,e,u,v,index ;
25
+ cin>>n>>e;
26
+ for (i=0 ;i<e;i++) {
27
+ cin>>u>>v;
28
+ edges[u].insert (make_pair (v,i));
29
+ edges[v].insert (make_pair (u,i));
30
+ deg[u] += 1 ;
31
+ deg[v] += 1 ;
32
+ }
33
+ for (i=1 ;i<=n;i++) {
34
+ if (deg[i] & 1 ) {
35
+ cout<<" NO" ;
36
+ return 0 ;
37
+ }
38
+ }
39
+ dfs (1 );
40
+ if (cnt != n) {
41
+ cout<<" NO" ;
42
+ return 0 ;
43
+ }
44
+ cout<<" YES\n " ;
45
+ // generate eulerian cycle
46
+ for (u=1 ;u<=n;u++) {
47
+ while (!edges[u].empty ()) {
48
+ ll s = u;
49
+ do {
50
+ pair<ll,ll> p = *edges[s].begin ();
51
+ index = p.second ;
52
+ v = p.first ;
53
+ final_edges[index ] = make_pair (s,v);
54
+ edges[s].erase (p);
55
+ edges[v].erase (make_pair (s,index ));
56
+ s = v;
57
+ }while (s != u);
58
+ }
59
+ }
60
+ // final_edges store the directed edges in the same order of input
61
+ for (i=0 ;i<e;i++) {
62
+ cout<<final_edges[i].first <<" " <<final_edges[i].second <<" \n " ;
63
+ }
64
+ return 0 ;
65
+ }
You can’t perform that action at this time.
0 commit comments