This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//In the name of God
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
const ll maxn = 2e6 + 100;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r+", stdin);freopen("output.txt", "w+", stdout);
#define pb push_back
#define Mp make_pair
#define F first
#define S second
#define Sz(x) ll((x).size())
#define all(x) (x).begin(), (x).end()
ll pw(ll a, ll b){
ll c = 1;
while(b){
if(b & 1) c = c * a % mod;
a = a * a % mod;
b >>= 1;
}
return c;
}
int n, m;
namespace sub1{
int g[maxn], d[maxn], p[maxn], pp[maxn];
bool vis[maxn];
queue<int> q;
int get(int x){
int y = 0;
for(int i = 0; i < n; i++){
if((x >> i) & 1) y |= g[i];
}
return y;
}
bool check(vector<int> vec){
int v = (1 << n) - 1;
for(int i : vec){
v = get(v & (~(1 << i)));
cout << i << " ! ";
for(int j = 0; j < n; j++){
if((v >> j) & 1) cout << j << " ";
else cout << " ";
}
cout << "\n";
}
return (v == 0);
}
void solve(){
for(int i = 0; i < m; i++){
int v, u;
cin >> v >> u;
v--;
u--;
g[v] |= (1 << u);
g[u] |= (1 << v);
}
p[(1 << n) - 1] = -1;
vis[(1 << n) - 1] = 1;
q.push((1 << n) - 1);
while(!q.empty()){
int v = q.front();
q.pop();
for(int i = 0; i < n; i++){
int u = get(v & (~(1 << i)));
if(!vis[u]){
vis[u] = 1;
d[u] = d[v] + 1;
p[u] = v;
pp[u] = i;
q.push(u);
}
}
}
if(!vis[0]){
cout << "NO\n";
return;
}
cout << "YES\n";
cout << d[0] << "\n";
int v = 0;
vector<int> vec;
while(p[v] != -1){
vec.pb(pp[v]);
v = p[v];
}
reverse(all(vec));
for(int i : vec){
cout << i + 1 << " ";
}
return;
}
};
vector<int> gg[maxn], g[maxn], vec;
bool vvv;
void dfs(int v, int p = -1){
if(vvv) vec.pb(v);
vvv = 1;
for(int u : g[v]){
if(Sz(g[u]) == 1){
vec.pb(u);
vec.pb(v);
}
}
for(int u : g[v]){
if(Sz(g[u]) != 1 && u != p) dfs(u, v);
}
return;
}
int main(){
fast_io;
cin >> n >> m;
if(m != n - 1){
cout << "NO\n";
return 0;
}
if(n <= 20){
sub1::solve();
return 0;
}
for(int i = 0; i < m; i++){
int v, u;
cin >> v >> u;
gg[v].pb(u);
gg[u].pb(v);
}
vector<int> ve2;
for(int i = 1; i <= n; i++){
if(Sz(gg[i]) == 1) continue;
ve2.pb(i);
for(int j : gg[i]){
if(Sz(gg[j]) != 1) g[i].pb(j);
}
}
if(Sz(ve2) == 1){
cout << "YES\n2\n" << ve2[0] << " " << ve2[0] << "\n";
return 0;
}
if(Sz(ve2) == 2){
cout << "YES\n2\n" << ve2[0] << " " << ve2[1] << " " << ve2[1] << " " << ve2[0] << "\n";
return 0;
}
int v = -1;
for(int i = 1; i <= n; i++){
if(Sz(g[i]) < 2) continue;
int c = 0;
for(int j : g[i]){
if(Sz(g[j]) != 1) c++;
}
if(c > 2){
cout << "NO\n";
return 0;
}
if(c != 2) v = i;
}
dfs(v);
vec.pop_back();
vector<int> v2 = vec;
reverse(all(v2));
for(int i : v2){
vec.pb(i);
}
cout << "YES\n";
cout << Sz(vec) << "\n";
for(int i : vec){
cout << i << " ";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |