题目链接:bzoj上是权限题~
题目大意:
题解:
树型DP
f[i][0]表示不选i,f[i][1]表示选i。(因为很水..就不详写了)
可列出方程:
f[i][0]=sigama(max(f[y][1],f[y][0]));
f[i][1]=sigama(f[y][0]); y为i的孩子
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 51000struct node
{int x,y,next;
}a[maxn*2];int len,first[maxn];
int f[maxn][2];//0-不选自己 1-选
int mymax(int x,int y){return (x>y)?x:y;}
void ins(int x,int y)
{len++;a[len].x=x;a[len].y=y;a[len].next=first[x];first[x]=len;
}
void dp(int x,int fa)
{int f0=0,f1=0;for (int k=first[x];k!=-1;k=a[k].next){int y=a[k].y;if (y==fa) continue;dp(y,x);f1+=f[y][0];f0+=mymax(f[y][0],f[y][1]);}f[x][0]=f0;f[x][1]=1+f1;
}
int main()
{//freopen("vacation.in","r",stdin);//freopen("vacation.out","w",stdout);int n,i,x,y;scanf("%d",&n);len=0;memset(first,-1,sizeof(first));for(i=1;i<n;i++){scanf("%d%d",&x,&y);ins(x,y);ins(y,x);}dp(1,0);printf("%d\n",mymax(f[1][0],f[1][1]));return 0;
}