# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1114598 | vjudge1 | Newspapers (CEOI21_newspapers) | C++17 | 2 ms | 504 KiB |
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;
#define int long long
#define pb push_back
using pii=pair<int,int>;
int parent[5000];
int find(int i){
if(parent[i]==i)return i;
return parent[i]=find(parent[i]);
}
int unite(int i,int j){
i=find(i);j=find(j);
if(i==j)return 1;
parent[i]=j;
return 0;
}
signed main(){
int n,m;
cin>>n>>m;
if(n==1){
cout<<"YES\n1\n1";
return 0;
}
for(int i=1;i<=n;i++)parent[i]=i;
vector<int>v[n+1];
for(int i=0;i<m;i++){
int x,y;
cin>>x>>y;
v[x].pb(y);
v[y].pb(x);
if(unite(x,y)){
cout<<"NO\n";
return 0;
}
}
int guy=0;
for(int i=1;i<=n;i++){
if(v[i].size()>2){
guy=i;
}
}
if(!guy){
for(int i=1;i<=n;i++){
if(v[i].size()==1){
guy=v[i][0];
break;
}
}
}
vector<int>chain;
chain.pb(guy);
while(1){
int past=-1;
if(1<chain.size())past=chain[chain.size()-2];
int now=chain.back();
for(int j:v[now]){
if(j!=past&&v[j].size()>1){
if(chain.back()!=now){
cout<<"NO\n";
return 0;
}
chain.pb(j);
}
}
if(chain.back()==now){
break;
}
}
for(int i=1;i+1<chain.size();i++){
for(int j:v[chain[i]]){
if(j!=chain[i-1]&&j!=chain[i+1]&&v[j].size()>1){
cout<<"NO\n";
return 0;
}
}
}
cout<<"YES\n";
vector<int>ans;
for(int i=0;i<chain.size();i++){
ans.pb(chain[i]);
}
for(int i=chain.size()-1;0<=i;i--){
ans.pb(chain[i]);
}
cout<<ans.size()<<'\n';
for(int i:ans)cout<<i<<' ';
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |