Submission #311451

#TimeUsernameProblemLanguageResultExecution timeMemory
311451sofapudenNetwork (BOI15_net)C++14
100 / 100
1056 ms60012 KiB
#include <bits/stdc++.h> using namespace std; vector<vector<int>> v; vector<int> o; vector<int> lea; void dfs(int x, int p){ for(int y : v[x]){ if(y == p)continue; dfs(y,x); } o.push_back(x); } int main(){ int n; cin >> n; v.resize(n); for(int i = 0; i < n-1; ++i){ int a, b; cin >> a >> b; a--, b--; v[a].push_back(b); v[b].push_back(a); } int root = 0; for(int i = 0; i < n; ++i){ if(v[i].size() >= 2)root = i; } dfs(root,root); for(int x : o){ if(v[x].size() == 1)lea.push_back(x); } int ln = lea.size(); cout << (ln+1)/2 << "\n"; for(int i = 0; i < ln/2; ++i){ cout << lea[i]+1 << " " << lea[(ln+1)/2+i]+1 << "\n"; } if(ln&1){ cout << root+1 << " " << lea[ln/2]+1 << "\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...