이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define maxn 500050
vector<vector<int> > adj(maxn);
vector<int> leaf;
void dfs(int node,int parent){
if(adj[node].size()==1){
leaf.push_back(node);
return;
}
for(auto k:adj[node]){
if(k==parent){
continue;
}
dfs(k,node);
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int no_of_input;
int input1,input2;
cin >> no_of_input;
for(int i=0;i<no_of_input-1;i++){
cin >> input1 >> input2;
adj[input1].push_back(input2);
adj[input2].push_back(input1);
}
if(no_of_input==2){
assert(0==1);
cout << 1 << "\n";
cout << "1 2";
return 0;
}
int root = -1;
for(int i=1;i<=no_of_input;i++){
if(adj[i].size()>=2){
root = i;
break;
}
}
dfs(root,-1);
vector<pair<int,int> > ans;
for(int i=0;i<leaf.size()/2;i++){
ans.push_back({leaf[i],leaf[leaf.size()-i-1]});
}
if(leaf.size()%2==1){
ans.push_back({leaf[leaf.size()/2],leaf[0]});
}
cout << ans.size() << "\n";
for(auto k:ans){
cout << k.first << " " << k.second << "\n";
}
}
컴파일 시 표준 에러 (stderr) 메시지
net.cpp: In function 'int32_t main()':
net.cpp:44:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<leaf.size()/2;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... |