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>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>
using namespace std;
// using namespace __gnu_cxx;
// using namespace __gnu_pbds;
void Hollwo_Pelw();
signed main(){
#ifndef hollwo_pelw_local
if (fopen(".inp", "r"))
assert(freopen(".inp", "r", stdin)), assert(freopen(".out", "w", stdout));
#else
using namespace chrono;
auto start = steady_clock::now();
#endif
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
int testcases = 1;
// cin >> testcases;
for (int test = 1; test <= testcases; test++){
// cout << "Case #" << test << ": ";
Hollwo_Pelw();
}
#ifdef hollwo_pelw_local
auto end = steady_clock::now();
cout << "\nExecution time : " << duration_cast<milliseconds> (end - start).count() << "[ms]" << endl;
#endif
}
const int N = 1005;
int n, m, S, T, hS[N], hT[N], path[N];
vector<int> adj[N], res;
void dfs(int u, int p, int *h) {
h[u] = p ? h[p] + 1 : 0;
for (int v : adj[u]) if (v != p)
dfs(v, u, h);
}
void Hollwo_Pelw(){
cin >> n >> m;
if (m >= n) return cout << "NO\n", (void) 0;
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
if (n == 1) return cout << "YES\n1\n1\n", (void) 0;
if (n == 2) return cout << "YES\n2\n1 1\n", (void) 0;
dfs(1, 0, hS);
S = max_element(hS + 1, hS + n + 1) - hS, dfs(S, 0, hS);
T = max_element(hS + 1, hS + n + 1) - hS, dfs(T, 0, hT);
const int LEN = hS[T];
// cout << LEN << '\n';
// cout << S << ' ' << T << '\n';
for (int i = 1; i <= n; i++) {
if (hS[i] + hT[i] > LEN + 4) // can not reach ?
return cout << "NO\n", (void) 0;
if (hS[i] + hT[i] == LEN)
path[hS[i]] = i;
}
// for (int i = 1; i <= n; i++)
// cout << hS[i] + hT[i] << " \n"[i == n];
for (int i = 1; i <= LEN - 1; i++) {
int u = path[i];
res.push_back(u);
for (int v : adj[u])
if (hS[v] + hT[v] > LEN && (int) adj[v].size() > 1)
res.push_back(v), res.push_back(u);
}
for (int i = LEN - 1; i >= 1; i--) {
int u = path[i];
res.push_back(u);
for (int v : adj[u])
if (hS[v] + hT[v] > LEN && (int) adj[v].size() > 1)
res.push_back(v), res.push_back(u);
}
cout << "YES\n";
cout << res.size() << '\n';
for (int x : res)
cout << x << ' ';
cout << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |