|
| 1 | +#include<bits/stdc++.h> |
| 2 | +#include<ext/pb_ds/assoc_container.hpp> |
| 3 | +#include<ext/pb_ds/tree_policy.hpp> |
| 4 | +#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); |
| 5 | +#define Fileio freopen("output.txt","w",stdout);freopen("input.txt","r",stdin); |
| 6 | +#define all(v) v.begin(),v.end() |
| 7 | +#define rall(v) v.rbegin(),v.rend() |
| 8 | +#define MEM(a,x) memset(a,x,sizeof(a)) |
| 9 | +#define SZ(v) v.size() |
| 10 | +#define nl "\n" |
| 11 | +#define bug cout<<"bug"<<nl; |
| 12 | +#define pi acos(-1.0) |
| 13 | +#define ll long long |
| 14 | +#define pb push_back |
| 15 | +#define mp make_pair |
| 16 | +#define pii pair< int,int > |
| 17 | +#define pll pair< ll,ll > |
| 18 | +#define vii vector< int > |
| 19 | +#define vll vector< ll > |
| 20 | +#define vpi vector< pii > |
| 21 | +#define vpl vector<pll> |
| 22 | +#define MX 200005 |
| 23 | +#define EPS 1e-12 |
| 24 | +#define ss second |
| 25 | +#define ff first |
| 26 | +using namespace std; |
| 27 | +using namespace __gnu_pbds; |
| 28 | + |
| 29 | +template<typename T> |
| 30 | +using ordered_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; |
| 31 | +template<typename T> |
| 32 | +using ordered_multiset=tree<T, null_type, less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>; |
| 33 | + |
| 34 | +inline ll powr(int a,int b){ll res=1;while(b){if(b&1) res*=a;a*=a;b/=2;}return res;} |
| 35 | +int cases=1; |
| 36 | + |
| 37 | +#ifdef ppqq |
| 38 | + #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__) |
| 39 | + template < typename Arg1 > |
| 40 | + void __f(const char* name, Arg1&& arg1){ |
| 41 | + cerr << name << " is " << arg1 << std::endl; |
| 42 | + } |
| 43 | + template < typename Arg1, typename... Args> |
| 44 | + void __f(const char* names, Arg1&& arg1, Args&&... args){ |
| 45 | + const char* comma = strchr(names+1, ','); |
| 46 | + cerr.write(names, comma - names) << " is " << arg1 <<" "; |
| 47 | + __f(comma+1, args...); |
| 48 | + } |
| 49 | +#else |
| 50 | + #define debug(...) |
| 51 | +#endif |
| 52 | + |
| 53 | +///******************************************START****************************************** |
| 54 | +//Ref : https://github.com/sayedgkm/CodeLibrary/blob/master/Graph/Euler_circuit(directed).cpp |
| 55 | + |
| 56 | +vector<int> adj[MX],rev[MX]; |
| 57 | +int inDeg[MX],outDeg[MX]; |
| 58 | +bool vis[MX],vis2[MX]; |
| 59 | +vector<int> tour; |
| 60 | + |
| 61 | +void findTour(int u) |
| 62 | +{ |
| 63 | + while(adj[u].size()) |
| 64 | + { |
| 65 | + int v=adj[u].back(); |
| 66 | + adj[u].pop_back(); |
| 67 | + |
| 68 | + findTour(v); |
| 69 | + } |
| 70 | + tour.push_back(u); |
| 71 | +} |
| 72 | +void dfs(int u) |
| 73 | +{ |
| 74 | + vis[u]=true; |
| 75 | + for(auto it: adj[u]) |
| 76 | + { |
| 77 | + if(vis[it]) continue; |
| 78 | + dfs(it); |
| 79 | + } |
| 80 | +} |
| 81 | +void dfs2(int u) |
| 82 | +{ |
| 83 | + vis2[u]=true; |
| 84 | + for(auto it: rev[u]) |
| 85 | + { |
| 86 | + if(!vis2[it]) dfs2(it); |
| 87 | + } |
| 88 | +} |
| 89 | +bool connected(int u,int n) |
| 90 | +{ |
| 91 | + dfs(u); |
| 92 | + dfs2(u); |
| 93 | + for(int i=1;i<=n;i++) if(!vis[i] && !vis2[i] && (inDeg[i] || outDeg[i])) return false; |
| 94 | + return true; |
| 95 | +} |
| 96 | +bool isEulerCycle(int n) |
| 97 | +{ |
| 98 | + int st=-1; |
| 99 | + for(int i=1;i<=n;i++) |
| 100 | + { |
| 101 | + if(inDeg[i]!=outDeg[i]) return false; |
| 102 | + if(outDeg[i]) st=i; |
| 103 | + } |
| 104 | + |
| 105 | + if(st==-1) return true; |
| 106 | + |
| 107 | + ///if the graph is already connected no need to check it just return true |
| 108 | + if(connected(st,n)) return true; |
| 109 | + return false; |
| 110 | +} |
| 111 | +bool isEulerPath(int n) |
| 112 | +{ |
| 113 | + int st=-1,odd=0; |
| 114 | + for(int i=1;i<=n;i++) |
| 115 | + { |
| 116 | + if(inDeg[i]!=outDeg[i]) |
| 117 | + { |
| 118 | + if(abs(inDeg[i]-outDeg[i])>1) return false; |
| 119 | + odd++; |
| 120 | + } |
| 121 | + if(outDeg[i]) st=i; |
| 122 | + } |
| 123 | + |
| 124 | + if(st==-1) return true; |
| 125 | + |
| 126 | + ///if the graph is already connected no need to check it just return true |
| 127 | + if(connected(st,n) && odd==2) return true; |
| 128 | + return false; |
| 129 | +} |
| 130 | +void EulerCycle(int n) |
| 131 | +{ |
| 132 | + if(isEulerCycle(n)) |
| 133 | + { |
| 134 | + int st=1; |
| 135 | + for(int i=1;i<=n;i++) if(outDeg[i]) st=i; |
| 136 | + findTour(st); |
| 137 | + reverse(all(tour)); |
| 138 | + for(auto it: tour) cout<<it<<" "; |
| 139 | + } |
| 140 | + else cout<<"IMPOSSIBLE\n"; |
| 141 | +} |
| 142 | +void EulerPath(int n) |
| 143 | +{ |
| 144 | + if(isEulerPath(n)) |
| 145 | + { |
| 146 | + int st=1; |
| 147 | + for(int i=1;i<=n;i++) if(inDeg[i]!=outDeg[i]) st=i; |
| 148 | + findTour(st); |
| 149 | + reverse(all(tour)); |
| 150 | + for(auto it: tour) cout<<it<<" "; |
| 151 | + } |
| 152 | + else cout<<"IMPOSSIBLE\n"; |
| 153 | +} |
| 154 | +int main() |
| 155 | +{ |
| 156 | + FIO; |
| 157 | + int n,m; |
| 158 | + cin>>n>>m; |
| 159 | + |
| 160 | + for(int i=0;i<m;i++) |
| 161 | + { |
| 162 | + int u,v; |
| 163 | + cin>>u>>v; |
| 164 | + adj[u].push_back(v); |
| 165 | + rev[v].push_back(u); |
| 166 | + inDeg[v]++; |
| 167 | + outDeg[u]++; |
| 168 | + } |
| 169 | + EulerPath(n); |
| 170 | +} |
0 commit comments