-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathfind_cycle.cpp
More file actions
48 lines (45 loc) · 852 Bytes
/
find_cycle.cpp
File metadata and controls
48 lines (45 loc) · 852 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
vector< int >cycle;
int v1=0,v2=0;
void dfs(int i, auto& adj, auto& vis, auto& in_stack, auto& path)
{
// cout<<i<<" "<<vis[i]<<" "<<in_stack[i]<<"\n";
if(cycle.size()>0) return;
if(in_stack[i])
{
cycle.push_back(i);
while(path.top()!=i)
{
cycle.push_back(path.top());
in_stack[path.top()]=0;
path.pop();
}
cycle.push_back(i);
in_stack[i]=0;
return;
}
if(vis[i]) return;
vis[i]=1;
in_stack[i]=1;
path.push(i);
for(auto x:adj[i])
{
if(i==v1 && x==v2) continue;
dfs(x,adj,vis,in_stack,path);
}
in_stack[i]=0;
if(!path.empty() && path.top()==i)
path.pop();
}
void find_cycle(vector< list<int> >& adj)
{
int n=adj.size();
vector<bool>vis(n,0),in_stack(n,0);
for(int i=1;i<n;i++)
{
if(!vis[i])
{
stack<int>path;
dfs(i,adj,vis,in_stack,path);
}
}
}