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;
int x;
vector<int>v[500005];
vector<pair<int,int>>ans;
set<pair<int,int>> dfs(int i,int aa,int c,int p=-1){
set<pair<int,int>>ret;
if(v[i].size()==1)ret.insert({c,i});
for(auto j:v[i]){
if(j==p)continue;
set<pair<int,int>>lol=dfs(j,aa,c+1,i);
for(auto k:lol)ret.insert({k.first,k.second});
}
if(i==aa){
vector<int>vv;
for(auto [j,r]:ret)vv.push_back(r);
for(int j=0;j<vv.size()/2;j++)ans.push_back({vv[j],vv[(int)vv.size()-j-1]});
if((int)vv.size()%2){
int m1=vv[vv.size()/2];
ans.push_back({m1,vv.back()});
}
}
return ret;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
cin>>x;
for(int i=1;i<=x-1;i++){
int l,r;cin>>l>>r;
v[l].push_back(r);
v[r].push_back(l);
}
for(int i=1;i<=x;i++)if((int)v[i].size()==1){dfs(i,i,0,i);break;}
cout<<ans.size()<<"\n";
for(auto [l,r]:ans)cout<<l<<" "<<r<<endl;
return 0;
}
Compilation message (stderr)
net.cpp: In function 'std::set<std::pair<int, int> > dfs(int, int, int, int)':
net.cpp:17:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for(int j=0;j<vv.size()/2;j++)ans.push_back({vv[j],vv[(int)vv.size()-j-1]});
| ~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |