#include <bits/stdc++.h>
using namespace std;
const int nmax = 1e5 + 1;
vector<int> adj[nmax];
int tata[nmax];
bool f[nmax];
bool viz[nmax];
pair<int, int> dfs(int i, int dad = 0) {
pair<int, int> deep = {0, i};
for (auto it : adj[i]) {
if (it != dad) {
tata[it] = i;
pair<int, int> aux = dfs(it, i);
aux.first++;
deep = max(deep, aux);
}
}
return deep;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
if (m != n - 1) {
cout << "NO\n";
return 0;
}
if (n == 1) {
cout << "YES\n1\n1";
return 0;
}
if (n == 2) {
cout << "YES\n2\n1 1";
return 0;
}
int u = dfs(1).second;
int v = dfs(u).second;
for (int i = v; ; i = tata[i]) {
f[i] = 1;
viz[i] = 1;
for (auto c1 : adj[i]) {
viz[c1] = 1;
for (auto c2 : adj[c1]) {
viz[c2] = 1;
}
}
if (i == u) {
break;
}
}
bool ok = 1;
for (int i = 1; i <= n; i++) {
ok = (ok & viz[i]);
}
if (!ok) {
cout << "NO\n";
return 0;
}
vector<int> ans;
for (int i = tata[v]; i != u; i = tata[i]) {
ans.push_back(i);
for (auto it : adj[i]) {
if (adj[it].size() != 1 && !f[it]) {
ans.push_back(it);
ans.push_back(i);
}
}
}
cout << "YES\n" << ans.size() * 2 + 1 << "\n1 ";
for (auto it : ans) {
cout << it << " ";
}
reverse(ans.begin(), ans.end());
for (auto it : ans) {
cout << it << " ";
}
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |