#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
const int maxn5 = 1e5 + 10;
vector <int> adj[maxn5], av;
bool mark[maxn5];
int h[maxn5];
void dfs(int v, int par){
for(auto u : adj[v]) if(u != par){
h[u] = h[v] + 1;
dfs(u, v);
}
}
bool dfs_set_mark(int v, int w, int par){
if(v == w){
av.pb(w);
mark[w] = true;
return true;
}
bool re = false;
for(auto u : adj[v]) if(u != par){
re |= dfs_set_mark(u, w, v);
}
if(re){
mark[v] = true;
av.pb(v);
}
return re;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int n, m; cin >> n >> m;
for(int i = 0; i < m; i++){
int a, b; cin >> a >> b;
a--; b--;
adj[a].pb(b);
adj[b].pb(a);
}
if(m != n - 1){
return cout << "NO" << endl, 0;
}
dfs(0, -1);
int d1 = 0;
for(int i = 0; i < n; i++) if(h[i] > h[d1])
d1 = i;
h[d1] = 0;
dfs(d1, -1);
int d2 = 0;
for(int i = 0; i < n; i++) if(h[i] > h[d2])
d2 = i;
dfs_set_mark(d1, d2, -1);
for(int i = 0; i < n; i++) if(!mark[i] && adj[i].size() > 1)
return cout << "YES\n", 0;
cout << "YES\n";
//cout << "its " << d1 << ' ' << d2 << endl;
n = av.size();
if(n == 1){
return cout << 1 << '\n' << av[0] + 1 << endl, 0;
}
if(n == 2){
return cout << 2 << '\n' << av[0] + 1 << ' ' << av[0] + 1 << endl, 0;
}
cout << 2 * (n - 2) << endl;
for(int i = 1; i < n - 1; i++)
cout << av[i] + 1 << ' ';
for(int i = n - 2; i >= 1; i--)
cout << av[i] + 1 << ' ';
cout << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Incorrect |
1 ms |
2644 KB |
Unexpected end of file - int32 expected |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2660 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
3 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
3 ms |
2752 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2772 KB |
Output is correct |
17 |
Correct |
2 ms |
2772 KB |
Output is correct |
18 |
Correct |
2 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2772 KB |
Output is correct |
20 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Incorrect |
1 ms |
2644 KB |
Unexpected end of file - int32 expected |
9 |
Halted |
0 ms |
0 KB |
- |