#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
vi graph[1001];
int dist[1001];
bool is_dim[1001];
int p1,p2;
vi dim;
vi ls;
void dfs_dim(int v, int pop, int d)
{
if(!is_dim[v]) d++;
dist[v] = d;
forall(it,graph[v])
{
if(it != pop)
{
dfs_dim(it,v,d);
}
}
}
void find_dim(int v, int pop)
{
ls.pb(v);
if(v == p2) dim = ls;
forall(it,graph[v])
{
if(it != pop)
{
find_dim(it,v);
}
}
ls.pop_back();
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//random_start();
int n,m;
cin >> n >> m;
if(n == 1)
{
cout << "YES\n1\n1";
return 0;
}
if(n == 2)
{
cout << "YES\n2\n1 1";
return 0;
}
if(n != m+1)
{
cout << "NO\n";
return 0;
}
rep(i,m)
{
int a,b;
cin >> a >> b;
graph[a].pb(b);
graph[b].pb(a);
}
dfs_dim(1,1,1);
pii d1 = {0,0};
rep2(i,1,n) d1 = max(d1,{dist[i],i});
p1 = d1.ss;
dfs_dim(p1,p1,1);
d1 = {0,0};
rep2(i,1,n) d1 = max(d1,{dist[i],i});
p2 = d1.ss;
find_dim(p1,p1);
forall(it,dim) is_dim[it] = 1;
dfs_dim(p1,p1,0);
rep2(i,1,n)
{
if(dist[i] > 2)
{
cout << "NO\n";
return 0;
}
}
int prev = dim[0];
vi ans;
rep2(i,1,siz(dim)-2)
{
ans.pb(dim[i]);
forall(it,graph[dim[i]])
{
if(dim[it]) continue;
ans.pb(it);
ans.pb(dim[i]);
}
}
cout << "YES\n" << siz(ans) * 2<< "\n";
forall(it,ans) cout << it << " ";
reverse(all(ans));
forall(it,ans) cout << it << " ";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |