#include "bits/stdc++.h"
using namespace std;
const int MAXN = 3e5 + 10;
const int MOD = 1e9 + 7;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
if(p==0) return 1 % MOD;
int r = bm(b, p >> 1);
if(p&1) return (((r*r) % MOD) * b) % MOD;
return (r*r) % MOD;
}
int inv(int b) {
return bm(b, MOD-2);
}
int fastlog(int x) {
return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
int n, m;
vector<int> adj[1029];
int dp[1029];
void dpf(int node, int prv) {
dp[node] = 1;
for(int x: adj[node]) {
if(adj[x].size() == 1) continue;
if(x == prv) continue;
dpf(x, node);
dp[node] += dp[x];
}
}
vector<int> dfs(int node, int prv) {
int ct=0;
vector<pair<int, int> > vt;
for(int x: adj[node]) {
if(x != prv && adj[x].size() > 1) {
ct += (dp[x] > 1);
vt.push_back({dp[x], x});
}
}
if(ct > 2) {
return {-1};
}
if(prv != -1 && ct > 1) {
return {-1};
}
sort(vt.begin(), vt.end());
if(vt.empty()) {
return {node};
}
int to = vt[vt.size() - 1].second;
vector<int> res;
res = dfs(to, node);
if(res[0] == to) reverse(res.begin(), res.end());
res.push_back(node);
for(int i=0; i+1<vt.size(); i++) {
to = vt[i].second;
vector<int> get = dfs(to, node);
if(get[0] != to) reverse(get.begin(), get.end());
for(int x: get) res.push_back(x);
if(!(prv == -1 && i+2 == vt.size()))res.push_back(node);
}
for(int x: res) if(x == -1) return {-1};
return res;
}
void solve(int tc) {
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;
}
if(n == 2) {
cout << "YES\n2\n1 1\n"; return;
}
if(n == 1) {
cout << "YES\n1\n1\n"; return;
}
for(int rt=1; rt<=n; rt++) {
if(adj[rt].size() > 1) {
for(int i=1; i<=n; i++) dp[i] = 0;
dpf(rt, -1);
vector<int> seq = dfs(rt, -1);
if(seq[0] != -1) {
cout << "YES\n";
cout << seq.size() * 2 << "\n";
for(int i=0; i<seq.size(); i++) cout << seq[i] << ' ';
if(adj[rt].size() & 1) for(int i=0; i<seq.size(); i++) cout << seq[i] << ' ';
else for(int i=seq.size()-1; i>=0; i--) cout << seq[i] << ' ';
cout << "\n"; return;
}
}
}
cout << "NO\n";
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
Compilation message
newspapers.cpp: In function 'std::vector<int> dfs(int, int)':
newspapers.cpp:58:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int i=0; i+1<vt.size(); i++) {
| ~~~^~~~~~~~~~
newspapers.cpp:63:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | if(!(prv == -1 && i+2 == vt.size()))res.push_back(node);
| ~~~~^~~~~~~~~~~~
newspapers.cpp: In function 'void solve(int)':
newspapers.cpp:93:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for(int i=0; i<seq.size(); i++) cout << seq[i] << ' ';
| ~^~~~~~~~~~~
newspapers.cpp:94:46: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | if(adj[rt].size() & 1) for(int i=0; i<seq.size(); i++) cout << seq[i] << ' ';
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
352 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Partially correct |
1 ms |
356 KB |
Failed to provide a successful strategy. |
13 |
Partially correct |
1 ms |
340 KB |
Failed to provide a successful strategy. |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
352 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
352 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
352 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
352 KB |
Output is correct |
29 |
Correct |
0 ms |
352 KB |
Output is correct |
30 |
Partially correct |
1 ms |
348 KB |
Failed to provide a successful strategy. |
31 |
Partially correct |
1 ms |
340 KB |
Failed to provide a successful strategy. |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
352 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
0 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
360 KB |
Output is correct |
45 |
Correct |
1 ms |
352 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
0 ms |
352 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
352 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
0 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
0 ms |
340 KB |
Output is correct |
58 |
Correct |
0 ms |
340 KB |
Output is correct |
59 |
Correct |
0 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
352 KB |
Output is correct |
61 |
Correct |
1 ms |
468 KB |
Output is correct |
62 |
Correct |
0 ms |
340 KB |
Output is correct |
63 |
Correct |
1 ms |
340 KB |
Output is correct |
64 |
Correct |
1 ms |
360 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 |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 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 |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 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 |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
352 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Partially correct |
1 ms |
356 KB |
Failed to provide a successful strategy. |
13 |
Partially correct |
1 ms |
340 KB |
Failed to provide a successful strategy. |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
352 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
352 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
352 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
352 KB |
Output is correct |
29 |
Correct |
0 ms |
352 KB |
Output is correct |
30 |
Partially correct |
1 ms |
348 KB |
Failed to provide a successful strategy. |
31 |
Partially correct |
1 ms |
340 KB |
Failed to provide a successful strategy. |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
352 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
0 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
360 KB |
Output is correct |
45 |
Correct |
1 ms |
352 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
0 ms |
352 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
352 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
0 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
0 ms |
340 KB |
Output is correct |
58 |
Correct |
0 ms |
340 KB |
Output is correct |
59 |
Correct |
0 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
352 KB |
Output is correct |
61 |
Correct |
1 ms |
468 KB |
Output is correct |
62 |
Correct |
0 ms |
340 KB |
Output is correct |
63 |
Correct |
1 ms |
340 KB |
Output is correct |
64 |
Correct |
1 ms |
360 KB |
Output is correct |
65 |
Correct |
1 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 |
1 ms |
352 KB |
Output is correct |
69 |
Correct |
0 ms |
352 KB |
Output is correct |
70 |
Correct |
1 ms |
340 KB |
Output is correct |
71 |
Correct |
1 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
340 KB |
Output is correct |
73 |
Correct |
1 ms |
340 KB |
Output is correct |
74 |
Correct |
0 ms |
340 KB |
Output is correct |
75 |
Correct |
1 ms |
340 KB |
Output is correct |
76 |
Partially correct |
1 ms |
340 KB |
Failed to provide a successful strategy. |
77 |
Partially correct |
1 ms |
352 KB |
Failed to provide a successful strategy. |
78 |
Correct |
0 ms |
340 KB |
Output is correct |
79 |
Correct |
3 ms |
340 KB |
Output is correct |
80 |
Correct |
4 ms |
360 KB |
Output is correct |
81 |
Correct |
4 ms |
340 KB |
Output is correct |
82 |
Correct |
4 ms |
340 KB |
Output is correct |
83 |
Correct |
4 ms |
340 KB |
Output is correct |
84 |
Correct |
4 ms |
340 KB |
Output is correct |
85 |
Correct |
5 ms |
340 KB |
Output is correct |
86 |
Correct |
4 ms |
356 KB |
Output is correct |
87 |
Correct |
6 ms |
340 KB |
Output is correct |
88 |
Correct |
3 ms |
352 KB |
Output is correct |
89 |
Correct |
1 ms |
468 KB |
Output is correct |
90 |
Correct |
1 ms |
468 KB |
Output is correct |
91 |
Correct |
1 ms |
468 KB |
Output is correct |
92 |
Correct |
1 ms |
468 KB |
Output is correct |
93 |
Correct |
1 ms |
468 KB |
Output is correct |
94 |
Correct |
1 ms |
488 KB |
Output is correct |
95 |
Correct |
1 ms |
468 KB |
Output is correct |
96 |
Correct |
1 ms |
468 KB |
Output is correct |
97 |
Correct |
1 ms |
468 KB |
Output is correct |
98 |
Correct |
1 ms |
468 KB |
Output is correct |
99 |
Correct |
1 ms |
468 KB |
Output is correct |
100 |
Correct |
1 ms |
496 KB |
Output is correct |
101 |
Correct |
1 ms |
472 KB |
Output is correct |
102 |
Correct |
1 ms |
472 KB |
Output is correct |
103 |
Correct |
1 ms |
628 KB |
Output is correct |
104 |
Correct |
1 ms |
352 KB |
Output is correct |
105 |
Correct |
1 ms |
468 KB |
Output is correct |
106 |
Correct |
1 ms |
468 KB |
Output is correct |
107 |
Correct |
1 ms |
468 KB |
Output is correct |
108 |
Correct |
1 ms |
468 KB |
Output is correct |
109 |
Correct |
1 ms |
468 KB |
Output is correct |
110 |
Partially correct |
1 ms |
468 KB |
Failed to provide a successful strategy. |
111 |
Partially correct |
1 ms |
468 KB |
Failed to provide a successful strategy. |
112 |
Correct |
1 ms |
468 KB |
Output is correct |
113 |
Correct |
1 ms |
468 KB |
Output is correct |
114 |
Correct |
1 ms |
468 KB |
Output is correct |
115 |
Correct |
1 ms |
468 KB |
Output is correct |
116 |
Correct |
1 ms |
468 KB |
Output is correct |
117 |
Correct |
1 ms |
468 KB |
Output is correct |
118 |
Correct |
1 ms |
468 KB |
Output is correct |
119 |
Correct |
1 ms |
592 KB |
Output is correct |
120 |
Correct |
1 ms |
468 KB |
Output is correct |
121 |
Correct |
1 ms |
468 KB |
Output is correct |
122 |
Correct |
1 ms |
484 KB |
Output is correct |
123 |
Correct |
1 ms |
468 KB |
Output is correct |
124 |
Correct |
1 ms |
468 KB |
Output is correct |
125 |
Correct |
1 ms |
468 KB |
Output is correct |
126 |
Correct |
1 ms |
372 KB |
Output is correct |
127 |
Correct |
1 ms |
468 KB |
Output is correct |
128 |
Correct |
1 ms |
468 KB |
Output is correct |
129 |
Correct |
6 ms |
340 KB |
Output is correct |
130 |
Correct |
5 ms |
392 KB |
Output is correct |
131 |
Correct |
7 ms |
392 KB |
Output is correct |
132 |
Correct |
5 ms |
392 KB |
Output is correct |
133 |
Correct |
5 ms |
388 KB |
Output is correct |
134 |
Correct |
17 ms |
1892 KB |
Output is correct |
135 |
Correct |
11 ms |
2004 KB |
Output is correct |
136 |
Correct |
28 ms |
4332 KB |
Output is correct |
137 |
Correct |
19 ms |
2804 KB |
Output is correct |
138 |
Correct |
31 ms |
3180 KB |
Output is correct |
139 |
Correct |
56 ms |
4940 KB |
Output is correct |
140 |
Correct |
9 ms |
1236 KB |
Output is correct |
141 |
Correct |
3 ms |
724 KB |
Output is correct |
142 |
Correct |
51 ms |
4592 KB |
Output is correct |
143 |
Correct |
59 ms |
4316 KB |
Output is correct |
144 |
Correct |
1 ms |
596 KB |
Output is correct |
145 |
Correct |
1 ms |
596 KB |
Output is correct |