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>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
string _reset = "\u001b[0m", _yellow = "\u001b[33m", _bold = "\u001b[1m";
void DBG() { cerr << "]" << _reset << endl; }
template<class H, class...T> void DBG(H h, T ...t) {
cerr << to_string(h);
if(sizeof ...(t)) cerr << ", ";
DBG(t...);
}
#ifdef CHEISSMART
#define debug(...) cerr << _yellow << _bold << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define debug(...)
#endif
const int INF = 1e9 + 7, N = 1003;
vi G[N];
int d[N], p[N];
bool onpath[N];
void dfs(int u, int pa = -1, int depth = 0) {
d[u] = depth, p[u] = pa;
for(int v:G[u]) if(v != pa)
dfs(v, u, depth + 1);
}
int mx_depth(int u, int pa) {
int mx = 0;
for(int v:G[u]) if(v != pa)
mx = max(mx, mx_depth(v, u) + 1);
return mx;
}
signed main()
{
IO_OP;
int n, m;
cin >> n >> m;
if(m != n - 1) {
cout << "NO" << '\n';
return 0;
}
if(n == 1) {
cout << "YES\n" << 1 << "\n" << 1 << '\n';
return 0;
}
if(n == 2) {
cout << "YES\n" << 2 << '\n' << "1 1\n";
return 0;
}
for(int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
G[u].PB(v);
G[v].PB(u);
}
dfs(1);
int s = max_element(d + 1, d + n + 1) - d;
dfs(s);
int t = max_element(d + 1, d + n + 1) - d;
vi path;
while(SZ(path) == 0 || path.back() != s) {
path.PB(t);
onpath[t] = true;
t = p[t];
}
assert(SZ(path) >= 3);
vi ans;
for(int i = 1; i < SZ(path) - 1; i++) {
int u = path[i];
ans.PB(u);
for(int v:G[u]) if(!onpath[v]) {
int tt = mx_depth(v, u);
if(tt > 1) {
cout << "NO\n";
return 0;
}
if(tt == 1)
ans.PB(v), ans.PB(u);
}
}
for(int i = SZ(ans) - 1; i >= 0; i--)
ans.PB(ans[i]);
cout << "YES\n";
cout << SZ(ans) << '\n';
for(int u:ans) cout << u << " ";
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... |