Submission #821955

#TimeUsernameProblemLanguageResultExecution timeMemory
821955exodus_Network (BOI15_net)C++14
0 / 100
8 ms12092 KiB
#include<bits/stdc++.h> #define fi first #define se second using namespace std; const int nmax = 500005; vector<int>adj[nmax]; bool leaf[nmax]={0}; bool vis[nmax]={0}; vector<pair<int,int>>dist; vector<pair<int,int>>daft; void dfs(int x) { stack<pair<int,int>>st; st.push({x,1}); while(!st.empty()) { int nod = st.top().fi; int dis = st.top().se; vis[nod]=true; if(leaf[nod]==true) { dist.push_back({dis,nod}); } st.pop(); for(auto it:adj[nod]) { if(vis[it]==false) { st.push({it, dis+1}); } } } return; } int main() { int n,a,b; cin >> n; for(int i=1; i<n; i++) { cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } int random = -1; for(int i=1; i<=n; i++) { if(adj[i].size()==1) { leaf[i]=true; if(random==-1) random = i; } } dfs(random); sort(dist.begin(), dist.end()); int mid = dist.size()/2; cout << (dist.size()+1)/2 << endl; for(int i=0; i<mid; i++) { cout << dist[i].se << " " << dist[i+mid].se << endl; } if(dist.size()%2==1) { cout << dist[0].se << " " << dist[dist.size()-1].se << endl; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...