Skip to content

Commit 0a725ba

Browse files
committed
Add euler path in graph
1 parent 1bbe706 commit 0a725ba

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Graph/EulerPath/euler_path.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// O(V + E)
2+
// Euler path is a path that uses every edge in graph exactly once
3+
// const int N = 1e5 + 50;
4+
int n, in[N], out[N]; // in and out degrees
5+
vector<int> path; // has the euler path in reverse order
6+
queue<int> adj[N];
7+
8+
void dfs(int u)
9+
{
10+
while(adj[u].size())
11+
{
12+
int v = adj[u].front();
13+
adj[u].pop();
14+
dfs(v);
15+
}
16+
path.push_back(u);
17+
}
18+
19+
pair<bool, int> is_eulerian() // {is eulerian, start node of dfs}
20+
{
21+
int _s = 0, _e = 0, st = 1;
22+
for(int u = 1; u < N; u++)
23+
{
24+
int sub = in[u] - out[u];
25+
if(sub == -1)
26+
++_s, st = u;
27+
else if(sub == 1)
28+
++_e;
29+
else if(sub != 0)
30+
return {0, -1};
31+
}
32+
if(_s > 1 || _e > 1 || _s + _e == 1)
33+
return {0, -1};
34+
return {1, st};
35+
}

0 commit comments

Comments
 (0)