#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ff first
#define ss second
#define all(x) begin(x), end(x)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define F0R(i, x) FOR(i, 0, x)
#define FORd(i, a, b) for(int i = (b)-1; i >= (a); i--)
#define F0Rd(i, x) FORd(i, 0, x)
#define ckif(a, b, c) ((c) ? (a) : (b))
const int MAX_N = 2e5+69;
const ll MOD = 1000000007;
const ll INF = 1e18;
int n, m;
vector<int> adj[MAX_N];
int anc[MAX_N][20], dep[MAX_N], vis[MAX_N];
pii mx[MAX_N], mx2[MAX_N];
pair<int, pii> diam[MAX_N];
bool dfs(int curr, int par = -1){
vis[curr] = 1, mx[curr] = {0, curr};
for(auto x : adj[curr]){
if(x == par) continue;
if(vis[x]) return false;
anc[x][0] = curr;
dep[x] = dep[curr]+1;
if(!dfs(x, curr)) return false;
if(mx[x].ff+1 >= mx[curr].ff) mx2[curr] = mx[curr], mx[curr] = mx[x], mx[curr].ff++;
else if(mx[x].ff+1 >= mx2[curr].ff) mx2[curr] = mx[x], mx2[curr].ff++;
if(diam[x].ff >= diam[curr].ff) diam[curr] = diam[x];
}
if(mx[curr].ff+mx2[curr].ff >= diam[curr].ff) diam[curr] = {mx[curr].ff+mx2[curr].ff, {mx[curr].ss, mx2[curr].ss}};
return true;
}
int lca(int a, int b){
if(dep[a] > dep[b]) swap(a, b);
F0Rd(i, 20) if(dep[a]+(1 << i) <= dep[b])
b = anc[b][i];
if(a == b) return a;
F0Rd(i, 20) if(anc[a][i] != anc[b][i])
a = anc[a][i], b = anc[b][i];
return anc[a][0];
}
int dist(int a, int b){
return dep[a]+dep[b]-dep[lca(a, b)]*2;
}
int main(int argc, const char * argv[]){
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
cin >> n >> m;
if(n == 1){
cout << "YES\n";
cout << "1\n1\n";
return 0;
}
if(n == 2){
cout << "YES\n";
cout << "2\n1 1\n";
return 0;
}
F0R(i, m){
int a, b; cin >> a >> b; a--, b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
if(!dfs(0)){
cout << "NO\n";
return 0;
}
FOR(d, 1, 20) F0R(i, n)
anc[i][d] = anc[anc[i][d-1]][d-1];
int a = diam[0].ss.ff, b = diam[0].ss.ss, c = lca(a, b);
F0R(i, n){
int x = c^lca(a, i)^lca(b, i);
if(dist(i, x) > 2){
cout << "NO\n";
return 0;
}
}
cout << "YES\n";
if(dep[a] < dep[b]) swap(a, b);
vector<int> seq, seq2;
while(anc[a][0] != c){
int tmp = a; a = anc[a][0];
seq.push_back(a);
for(auto x : adj[a]) if(x != tmp && x != anc[a][0] && adj[x].size() > 1){
seq.push_back(x), seq.push_back(a);
}
}
while(anc[b][0] != c){
int tmp = b; b = anc[b][0];
seq2.push_back(b);
for(auto x : adj[b]) if(x != tmp && x != anc[b][0] && adj[x].size() > 1)
seq2.push_back(x), seq2.push_back(b);
}
if(b != c){
seq.push_back(c);
for(auto x : adj[c]) if(x != a && x != b && adj[x].size() > 1)
seq.push_back(x), seq.push_back(c);
reverse(all(seq2));
for(auto x : seq2) seq.push_back(x);
}
cout << 2*seq.size() << '\n';
for(auto x : seq) cout << x+1 << ' ';
reverse(all(seq));
for(auto x : seq) cout << x+1 << ' ';
cout << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7004 KB |
Output is correct |
2 |
Correct |
2 ms |
7004 KB |
Output is correct |
3 |
Correct |
2 ms |
13148 KB |
Output is correct |
4 |
Correct |
2 ms |
13148 KB |
Output is correct |
5 |
Correct |
2 ms |
13148 KB |
Output is correct |
6 |
Correct |
2 ms |
13148 KB |
Output is correct |
7 |
Correct |
3 ms |
13148 KB |
Output is correct |
8 |
Correct |
3 ms |
13240 KB |
Output is correct |
9 |
Correct |
2 ms |
13148 KB |
Output is correct |
10 |
Correct |
2 ms |
13148 KB |
Output is correct |
11 |
Correct |
2 ms |
13148 KB |
Output is correct |
12 |
Correct |
2 ms |
13148 KB |
Output is correct |
13 |
Correct |
3 ms |
13148 KB |
Output is correct |
14 |
Correct |
2 ms |
13148 KB |
Output is correct |
15 |
Correct |
3 ms |
13148 KB |
Output is correct |
16 |
Correct |
2 ms |
13148 KB |
Output is correct |
17 |
Correct |
2 ms |
13148 KB |
Output is correct |
18 |
Correct |
3 ms |
13148 KB |
Output is correct |
19 |
Correct |
2 ms |
13148 KB |
Output is correct |
20 |
Correct |
2 ms |
13144 KB |
Output is correct |
21 |
Correct |
2 ms |
13148 KB |
Output is correct |
22 |
Correct |
3 ms |
13312 KB |
Output is correct |
23 |
Correct |
2 ms |
13148 KB |
Output is correct |
24 |
Correct |
2 ms |
13148 KB |
Output is correct |
25 |
Correct |
2 ms |
13148 KB |
Output is correct |
26 |
Correct |
2 ms |
13148 KB |
Output is correct |
27 |
Correct |
2 ms |
13148 KB |
Output is correct |
28 |
Correct |
2 ms |
13400 KB |
Output is correct |
29 |
Correct |
3 ms |
13148 KB |
Output is correct |
30 |
Correct |
2 ms |
13148 KB |
Output is correct |
31 |
Correct |
2 ms |
13144 KB |
Output is correct |
32 |
Correct |
3 ms |
13148 KB |
Output is correct |
33 |
Correct |
2 ms |
13148 KB |
Output is correct |
34 |
Correct |
2 ms |
13148 KB |
Output is correct |
35 |
Correct |
3 ms |
13324 KB |
Output is correct |
36 |
Correct |
2 ms |
13148 KB |
Output is correct |
37 |
Correct |
2 ms |
13148 KB |
Output is correct |
38 |
Correct |
2 ms |
13148 KB |
Output is correct |
39 |
Correct |
2 ms |
13148 KB |
Output is correct |
40 |
Correct |
3 ms |
13148 KB |
Output is correct |
41 |
Correct |
2 ms |
13240 KB |
Output is correct |
42 |
Correct |
2 ms |
13148 KB |
Output is correct |
43 |
Correct |
2 ms |
13312 KB |
Output is correct |
44 |
Correct |
2 ms |
13148 KB |
Output is correct |
45 |
Correct |
2 ms |
13148 KB |
Output is correct |
46 |
Correct |
3 ms |
13148 KB |
Output is correct |
47 |
Correct |
3 ms |
13148 KB |
Output is correct |
48 |
Correct |
2 ms |
13148 KB |
Output is correct |
49 |
Correct |
3 ms |
13148 KB |
Output is correct |
50 |
Correct |
3 ms |
13148 KB |
Output is correct |
51 |
Correct |
3 ms |
13148 KB |
Output is correct |
52 |
Correct |
2 ms |
13144 KB |
Output is correct |
53 |
Correct |
2 ms |
13148 KB |
Output is correct |
54 |
Correct |
2 ms |
13148 KB |
Output is correct |
55 |
Correct |
2 ms |
13148 KB |
Output is correct |
56 |
Correct |
2 ms |
13144 KB |
Output is correct |
57 |
Correct |
2 ms |
13148 KB |
Output is correct |
58 |
Correct |
2 ms |
13148 KB |
Output is correct |
59 |
Correct |
2 ms |
13240 KB |
Output is correct |
60 |
Correct |
2 ms |
13148 KB |
Output is correct |
61 |
Correct |
3 ms |
13148 KB |
Output is correct |
62 |
Correct |
2 ms |
13148 KB |
Output is correct |
63 |
Correct |
2 ms |
13148 KB |
Output is correct |
64 |
Correct |
2 ms |
13148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7004 KB |
Output is correct |
2 |
Correct |
1 ms |
7004 KB |
Output is correct |
3 |
Correct |
2 ms |
13148 KB |
Output is correct |
4 |
Correct |
3 ms |
13148 KB |
Output is correct |
5 |
Correct |
2 ms |
13148 KB |
Output is correct |
6 |
Correct |
2 ms |
13148 KB |
Output is correct |
7 |
Correct |
2 ms |
13148 KB |
Output is correct |
8 |
Correct |
2 ms |
13148 KB |
Output is correct |
9 |
Correct |
2 ms |
13144 KB |
Output is correct |
10 |
Correct |
2 ms |
13148 KB |
Output is correct |
11 |
Correct |
3 ms |
13404 KB |
Output is correct |
12 |
Correct |
2 ms |
13148 KB |
Output is correct |
13 |
Correct |
3 ms |
13404 KB |
Output is correct |
14 |
Correct |
3 ms |
13404 KB |
Output is correct |
15 |
Correct |
4 ms |
13404 KB |
Output is correct |
16 |
Correct |
3 ms |
13404 KB |
Output is correct |
17 |
Correct |
3 ms |
13404 KB |
Output is correct |
18 |
Correct |
3 ms |
13400 KB |
Output is correct |
19 |
Correct |
3 ms |
13404 KB |
Output is correct |
20 |
Correct |
3 ms |
13404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7004 KB |
Output is correct |
2 |
Correct |
2 ms |
7004 KB |
Output is correct |
3 |
Correct |
2 ms |
13148 KB |
Output is correct |
4 |
Correct |
2 ms |
13148 KB |
Output is correct |
5 |
Correct |
2 ms |
13148 KB |
Output is correct |
6 |
Correct |
2 ms |
13148 KB |
Output is correct |
7 |
Correct |
3 ms |
13148 KB |
Output is correct |
8 |
Correct |
3 ms |
13240 KB |
Output is correct |
9 |
Correct |
2 ms |
13148 KB |
Output is correct |
10 |
Correct |
2 ms |
13148 KB |
Output is correct |
11 |
Correct |
2 ms |
13148 KB |
Output is correct |
12 |
Correct |
2 ms |
13148 KB |
Output is correct |
13 |
Correct |
3 ms |
13148 KB |
Output is correct |
14 |
Correct |
2 ms |
13148 KB |
Output is correct |
15 |
Correct |
3 ms |
13148 KB |
Output is correct |
16 |
Correct |
2 ms |
13148 KB |
Output is correct |
17 |
Correct |
2 ms |
13148 KB |
Output is correct |
18 |
Correct |
3 ms |
13148 KB |
Output is correct |
19 |
Correct |
2 ms |
13148 KB |
Output is correct |
20 |
Correct |
2 ms |
13144 KB |
Output is correct |
21 |
Correct |
2 ms |
13148 KB |
Output is correct |
22 |
Correct |
3 ms |
13312 KB |
Output is correct |
23 |
Correct |
2 ms |
13148 KB |
Output is correct |
24 |
Correct |
2 ms |
13148 KB |
Output is correct |
25 |
Correct |
2 ms |
13148 KB |
Output is correct |
26 |
Correct |
2 ms |
13148 KB |
Output is correct |
27 |
Correct |
2 ms |
13148 KB |
Output is correct |
28 |
Correct |
2 ms |
13400 KB |
Output is correct |
29 |
Correct |
3 ms |
13148 KB |
Output is correct |
30 |
Correct |
2 ms |
13148 KB |
Output is correct |
31 |
Correct |
2 ms |
13144 KB |
Output is correct |
32 |
Correct |
3 ms |
13148 KB |
Output is correct |
33 |
Correct |
2 ms |
13148 KB |
Output is correct |
34 |
Correct |
2 ms |
13148 KB |
Output is correct |
35 |
Correct |
3 ms |
13324 KB |
Output is correct |
36 |
Correct |
2 ms |
13148 KB |
Output is correct |
37 |
Correct |
2 ms |
13148 KB |
Output is correct |
38 |
Correct |
2 ms |
13148 KB |
Output is correct |
39 |
Correct |
2 ms |
13148 KB |
Output is correct |
40 |
Correct |
3 ms |
13148 KB |
Output is correct |
41 |
Correct |
2 ms |
13240 KB |
Output is correct |
42 |
Correct |
2 ms |
13148 KB |
Output is correct |
43 |
Correct |
2 ms |
13312 KB |
Output is correct |
44 |
Correct |
2 ms |
13148 KB |
Output is correct |
45 |
Correct |
2 ms |
13148 KB |
Output is correct |
46 |
Correct |
3 ms |
13148 KB |
Output is correct |
47 |
Correct |
3 ms |
13148 KB |
Output is correct |
48 |
Correct |
2 ms |
13148 KB |
Output is correct |
49 |
Correct |
3 ms |
13148 KB |
Output is correct |
50 |
Correct |
3 ms |
13148 KB |
Output is correct |
51 |
Correct |
3 ms |
13148 KB |
Output is correct |
52 |
Correct |
2 ms |
13144 KB |
Output is correct |
53 |
Correct |
2 ms |
13148 KB |
Output is correct |
54 |
Correct |
2 ms |
13148 KB |
Output is correct |
55 |
Correct |
2 ms |
13148 KB |
Output is correct |
56 |
Correct |
2 ms |
13144 KB |
Output is correct |
57 |
Correct |
2 ms |
13148 KB |
Output is correct |
58 |
Correct |
2 ms |
13148 KB |
Output is correct |
59 |
Correct |
2 ms |
13240 KB |
Output is correct |
60 |
Correct |
2 ms |
13148 KB |
Output is correct |
61 |
Correct |
3 ms |
13148 KB |
Output is correct |
62 |
Correct |
2 ms |
13148 KB |
Output is correct |
63 |
Correct |
2 ms |
13148 KB |
Output is correct |
64 |
Correct |
2 ms |
13148 KB |
Output is correct |
65 |
Correct |
2 ms |
7000 KB |
Output is correct |
66 |
Correct |
2 ms |
7000 KB |
Output is correct |
67 |
Correct |
2 ms |
13508 KB |
Output is correct |
68 |
Correct |
2 ms |
13160 KB |
Output is correct |
69 |
Correct |
2 ms |
13240 KB |
Output is correct |
70 |
Correct |
2 ms |
13148 KB |
Output is correct |
71 |
Correct |
3 ms |
13148 KB |
Output is correct |
72 |
Correct |
3 ms |
13148 KB |
Output is correct |
73 |
Correct |
3 ms |
13148 KB |
Output is correct |
74 |
Correct |
2 ms |
13148 KB |
Output is correct |
75 |
Correct |
2 ms |
13148 KB |
Output is correct |
76 |
Correct |
2 ms |
13144 KB |
Output is correct |
77 |
Correct |
2 ms |
13148 KB |
Output is correct |
78 |
Correct |
3 ms |
13148 KB |
Output is correct |
79 |
Correct |
2 ms |
13148 KB |
Output is correct |
80 |
Correct |
2 ms |
13148 KB |
Output is correct |
81 |
Correct |
3 ms |
13148 KB |
Output is correct |
82 |
Correct |
2 ms |
13148 KB |
Output is correct |
83 |
Correct |
3 ms |
13148 KB |
Output is correct |
84 |
Correct |
2 ms |
13148 KB |
Output is correct |
85 |
Correct |
3 ms |
13148 KB |
Output is correct |
86 |
Correct |
2 ms |
13148 KB |
Output is correct |
87 |
Correct |
3 ms |
13408 KB |
Output is correct |
88 |
Correct |
3 ms |
13148 KB |
Output is correct |
89 |
Correct |
3 ms |
13400 KB |
Output is correct |
90 |
Correct |
3 ms |
13400 KB |
Output is correct |
91 |
Correct |
3 ms |
13404 KB |
Output is correct |
92 |
Correct |
3 ms |
13404 KB |
Output is correct |
93 |
Correct |
3 ms |
13404 KB |
Output is correct |
94 |
Correct |
3 ms |
13148 KB |
Output is correct |
95 |
Correct |
3 ms |
13404 KB |
Output is correct |
96 |
Correct |
3 ms |
13404 KB |
Output is correct |
97 |
Correct |
3 ms |
13144 KB |
Output is correct |
98 |
Correct |
4 ms |
13400 KB |
Output is correct |
99 |
Correct |
3 ms |
13356 KB |
Output is correct |
100 |
Correct |
3 ms |
13148 KB |
Output is correct |
101 |
Correct |
3 ms |
13404 KB |
Output is correct |
102 |
Correct |
3 ms |
13404 KB |
Output is correct |
103 |
Correct |
3 ms |
13400 KB |
Output is correct |
104 |
Correct |
3 ms |
13400 KB |
Output is correct |
105 |
Correct |
3 ms |
13404 KB |
Output is correct |
106 |
Correct |
3 ms |
13400 KB |
Output is correct |
107 |
Correct |
3 ms |
13144 KB |
Output is correct |
108 |
Correct |
3 ms |
13400 KB |
Output is correct |
109 |
Correct |
3 ms |
13404 KB |
Output is correct |
110 |
Correct |
3 ms |
13400 KB |
Output is correct |
111 |
Correct |
3 ms |
13148 KB |
Output is correct |
112 |
Correct |
3 ms |
13148 KB |
Output is correct |
113 |
Correct |
3 ms |
13400 KB |
Output is correct |
114 |
Correct |
3 ms |
13404 KB |
Output is correct |
115 |
Correct |
4 ms |
13408 KB |
Output is correct |
116 |
Correct |
3 ms |
13404 KB |
Output is correct |
117 |
Correct |
3 ms |
13404 KB |
Output is correct |
118 |
Correct |
3 ms |
13400 KB |
Output is correct |
119 |
Correct |
3 ms |
13404 KB |
Output is correct |
120 |
Correct |
3 ms |
13400 KB |
Output is correct |
121 |
Correct |
3 ms |
13400 KB |
Output is correct |
122 |
Correct |
3 ms |
13404 KB |
Output is correct |
123 |
Correct |
3 ms |
13404 KB |
Output is correct |
124 |
Correct |
3 ms |
13404 KB |
Output is correct |
125 |
Correct |
3 ms |
13148 KB |
Output is correct |
126 |
Correct |
3 ms |
13404 KB |
Output is correct |
127 |
Correct |
3 ms |
13148 KB |
Output is correct |
128 |
Correct |
3 ms |
13404 KB |
Output is correct |
129 |
Correct |
3 ms |
13148 KB |
Output is correct |
130 |
Correct |
2 ms |
13148 KB |
Output is correct |
131 |
Correct |
3 ms |
13148 KB |
Output is correct |
132 |
Correct |
3 ms |
13148 KB |
Output is correct |
133 |
Correct |
3 ms |
13148 KB |
Output is correct |
134 |
Correct |
13 ms |
14052 KB |
Output is correct |
135 |
Correct |
12 ms |
14424 KB |
Output is correct |
136 |
Correct |
25 ms |
16732 KB |
Output is correct |
137 |
Correct |
19 ms |
15192 KB |
Output is correct |
138 |
Correct |
30 ms |
15696 KB |
Output is correct |
139 |
Correct |
51 ms |
17488 KB |
Output is correct |
140 |
Correct |
9 ms |
13916 KB |
Output is correct |
141 |
Correct |
5 ms |
13400 KB |
Output is correct |
142 |
Correct |
44 ms |
17440 KB |
Output is correct |
143 |
Correct |
56 ms |
17236 KB |
Output is correct |
144 |
Correct |
3 ms |
13404 KB |
Output is correct |
145 |
Correct |
3 ms |
13404 KB |
Output is correct |