//Challenge: Accepted
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 1005
#define mod 998244353
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
vector<int> adj[maxn], g[maxn];
int getdep(int n, int par) {
int ret = 1;
for (int v:adj[n]) {
if (v != par) {
ret = max(ret, getdep(v, n) + 1);
}
}
return ret;
}
bool path[maxn];
void getpath(int n, int par) {
path[n] = 1;
bool to = 0;
for (int v:g[n]) {
if (v != par && g[v].size() > 1) {
getpath(v, n);
to = 1;
break;
}
}
if (to == 0) {
for (int v:g[n]) {
if (v != par) {
getpath(v, n);
break;
}
}
}
}
vector<int> ans;
int last = 0;
void solve(int n, int par) {
ans.push_back(n);
last = n;
for (int v:g[n]) {
if (v != par && !path[v]) {
ans.push_back(v);
ans.push_back(n);
}
}
for (int v:g[n]) {
if (v != par && path[v]) {
solve(v, n);
}
}
}
int main() {
io
int n, m;
cin >> n >> m;
for (int i = 0;i < m;i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
if (m > n - 1) {
cout << "NO\n";
return 0;
}
vector<int> nodes;
for (int i = 1;i <= n;i++) {
int cnt = 0;
for (int v:adj[i]) {
if (getdep(v, i) >= 3) cnt++;
}
if (cnt > 2) {
cout << "NO\n";
return 0;
}
if (adj[i].size() > 1) {
nodes.push_back(i);
for (int v:adj[i]) {
if (adj[v].size() > 1) {
g[i].push_back(v);
}
}
}
}
cout << "YES\n";
if (nodes.size() == 0) {
ans.push_back(1);
if (n > 1) ans.push_back(1);
} else if (nodes.size() == 1) {
ans.push_back(nodes[0]);
ans.push_back(nodes[0]);
} else {
if (nodes.size() == 2) {
getpath(nodes[0], 0), solve(nodes[0], 0);
} else {
for (int i:nodes) {
if (g[i].size() > 1) {
int cnt = 0;
for (int v:g[i]) {
if (g[v].size() > 1) cnt++;
}
if (cnt <= 1) {
int ini = i;
for (int v:g[i]) {
if (g[v].size() == 1) ini = v;
}
getpath(ini, 0);
solve(ini, 0);
break;
}
}
}
}
solve(last, 0);
}
cout << ans.size() << "\n";
for (int i:ans) cout << i << " ";
cout << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
372 KB |
Output is correct |
17 |
Correct |
1 ms |
372 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
368 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
368 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
372 KB |
Output is correct |
32 |
Correct |
1 ms |
372 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
364 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
368 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
364 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
372 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
0 ms |
364 KB |
Output is correct |
55 |
Correct |
0 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
368 KB |
Output is correct |
59 |
Correct |
0 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
1 ms |
340 KB |
Output is correct |
62 |
Correct |
1 ms |
340 KB |
Output is correct |
63 |
Correct |
1 ms |
468 KB |
Output is correct |
64 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
10 ms |
520 KB |
Output is correct |
12 |
Correct |
4 ms |
340 KB |
Output is correct |
13 |
Correct |
5 ms |
480 KB |
Output is correct |
14 |
Correct |
6 ms |
468 KB |
Output is correct |
15 |
Correct |
7 ms |
468 KB |
Output is correct |
16 |
Correct |
12 ms |
468 KB |
Output is correct |
17 |
Correct |
12 ms |
436 KB |
Output is correct |
18 |
Correct |
11 ms |
468 KB |
Output is correct |
19 |
Correct |
12 ms |
468 KB |
Output is correct |
20 |
Correct |
13 ms |
456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
372 KB |
Output is correct |
17 |
Correct |
1 ms |
372 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
368 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
368 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
372 KB |
Output is correct |
32 |
Correct |
1 ms |
372 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
364 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
368 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
364 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
372 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
0 ms |
364 KB |
Output is correct |
55 |
Correct |
0 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
368 KB |
Output is correct |
59 |
Correct |
0 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
1 ms |
340 KB |
Output is correct |
62 |
Correct |
1 ms |
340 KB |
Output is correct |
63 |
Correct |
1 ms |
468 KB |
Output is correct |
64 |
Correct |
1 ms |
340 KB |
Output is correct |
65 |
Correct |
0 ms |
340 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
1 ms |
340 KB |
Output is correct |
68 |
Correct |
0 ms |
364 KB |
Output is correct |
69 |
Correct |
1 ms |
368 KB |
Output is correct |
70 |
Correct |
1 ms |
340 KB |
Output is correct |
71 |
Correct |
1 ms |
340 KB |
Output is correct |
72 |
Correct |
1 ms |
340 KB |
Output is correct |
73 |
Correct |
1 ms |
440 KB |
Output is correct |
74 |
Correct |
1 ms |
340 KB |
Output is correct |
75 |
Correct |
1 ms |
340 KB |
Output is correct |
76 |
Correct |
1 ms |
340 KB |
Output is correct |
77 |
Correct |
1 ms |
340 KB |
Output is correct |
78 |
Correct |
1 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
340 KB |
Output is correct |
80 |
Correct |
1 ms |
340 KB |
Output is correct |
81 |
Correct |
1 ms |
372 KB |
Output is correct |
82 |
Correct |
1 ms |
340 KB |
Output is correct |
83 |
Correct |
1 ms |
340 KB |
Output is correct |
84 |
Correct |
1 ms |
340 KB |
Output is correct |
85 |
Correct |
1 ms |
368 KB |
Output is correct |
86 |
Correct |
1 ms |
340 KB |
Output is correct |
87 |
Correct |
1 ms |
372 KB |
Output is correct |
88 |
Correct |
1 ms |
368 KB |
Output is correct |
89 |
Correct |
17 ms |
432 KB |
Output is correct |
90 |
Correct |
13 ms |
468 KB |
Output is correct |
91 |
Correct |
12 ms |
468 KB |
Output is correct |
92 |
Correct |
13 ms |
416 KB |
Output is correct |
93 |
Correct |
17 ms |
368 KB |
Output is correct |
94 |
Correct |
12 ms |
420 KB |
Output is correct |
95 |
Correct |
19 ms |
568 KB |
Output is correct |
96 |
Correct |
18 ms |
432 KB |
Output is correct |
97 |
Correct |
13 ms |
532 KB |
Output is correct |
98 |
Correct |
19 ms |
420 KB |
Output is correct |
99 |
Correct |
13 ms |
428 KB |
Output is correct |
100 |
Correct |
14 ms |
416 KB |
Output is correct |
101 |
Correct |
12 ms |
468 KB |
Output is correct |
102 |
Correct |
12 ms |
368 KB |
Output is correct |
103 |
Correct |
15 ms |
468 KB |
Output is correct |
104 |
Correct |
12 ms |
468 KB |
Output is correct |
105 |
Correct |
11 ms |
468 KB |
Output is correct |
106 |
Correct |
15 ms |
420 KB |
Output is correct |
107 |
Correct |
14 ms |
432 KB |
Output is correct |
108 |
Correct |
18 ms |
468 KB |
Output is correct |
109 |
Correct |
16 ms |
468 KB |
Output is correct |
110 |
Correct |
14 ms |
420 KB |
Output is correct |
111 |
Correct |
11 ms |
468 KB |
Output is correct |
112 |
Correct |
13 ms |
468 KB |
Output is correct |
113 |
Correct |
18 ms |
440 KB |
Output is correct |
114 |
Correct |
15 ms |
468 KB |
Output is correct |
115 |
Correct |
12 ms |
468 KB |
Output is correct |
116 |
Correct |
16 ms |
468 KB |
Output is correct |
117 |
Correct |
19 ms |
436 KB |
Output is correct |
118 |
Correct |
15 ms |
468 KB |
Output is correct |
119 |
Correct |
12 ms |
468 KB |
Output is correct |
120 |
Correct |
16 ms |
420 KB |
Output is correct |
121 |
Correct |
19 ms |
468 KB |
Output is correct |
122 |
Correct |
13 ms |
468 KB |
Output is correct |
123 |
Correct |
14 ms |
416 KB |
Output is correct |
124 |
Correct |
22 ms |
420 KB |
Output is correct |
125 |
Correct |
10 ms |
432 KB |
Output is correct |
126 |
Correct |
11 ms |
468 KB |
Output is correct |
127 |
Correct |
10 ms |
468 KB |
Output is correct |
128 |
Correct |
19 ms |
428 KB |
Output is correct |
129 |
Correct |
1 ms |
340 KB |
Output is correct |
130 |
Correct |
1 ms |
340 KB |
Output is correct |
131 |
Correct |
1 ms |
340 KB |
Output is correct |
132 |
Correct |
1 ms |
340 KB |
Output is correct |
133 |
Correct |
1 ms |
372 KB |
Output is correct |
134 |
Correct |
14 ms |
1920 KB |
Output is correct |
135 |
Correct |
14 ms |
2092 KB |
Output is correct |
136 |
Correct |
31 ms |
5368 KB |
Output is correct |
137 |
Correct |
23 ms |
3320 KB |
Output is correct |
138 |
Correct |
36 ms |
4684 KB |
Output is correct |
139 |
Correct |
61 ms |
7876 KB |
Output is correct |
140 |
Correct |
10 ms |
1476 KB |
Output is correct |
141 |
Correct |
3 ms |
744 KB |
Output is correct |
142 |
Correct |
58 ms |
7676 KB |
Output is correct |
143 |
Correct |
69 ms |
8212 KB |
Output is correct |
144 |
Correct |
11 ms |
468 KB |
Output is correct |
145 |
Correct |
12 ms |
464 KB |
Output is correct |