This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using ll = long long;
#define int ll
using namespace std;
#define sz(x) (int)(x).size()
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, l, r) for(int i = l; i >= r; i--)
#define fi first
#define se second
#define mod 998244353
#define db(x) cerr << __LINE__ << " " << #x << " " << x << "\n"
using vi = vector<int>;
using pi = pair<int, int>;
const int N = 1005;
const int inf = 3e18;
int n, m, par[N], used[N], ok[N];
vi adj[N], ret;
pi dfs(int x, int p = -1){
pi res(0, x);
for(auto &i : adj[x]){
if(i == p) continue;
par[i] = x;
auto j = dfs(i, x);
j.fi++;
res = max(res, j);
}
return res;
}
void solve(){
cin >> n >> m;
if(n == 1){
cout << "YES\n1\n1"; return;
}
if(n == 2){
cout << "YES\n2\n1 1"; return;
}
if(m != n - 1){
cout << "NO"; return;
}
foru(i, 1, n - 1){
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
int o = dfs(1).se;
int z = dfs(o).se;
for(int i = z; ; i = par[i]){
ok[i] = 1;
used[i] = 1;
for(auto &j : adj[i]){
ok[j] = 1;
for(auto &k : adj[j]) ok[k] = 1;
}
if(i == o) break;
}
for(auto &i : ok) if(i == 0){
break;
cout << "NO"; return;
}
for(int i = par[z]; i != o; i = par[i]){
ret.push_back(i);
for(auto &j : adj[i]){
if(sz(adj[j]) == 1 or used[j]) continue;
ret.push_back(j);
ret.push_back(i);
}
}
cout << "YES\n" << (sz(ret) << 1) << "\n";
foru(i, 0, sz(ret) - 1) cout << ret[i] << " ";
ford(i, sz(ret) - 1, 0) cout << ret[i] << " ";
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int t = 1; // cin >> t;
while(t--){
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |