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 gibon ios::sync_with_stdio(false); cin.tie(0);
#define fir first
#define sec second
#define pii pair<int, int>
#define pll pair<ll, ll>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
using namespace std;
int dx[4]={0, 1, 0, -1}, dy[4]={1, 0, -1 , 0};
const int mxN=1005;
const int mxM=1010;
const int mxK=105;
const int MOD=1000000007;
const ll INF=1e15;
const ll P1=1000000007;
const ll P2=1000000009;
int N, M;
vector <int> v[mxN], child[mxN];
int dis[mxN];
void dfs0(int now, int pre)
{
for(int nxt : v[now]) if(nxt!=pre)
{
dis[nxt]=dis[now]+1;
dfs0(nxt, now);
}
}
int sub[mxN];
bool ok=true;
void dfs1(int now, int pre)
{
int cnt2=0;
for(int nxt : v[now]) if(nxt!=pre)
{
dis[nxt]=dis[now]+1;
child[now].push_back(nxt);
dfs1(nxt, now);
sub[now]=max(sub[nxt]+1, sub[now]);
if(sub[nxt]>=2) cnt2++;
}
if(cnt2>=2) ok=false;
}
vector <int> ans;
int ncnt;
bool dfs2(int now)
{
ncnt--;
ans.push_back(now);
if(ncnt==0) return false;
sort(child[now].begin(), child[now].end(), [](int a, int b){return sub[a]<sub[b];});
for(int nxt : child[now])
{
if(sub[nxt]==1)
{
if(!dfs2(nxt)) return false;
ans.push_back(now);
}
if(sub[nxt]>=2) if(dfs2(nxt)) return false;
}
return true;
}
int main()
{
gibon
cin >> N >> M;
for(int i=0;i<M;i++)
{
int a, b; cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
if(M>=N)
{
cout << "NO";
return 0;
}
if(N==1)
{
cout << "YES\n1\n1";
return 0;
}
if(N==2)
{
cout << "YES\n2\n1 1";
return 0;
}
dfs0(1, -1);
int far=1;
for(int i=2;i<=N;i++) if(dis[far]<dis[i]) far=i;
for(int i=1;i<=N;i++) dis[i]=0;
dfs1(far, -1);
if(!ok)
{
cout << "NO";
return 0;
}
for(int i=1;i<=N;i++) if(v[i].size()>=2) ncnt++;
dfs2(child[far][0]);
cout << "YES\n";
cout << 2*ans.size() << '\n';
for(int i=0;i<ans.size();i++) cout << ans[i] << " ";
for(int i=ans.size()-1;i>=0;i--) cout << ans[i] << " ";
}
Compilation message (stderr)
newspapers.cpp: In function 'int main()':
newspapers.cpp:104:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for(int i=0;i<ans.size();i++) cout << ans[i] << " ";
| ~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |