### Code

#include <bits/stdc++.h>
#define maxn 500100
#define max(a,b) (a>b?a:b)
#define pb push_back
using namespace std;
vector<int> s[maxn];
priority_queue<pair<int,int> >q;
int siz[maxn],lea[maxn],mx[maxn];
int ans,con[maxn][2];
int x=0,flag=1;char ch=getchar();
while(!isdigit(ch)&&ch!='-') ch=getchar();
if (ch=='-') flag=-1,ch=getchar();
while(isdigit(ch)) x=(x<<3)+(x<<1)+(ch-'0'),ch=getchar();
return x*flag;
}
struct Edge{
int to,next;
Edge(int a=0,int b=0){
to=a,next=b;
}
void wipe(){
to=next=0;
}
}l[maxn<<1];
}
bool Compare(vector<int> a,vector<int> b){
return a.size()>b.size();
}
void Pre(int u,int f){
int v=l[i].to;
if (v==f) continue;
Pre(v,u);siz[u]+=siz[v];
mx[u]=max(mx[u],siz[v]);
}
}
void Dfs(int u,int f){
if (lea[u]) s[son].pb(u);
if (l[i].to!=f) Dfs(l[i].to,u);
}
void New(int a,int b){
con[++ans][0]=a,
con[ans][1]=b;
}
void Update(int u){
s[u].pop_back();
if (!s[u].empty())
q.push(make_pair(s[u].size(),u));
}
void Solve(){
for (int i=1;i<=n;++i)
for (int i=1;i<=n;++i)
l[i].wipe();
for (int i=1;i<=son;++i)
s[i].clear();

for (int i=1,a,b;i<n;++i)
deg[a]++,deg[b]++;
for (int i=1;i<=n;++i)
if (deg[i]==1) lea[i]=1,siz[i]++;
Pre(1,0);
int rt=1;
for (int i=2;i<=n;++i){
mx[i]=max(mx[i],siz[1]-siz[i]);
if (mx[i]<mx[rt]) rt=i;
}
son++,Dfs(l[i].to,rt);
sort(s+1,s+1+son,Compare);
int fst=s[1].back();
q.push(make_pair(s[1].size(),1));
for (int i=2;i<=son;++i){
if (s[i].empty()) continue;
if (q.empty()) New(s[i].back(),fst);
else{
int cur=q.top().second;q.pop();
New(s[cur].back(),s[i].back());
Update(cur);
}
Update(i);
}
while(!q.empty()){
int a=q.top().second;q.pop();
if (q.empty()){New(s[a].back(),fst);break;}
int b=q.top().second;q.pop();
New(s[a].back(),s[b].back());
Update(a),Update(b);
}
printf("%d\n",ans);
for (int i=1;i<=ans;++i)
printf("%d %d\n",con[i][0],con[i][1]);
}
int main(){
}