This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int ukup=0;
int leafs=0;
int leafs1=0;
vector <int> graf[500005];
bool visited[500005];
int listovi[500005];
int brlist[500005];
void dfs1(int v){
visited[v] = 1;
bool nes = 0;
for(auto c : graf[v]){
if(!visited[c]){dfs1(c); nes=1; brlist[v] += brlist[c];}
}
if(nes == 0) {leafs++; brlist[v] = 1;}
}
void dfs3(int v){
visited[v] = 1;
bool nes = 0;
for(auto c : graf[v]){
if(!visited[c]){dfs3(c); nes=1;}
}
if(nes == 0){leafs++; listovi[leafs] = v;}
}
int main(){
ios_base::sync_with_stdio(false),cin.tie(0),cerr.tie(0),cout.tie(0);
cout.precision(10);
cout<<fixed;
int n;
cin >> n;
for(int i=1; i<n; i++){
int a, b;
cin >> a >> b;
graf[a].push_back(b);
graf[b].push_back(a);
}
if(n == 2){cout << "1\n 1 2"; return 0;}
dfs1(1);
int x = 1;
for(auto c : graf[1]){
if(brlist[c] > (brlist[1]/2)){
x = c;
int par;
par = 1;
while(1){
if(graf[x].size() != 2) break;
for(auto nesto : graf[x]){
if(nesto != par){par = x; x = nesto; break;}
}
}
break;
}
}
leafs = 0;
for(int i=1; i<=n; i++) visited[i] = 0;
dfs3(x);
int r = 1;
int br=0;
while(r+(leafs/2) <= leafs){
br++;
r++;
}
r = 1;
cout << br << "\n";
while(r+(leafs/2) <= leafs){
cout << listovi[r] << " " << listovi[r+(leafs/2)] << "\n";
r++;
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |