Submission #614159

#TimeUsernameProblemLanguageResultExecution timeMemory
614159sword060Network (BOI15_net)C++17
100 / 100
596 ms69416 KiB
#include <bits/stdc++.h> using namespace std; int x; vector<int>v[500005]; vector<pair<int,int>>ans; vector<int> dfs(int i,int p=-1){ vector<int>ret; if(v[i].size()==1)ret.push_back(i); for(auto j:v[i]){ if(j==p)continue; vector<int>lol=dfs(j,i); if(lol.size()>ret.size())swap(lol,ret); while(ret.size()&&ret.size()+lol.size()>2){ int c1=ret.back(),c2=lol.back(); ret.pop_back();lol.pop_back(); ans.push_back({c1,c2}); } for(auto k:lol)ret.push_back(k); } if(p==-1){ for(int j=0;j<ret.size()/2;j++) ans.push_back({ret[j],ret[ret.size()-j-1]}); if((int)ret.size()%2)ans.push_back({ret[ret.size()/2],i}); } 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(v[i].size()>1){dfs(1);break;} cout<<ans.size()<<"\n"; for(auto [l,r]:ans)cout<<l<<" "<<r<<endl; return 0; }

Compilation message (stderr)

net.cpp: In function 'std::vector<int> dfs(int, int)':
net.cpp:21:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |     for(int j=0;j<ret.size()/2;j++)
      |                 ~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...