给你一棵树,求剪掉一些边,再把它拼到树上,使树变成一条链
Sample input
6
1 3
3 2
3 4
4 5
4 6
Sample output
2
#include <cstdio>
#include <iostream>
#define N_MAX 300005
int n,ans,du[N_MAX];
struct Edge{
int node;
int next;
Edge(int node = 0,int next = 0) : node(node),next(next){}
}edge[N_MAX << 1];
int head[N_MAX],tot = 1;
void add(int u,int v){
edge[++tot] = Edge(v,head[u]);
head[u] = tot;
}
int main(int argc,char *argv[]){
scanf("%d",&n);
for(int i = 1;i < n;i++){
int u,v;
scanf("%d %d",&u,&v);
add(u,v);add(v,u);
du[u]++;du[v]++;
}
for(int i = 1;i <= n;i++){
if(du[i]>2) ans += (du[i] - 2);
//printf("i = %d size = %d\n",i,size[i]);
}
printf("%d\n",ans);
return 0;
}