-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAll Cycle Path.cpp
More file actions
75 lines (51 loc) · 1.06 KB
/
Copy pathAll Cycle Path.cpp
File metadata and controls
75 lines (51 loc) · 1.06 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
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define pb push_back
#define endl '\n';
const int N = 1e6;
vector<int> graph[N];
vector<vector<int>> cycles;
int color[N];
int par[N];
void dfs_cycle(int u, int p, int color[], int par[], int& cyclenumber)
{
if (color[u] == 2) {
return;
}
if (color[u] == 1) {
vector<int> v;
cyclenumber++;
int cur = p;
v.push_back(cur);
while (cur != u) {
cur = par[cur];
v.push_back(cur);
}
cycles.push_back(v);
return;
}
par[u] = p;
color[u] = 1;
for (int v : graph[u]) {
if (v == par[u]) {
continue;
}
dfs_cycle(v, u, color, par, cyclenumber);
}
color[u] = 2;
}
void solve() {
int cyclenumber = 0;
dfs_cycle(1, 0, color, par, cyclenumber);
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}