-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC.cpp
79 lines (78 loc) · 1.6 KB
/
C.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
74
75
76
77
78
79
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
const int maxn = 2.5e5 + 10;
vector <int> g[maxn];
int dep[maxn];
int par[maxn];
bool vis[maxn];
int n, k;
bool leaf[maxn];
void dfs(int x) {
vis[x] = true;
if(1LL * k * dep[x] >= n) {
cout << "PATH" << endl;
cout << dep[x] << endl;
while(x != 1) {
cout << x << " ";
x = par[x];
}
cout << 1 << endl;
exit(0);
}
int child = 0;
for(int i : g[x]) {
if(vis[i] == false) {
dep[i] = 1 + dep[x];
par[i] = x;
child += 1;
dfs(i);
}
}
leaf[x] = (child == 0);
}
int main() {
ios_base :: sync_with_stdio(false);
cin.tie(0);
int m;
cin >> n >> m >> k;
for(int i = 1; i <= m; i++) {
int p, q;
cin >> p >> q;
g[p].push_back(q);
g[q].push_back(p);
}
dep[1] = 1;
dfs(1);
cout << "CYCLES" << endl;
for(int i = 1; i <= n; i++) {
if(k == 0) break;
if(leaf[i]) {
k -= 1;
vector <int> can;
for(int j : g[i]) if(j != par[i]) can.push_back(j);
int p = can[0], q = can[1];
if(dep[p] > dep[q]) swap(p, q);
if((dep[q] - dep[p] + 2) % 3 != 0) {
cout << dep[q] - dep[p] + 2 << endl;
int cur = q;
cout << i << " ";
while(cur != p) {
cout << cur << " ";
cur = par[cur];
}
cout << p << endl;
} else {
if((dep[i] - dep[p] + 1) % 3 == 0) swap(p, q);
cout << dep[i] - dep[p] + 1 << endl;
int cur = i;
while(cur != p) {
cout << cur << " ";
cur = par[cur];
}
cout << p << endl;
}
}
}
return 0;
}