BZOJ2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛

n<=50000个点的树,求选最多不相邻点的个数。

f[i][0]=sigma max(f[j][0],f[j][1]),j为i的儿子

f[i][1]=sigma f[j][0],j同上

死于未初始化。不要歧视水题。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<cstdlib>
 5 #include<queue>
 6 //#include<iostream>
 7 using namespace std;
 8 
 9 int n;
10 #define maxn 50011
11 struct Edge{int to,next;};
12 struct Tree
13 {
14     Edge edge[maxn*2];int le;
15     int first[maxn],f[maxn][2];
16     Tree()//初始化。
17     {
18         memset(first,0,sizeof(first));
19         le=2;
20     }
21     void add_edge(int x,int y)
22     {
23         edge[le].to=y;
24         edge[le].next=first[x];
25         first[x]=le++;
26     }
27     void insert(int x,int y)
28     {
29         add_edge(x,y);
30         add_edge(y,x);
31     }
32     void dp(int x,int fa)
33     {
34         f[x][0]=0;f[x][1]=1;
35         for (int i=first[x];i;i=edge[i].next)
36         {
37             const int now=edge[i].to;
38             if (now==fa) continue;
39             dp(now,x);
40             f[x][1]+=f[now][0];
41             f[x][0]+=max(f[now][1],f[now][0]);
42         }
43     }
44 }t;
45 int x,y;
46 int main()
47 {
48     scanf("%d",&n);
49     for (int i=1;i<n;i++)
50     {
51         scanf("%d%d",&x,&y);
52         t.insert(x,y);
53     }
54     t.dp(1,0);
55     printf("%d\n",max(t.f[1][0],t.f[1][1]));
56     return 0;
57 }
View Code

 

posted @ 2017-07-26 17:39  Blue233333  阅读(186)  评论(0编辑  收藏  举报