思路: 每个点有三种状态,本身有塔,被子节点的塔覆盖,被父节点的塔覆盖。
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Maxn 10100
#define Maxm 100010
#define LL __int64
#define Abs(x) ((x)>0?(x):(-x))
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define inf 1000000
#define Mod 1000000007
using namespace std;
int dp[Maxn][],head[Maxn],vi[Maxn],e;
struct Edge{
int u,v,val,next;
}edge[Maxn*];
void init()
{
memset(dp,,sizeof(dp));
memset(head,-,sizeof(head));
memset(vi,,sizeof(vi));
e=;
}
void add(int u,int v)
{
edge[e].u=u,edge[e].v=v,edge[e].next=head[u],head[u]=e++;
edge[e].u=v,edge[e].v=u,edge[e].next=head[v],head[v]=e++;
}
inline int min(int a,int b,int c)
{
a=a<b?a:b;
return a<c?a:c;
}
void dfs(int u)
{
int i,v;
vi[u]=;
dp[u][]=;//有信息塔
dp[u][]=;//由前覆盖
dp[u][]=;//由后覆盖
int sum=,f=,min1=inf;
for(i=head[u];i!=-;i=edge[i].next){
v=edge[i].v;
if(vi[v]) continue;
dfs(v);
dp[u][]+=min(dp[v][],dp[v][],dp[v][]);
dp[u][]+=min(dp[v][],dp[v][]);
if(dp[v][]<=dp[v][]){
sum+=dp[v][];
f=;
}
else{
if(dp[v][]-dp[v][]<min1){
min1=dp[v][]-dp[v][];
}
sum+=dp[v][];
}
}
if(f) dp[u][]=sum;
else dp[u][]=sum+min1;
}
int main()
{
int n,i,j,u,v;
while(scanf("%d",&n)!=EOF){
init();
for(i=;i<n;i++){
scanf("%d%d",&u,&v);
add(u,v);
}
dfs();
printf("%d\n",min(dp[][],dp[][]));
}
return ;
}