Skip to content

Commit e40387f

Browse files
committed
euler
1 parent a889362 commit e40387f

File tree

2 files changed

+324
-0
lines changed

2 files changed

+324
-0
lines changed

Graph/Euler Tour(Directed).cpp

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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+
}

Graph/Euler Tour.cpp

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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.cpp
55+
56+
multiset<int> adj[MX];
57+
int deg[MX];
58+
bool vis[MX];
59+
vector<int> tour;
60+
void findTour(int u)
61+
{
62+
while(adj[u].size())
63+
{
64+
int v=*adj[u].begin();
65+
adj[u].erase(adj[u].find(v));
66+
adj[v].erase(adj[v].find(u));
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+
bool connected(int u,int n)
82+
{
83+
dfs(u);
84+
for(int i=1;i<=n;i++) if(!vis[i] && deg[i]) return false;
85+
return true;
86+
}
87+
bool isEulerCycle(int n)
88+
{
89+
int st=-1;
90+
for(int i=1;i<=n;i++)
91+
{
92+
if(deg[i]%2) return false;
93+
if(deg[i]) st=i;
94+
}
95+
96+
if(st==-1) return true;
97+
///if the graph is already connected no need to check it just return true
98+
if(connected(st,n)) return true;
99+
return false;
100+
}
101+
bool isEulerPath(int n)
102+
{
103+
int st=-1,odd=0;
104+
for(int i=1;i<=n;i++)
105+
{
106+
if(deg[i]%2) odd++;
107+
if(deg[i]) st=i;
108+
}
109+
110+
if(st==-1) return true;
111+
///if the graph is already connected no need to check it just return true
112+
if(connected(st,n) && odd==2) return true;
113+
return false;
114+
}
115+
void EulerCycle(int n)
116+
{
117+
if(isEulerCycle(n))
118+
{
119+
int st=1;
120+
for(int i=1;i<=n;i++) if(deg[i]) st=i;
121+
findTour(st);
122+
reverse(all(tour));
123+
for(auto it: tour) cout<<it<<" ";
124+
}
125+
else cout<<"IMPOSSIBLE\n";
126+
}
127+
void EulerPath(int n)
128+
{
129+
if(isEulerPath(n))
130+
{
131+
int st=1;
132+
for(int i=1;i<=n;i++) if(deg[i]%2) st=i;
133+
findTour(st);
134+
reverse(all(tour));
135+
for(auto it: tour) cout<<it<<" ";
136+
}
137+
else cout<<"IMPOSSIBLE\n";
138+
}
139+
int main()
140+
{
141+
FIO;
142+
int n,m;
143+
cin>>n>>m;
144+
145+
for(int i=0;i<m;i++)
146+
{
147+
int u,v;
148+
cin>>u>>v;
149+
adj[u].insert(v);
150+
adj[v].insert(u);
151+
deg[u]++;
152+
deg[v]++;
153+
}
154+
}

0 commit comments

Comments
 (0)