#include <bits/stdc++.h>
#ifdef ONPC
#include "t_debug.cpp"
#else
#define debug(...) 42
#endif
#define sz(a) ((int)(a).size())
using namespace std;
using ll = long long;
const int INF = 1e9;
const ll INFL = 1e18;
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rng(RANDOM);
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U>& p) { return is >> p.first >> p.second; }
const int N = 2e5, LOGN = 17, MOD = 1e9+7;
int solve() {
int n;
if (!(cin >> n)) {
return 1;
}
int m; cin >> m;
vector<vector<int>> adj(n);
for (int i = 0;i < m; i++) {
int u, v;
cin >> u >> v;
u--; v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
if (m != n-1) {cout << "NO\n"; return 0;}
bool yes = true;
function<pair<int,int>(int,int)> depth = [&](int i, int p)->pair<int,int> {
int res = 0, rese = i;
for (int e : adj[i]) {
if (e != p) {
auto [d, v] = depth(e, i);
if (d+1 > res) {
res = d+1;
rese = v;
}
}
}
return {res, rese};
};
auto [_d, d1] = depth(0, -1);
auto [_dd, d2] = depth(d1, -1);
vector<int> path = {d1};
vector<int> visited(n);
function<bool(int)> findD = [&](int i) {
if (visited[i]) return false;
if (i == d2) return true;
visited[i] = true;
for (int e : adj[i]) {
path.push_back(e);
if (findD(e)) return true;
path.pop_back();
}
return false;
};
findD(d1);
debug(d1, d2, path);
vector<int> res(n);
for (int i = 1; i < sz(path)-1; i++) {
int v = path[i];
int dep = 0;
for (int e : adj[v]) {
if (e == path[i-1] || e == path[i+1]) continue;
auto [de, end] = depth(e, v); de++;
if (de > 2) yes = false;
if (de > 1) {
res.push_back(e);
res.push_back(v);
}
}
}
cout << (yes ? "YES" : "NO") << '\n';
if (yes) {
cout << sz(res) * 2 << '\n';
for (int i : res) cout << i +1 << ' ';
for (auto it = res.rbegin(); it != res.rend(); it++) {
cout<< *it << ' ';
}
cout << '\n';
}
return 0;
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
#ifdef ONPC
t = 10000;
#endif
while (t-- && cin) {
if (solve()) break;
#ifdef ONPC
cout << "____________________" << endl;
#endif
}
return 0;
}
/*
█████ █████ ███ ████
▒▒███ ▒▒███ ▒▒▒ ▒▒███
███████ ████████ ███████ ████ ▒███ █████ ████ ██████ ████████
███▒▒███ ▒▒███▒▒███ ███▒▒███ ▒▒███ ▒███ ▒▒███ ▒███ ███▒▒███▒▒███▒▒███
▒███ ▒███ ▒███ ▒▒▒ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒▒▒
▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███
▒▒████████ █████ ▒▒████████ █████ █████ ▒▒███████ ▒▒██████ █████
▒▒▒▒▒▒▒▒ ▒▒▒▒▒ ▒▒▒▒▒▒▒▒ ▒▒▒▒▒ ▒▒▒▒▒ ▒▒▒▒▒███ ▒▒▒▒▒▒ ▒▒▒▒▒
███ ▒███
▒▒██████
▒▒▒▒▒▒
*/
Compilation message
newspapers.cpp: In function 'int solve()':
newspapers.cpp:5:24: warning: statement has no effect [-Wunused-value]
5 | #define debug(...) 42
| ^~
newspapers.cpp:66:5: note: in expansion of macro 'debug'
66 | debug(d1, d2, path);
| ^~~~~
newspapers.cpp:71:13: warning: unused variable 'dep' [-Wunused-variable]
71 | int dep = 0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Integer 0 violates the range [1, 1] |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Integer 0 violates the range [1, 1] |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Integer 0 violates the range [1, 1] |
2 |
Halted |
0 ms |
0 KB |
- |