# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1150870 | bilgun | Newspapers (CEOI21_newspapers) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
using namespace std;
long long n, m;
vector<int> g[1005];
int par[1005], path[1005];
vector<int> ans;
pair<int, int> dfs(int node, int parent) {
int maxd = 0;
int distm = node;
for (auto it : g[node]) {
if (it != parent) {
par[it] = node;
auto s = dfs(it, node);
s.first++;
if (s.first > maxd) {
maxd = s.first;
distm = s.second;
}
}
}
return {maxd, distm};
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
if (m != n - 1) {
cout << "NO";
return 0;
}
for (int i = 1; i <= m; i++) {
long long a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
int u = dfs(1, -1).second;
int v = dfs(u, -1).second;
for( int curr = v; ; curr = par[curr]){
h[curr] = 1;
path[curr] = 1;
for( auto it : g[curr]){
h[it] = 1;
for( auto i : g[it]){
h[i] = 1;
}
}
if( curr == u) break;
}
bool isUnvisited = false;
for (int i = 1; i <= n; i++) {
if (path[i] == 0) {
isUnvisited = true;
break;
}
}
if (isUnvisited) {
cout << "NO" << endl;
return 0;
}
for(int i = par[v]; i != u; i = par[i]){
ans.push_back(i);
for(auto &j : g[i]){
if(sz(g[j]) == 1 || path[j]) continue;
ans.push_back(j);
ans.push_back(i);
}
}
cout << "YES" << endl;
cout << 2 * ans.size() << endl;
for (int x : ans) cout << x << " ";
reverse(ans.begin(), ans.end());
for (int x : ans) cout << x << " ";
cout << endl;
return 0;
}