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 namespace std;
int main(){
int n,m;
cin>>n>>m;
if (m>=n){
cout<<"NO\n";
return 0;
}
if (n==1){
cout<<"YES\n1\n1\n";
return 0;
}
if (n==2){
cout<<"YES\n2\n1 1\n";
return 0;
}
vector <int> g[n+1];
for (int i=1; i<=m; i++){
int u,v; cin>>u>>v;
g[u].push_back(v); g[v].push_back(u);
}
bool visited[n+1];
int dist[n+1];
queue <int> q;
for (int i=1; i<=n; i++) visited[i]=0;
visited[1]=1;
dist[1]=0;
q.push(1);
while (!q.empty()){
int tp=q.front(); q.pop();
for (int i:g[tp]){
if (!visited[i]){
visited[i]=1;
q.push(i);
dist[i]=dist[tp]+1;
}
}
}
int u=max_element(dist+1,dist+n+1)-dist;
for (int i=1; i<=n; i++) visited[i]=0;
visited[u]=1;
q.push(u);
int bac[n+1];
dist[u]=0;
while (!q.empty()){
int tp=q.front(); q.pop();
for (int i:g[tp]){
if (!visited[i]){
visited[i]=1;
q.push(i);
dist[i]=dist[tp]+1;
bac[i]=tp;
}
}
}
int v=max_element(dist+1,dist+n+1)-dist;
for (int i=1; i<=n; i++) visited[i]=0;
pair <int,int> d[n+1];
for (int i=1; i<=n; i++) d[i]={0,0};
int tp=v;
while (tp!=u){
d[tp]={tp,0};
q.push(tp);
visited[tp]=1;
tp=bac[tp];
}
q.push(u);
visited[u]=1;
bool need[n+1];
for (int i=1; i<=n; i++) need[i]=0;
while (!q.empty()){
int tp=q.front(); q.pop();
for (int i:g[tp]){
if (!visited[i]){
visited[i]=1;
q.push(i);
d[i]={d[tp].first,d[tp].second+1};
if (d[i].second>=2) need[tp]=1;
}
}
}
for (int i=1; i<=n; i++){
if (d[i].second>=3){
cout<<"NO\n";
return 0;
}
}
cout<<"YES\n";
vector <int> vec;
tp=bac[v];
while (tp!=u){
vec.push_back(tp);
for (int i:g[tp]){
if (d[i].first==tp&&need[i]){
vec.push_back(i);
vec.push_back(tp);
}
}
tp=bac[tp];
}
vector <int> cpy=vec;
reverse(cpy.begin(),cpy.end());
for (int i:cpy) vec.push_back(i);
cout<<vec.size()<<'\n';
for (int i:vec) cout<<i<<' ';
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... |