-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcycle_detection.cpp
More file actions
78 lines (74 loc) · 1.45 KB
/
cycle_detection.cpp
File metadata and controls
78 lines (74 loc) · 1.45 KB
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
74
75
76
77
78
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define PI acos(-1)
#define pb push_back
#define int long long int
#define pi pair<int, int>
#define pii pair<int, pi>
#define fir first
#define sec second
#define MAXN 205
#define MAXP 100001
#define mod 1000000007
int n, m, idx;
vector<int> cycles[MAXN];
vector<int> adj[MAXN];
int color[MAXN];
int parent[MAXN];
int ans[MAXN];
void dfs(int u, int p)
{
if (color[u] == 2)
return;
if (color[u] == 1)
{
idx++;
int curr = p;
ans[curr] = idx;
cycles[idx].pb(curr);
while (curr != u)
{
curr = parent[curr];
cycles[idx].pb(curr);
ans[curr] = idx;
}
return;
}
parent[u] = p;
color[u] = 1;
for (auto const &v : adj[u])
if (v != parent[u])
dfs(v, u);
color[u] = 2;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 0; i < m; i++)
{
int a, b;
cin >> a >> b;
a--, b--;
adj[a].pb(b);
adj[b].pb(a);
}
for (int i = 0; i < n; i++)
if (!color[i])
dfs(i, -1);
cout << idx << endl;
for (int i = 1; i <= idx; i++)
{
cout << cycles[i].size() << endl;
for (auto const &j : cycles[i])
cout << j + 1 << " ";
cout << endl;
}
return 0;
}