-
Notifications
You must be signed in to change notification settings - Fork 0
/
printing all cycles.cpp
73 lines (60 loc) · 1.17 KB
/
printing all cycles.cpp
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <bits/stdc++.h>
using namespace std;
vector<int>adj[100];
bool visited[100];
vector<int>comp;
int n1;
int coun_t = 0;
bool decide = false;
void dfs(int source)
{
comp.push_back(source);
visited[source] = true;
for(int vertex:adj[source])
{
if(!visited[vertex])
{
//comp.push_back(vertex); you cannot write this line here because it will not add first source node
//in comp
dfs(vertex);
}
}
}
void dfutil()
{
for(int i=0; i<100; i++)
{
visited[i] = false;
}
for(int x=1; x<=n1; x++)
{
if(!visited[x])
{
comp.clear();
dfs(x);
decide = false;
for(int point:adj[comp[0]])
{
if(point == comp[comp.size()-1])
decide = true;
}
if(decide)
for(int v:comp)
cout<<v<<" ";
cout<<endl;
}
}
}
int main()
{
int a, b, n2;
cin>>n1>>n2;
for(int x=1; x<=n2; x++)
{
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfutil();
return 0;
}