Submission #978022

#TimeUsernameProblemLanguageResultExecution timeMemory
978022AlphaMale06Network (BOI15_net)C++17
100 / 100
374 ms59632 KiB
#include <bits/stdc++.h> using namespace std; #define pb push_back const int N = 5e5+3; int deg[N]; vector<int> g[N]; vector<int> ls; void dfs(int v, int p){ if(g[v].size()==1)ls.pb(v); for(int to : g[v]){ if(to!=p)dfs(to, v); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for(int i=1; i<n; i++){ int a, b; cin >> a >> b; g[a].pb(b); g[b].pb(a); deg[a]++; deg[b]++; } int root=1; for(int i=2; i<=n; i++){ if(deg[i]>1)root=i; } dfs(root, root); int cnt=ls.size(); cout << (cnt+1)/2 << '\n'; for(int i=0; i<cnt/2; i++){ cout << ls[i] << " " << ls[i+cnt/2] << '\n'; } if(cnt&1)cout << ls[0] << " " << ls.back() << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...