제출 #532850

#제출 시각아이디문제언어결과실행 시간메모리
53285079brueNewspapers (CEOI21_newspapers)C++14
0 / 100
1 ms324 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; 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(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); }

컴파일 시 표준 에러 (stderr) 메시지

newspapers.cpp: In function 'int main()':
newspapers.cpp:39:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
newspapers.cpp:47:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |         scanf("%d %d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...