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;
typedef long long ll;
namespace Naive{
int n, m;
int dissappear[25];
vector<int> link[25];
int movemove(int source){
int rv = 0;
for(int i=0; i<n; i++){
if(source & (1<<i)){
for(int j=0; j<(int)link[i].size(); j++){
rv |= (1<<link[i][j]);
}
}
}
// printf("movemove %d is %d\n", source, rv);
return rv;
}
void bfs(){
int temp;
map<int, bool> visited;
map<int, pair<int, int> > parent;
queue<int> que;
stack<int> ans;
que.push((1<<n)-1);
visited[(1<<n)-1] = true;
while(1){
if(que.empty()){
printf("NO\n");
return;
}
temp = que.front();
// printf("bfs %d\n", temp);
que.pop();
if(!temp) break;
for(int i=0; i<n; i++){
if(!(temp & (1<<i))) continue;
int tmp_val = movemove(temp - (1<<i));
if(!visited[tmp_val]){
que.push(tmp_val);
parent[tmp_val] = make_pair(temp, i);
// printf("parent of %d is %d(deleted %d)\n", tmp_val, temp, i);
visited[tmp_val] = true;
}
}
}
int cnt = 0, mark = 0;
while(mark != (1<<n)-1){
cnt++;
ans.push(parent[mark].second);
mark = parent[mark].first;
}
printf("YES\n%d\n", cnt);
for(int i=0; i<cnt; i++){
printf("%d ", ans.top()+1);
ans.pop();
}
puts("");
}
void main(){
int s, e;
if(m!=n-1){
puts("NO");
exit(0);
}
for(int i=0; i<n; i++) link[i].clear();
for(int i=1; i<=m; i++){
scanf("%d %d", &s, &e);
s--, e--;
link[s].push_back(e), link[e].push_back(s);
}
for(int i=0; i<n; i++){
dissappear[i] = 0;
for(int j=0; j<(int)link[i].size(); j++){
if((int)link[link[i][j]].size() == 1) dissappear[i] |= (1<<link[i][j]);
}
}
bfs();
}
}
int n, m;
vector<int> link[1002];
int maxDepth, maxIdx;
int p[1002];
int dfs(int x, int par = -1, int depth = 0){
vector<int> vec (1, 0);
p[x] = par;
if(maxDepth < depth) maxIdx = x, maxDepth = depth;
for(auto y: link[x]){
if(y==par) continue;
int tmp = dfs(y, x, depth+1);
vec.push_back(tmp+1);
}
sort(vec.rbegin(), vec.rend());
if(par == -1){
if(vec.size() >= 3 && vec[2] >= 3){
puts("NO");
exit(0);
}
}
return vec[0];
}
int depth[1002];
vector<int> line;
vector<vector<int> > lineChild;
vector<vector<int> > lineChildCnt;
vector<int> ans;
int main(){
scanf("%d %d", &n, &m);
if(n<=10){
Naive::n = n;
Naive::m = m;
Naive::main();
return 0;
}
if(m!=n-1){
puts("NO");
return 0;
}
for(int i=1; i<n; i++){
int x, y;
scanf("%d %d", &x, &y);
link[x].push_back(y), link[y].push_back(x);
}
for(int i=1; i<=n; i++){
dfs(i);
}
maxDepth = 0; dfs(1);
int A = maxIdx;
maxDepth = 0; dfs(A);
int B = maxIdx;
{
int x = B;
while(x != -1){
line.push_back(x);
depth[x] = 1;
x = p[x];
}
reverse(line.begin(), line.end());
}
lineChild.resize(line.size());
lineChildCnt.resize(line.size());
for(int i=1; i<=n; i++){
if(depth[i] == 1) continue;
if(depth[p[i]] == 1){
depth[i] = 2;
int idx = find(line.begin(), line.end(), p[i]) - line.begin();
lineChild[idx].push_back(i);
lineChildCnt[idx].push_back(0);
}
}
for(int i=1; i<=n; i++){
if(depth[i] == 1 || depth[i] == 2) continue;
if(depth[p[i]] == 2){
depth[i] = 3;
int idx = find(line.begin(), line.end(), p[p[i]]) - line.begin();
lineChildCnt[idx][find(lineChild[idx].begin(), lineChild[idx].end(), p[i]) - lineChild[idx].begin()]++;
}
}
for(int i=1; i<(int)line.size()-1; i++){
ans.push_back(line[i]);
for(int j=0; j<(int)lineChild[i].size(); j++){
if(lineChildCnt[i][j]) ans.push_back(lineChild[i][j]), ans.push_back(line[i]);
}
}
if(count(ans.begin(), ans.end(), ans.back()) >= 2) ans.pop_back();
if(count(ans.begin(), ans.end(), ans[0]) >= 2) ans.erase(ans.begin());
printf("YES\n%d\n", (int)ans.size()*2);
for(auto x: ans) printf("%d ", x);
if((int)ans.size() % 2 == 0){
reverse(ans.begin(), ans.end());
}
for(auto x: ans) printf("%d ", x);
}
Compilation message (stderr)
newspapers.cpp: In function 'void Naive::main()':
newspapers.cpp:75:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | scanf("%d %d", &s, &e);
| ~~~~~^~~~~~~~~~~~~~~~~
newspapers.cpp: In function 'int main()':
newspapers.cpp:121:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
121 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
newspapers.cpp:136:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
136 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |