# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
106883 | Pajaraja | Network (BOI15_net) | C++17 | 0 ms | 0 KiB |
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 cnt,nes;
vector<int> g[500007],d[500007];
vector<bool> v[500007];
int k[500007];
void dfs(int s,int f)
{
if(g[s].size()==1)
{
k[cnt++]=s;
return;
}
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) dfs(g[s][i],s);
}
int main()
{
int n,v1,v2,l=0;
nes=0;
fill(k,k+200007,0);
scanf("%d",&n);
for(int i=0;i<n-1;i++)
{
scanf("%d%d",&v1,&v2);
g[v1].push_back(v2);
g[v2].push_back(v1);
}
for(int i=1;i<=n;i++) if(g[i].size()==1) l++;
printf("%d\n",(l+1)/2);
for(int i=1;i<=n;i++) if(g[i].size()>1)
{
dfs(i,0);
for(int i=0;i<l/2;i++) printf("%d %d\n",k[i],k[i+l/2]);
if(l%2==0) printf("%d %d",s,k[l-1]);
return 0;
}
}