• 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
    ",max(t.f[1][0],t.f[1][1]));
    56     return 0;
    57 }
    View Code
  • 相关阅读:
    redis学习
    Ubuntu命令大全
    关于jquery中attr和prop的用法
    Ubuntu下修改为永久DNS的方法
    Yii2 behaviors中verbs access的一些理解
    vue_ form表单 v-model
    vue-one_demo_music
    ES6
    VUE 入门 01
    Django model.py表单设置默认值允许为空
  • 原文地址:https://www.cnblogs.com/Blue233333/p/7240978.html
Copyright © 2020-2023  润新知