Submission #923267

#TimeUsernameProblemLanguageResultExecution timeMemory
923267AiperiiiNetwork (BOI15_net)C++14
0 / 100
4 ms13872 KiB
#include <bits/stdc++.h> #define int long long #define ff first #define ss second #define pb push_back #define all(x) x.begin(),x.end() using namespace std; const int N=5e5+5; vector <int> g[N]; int d[N]; void dfs(int v,int p){ for(auto to : g[v]){ if(to!=p){ d[to]=d[v]+1; dfs(to,v); } } } signed main(){ ios_base::sync_with_stdio(); cin.tie(0);cout.tie(0); int n;cin>>n; for(int i=0;i<n-1;i++){ int u,v; cin>>u>>v; g[u].pb(v); g[v].pb(u); } int root=1; for(int i=1;i<=n;i++){ if(g[i].size()==1){ root=i; } } //cout<<root<<"\n"; dfs(root,0); vector <pair <int,int> > v; for(int i=1;i<=n;i++){ if(g[i].size()==1 && i!=root)v.pb({d[i],i}); } sort(all(v)); //cout<<v.size()<<"\n"; cout<<v.size()/2+1<<"\n"; for(int i=0;i<v.size()-1;i+=2){ cout<<v[i].ss<<" "<<v[i+1].ss<<"\n"; } cout<<v.back().ss<<" "<<root<<"\n"; }

Compilation message (stderr)

net.cpp: In function 'int main()':
net.cpp:44:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |    for(int i=0;i<v.size()-1;i+=2){
      |                ~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...