zoukankan      html  css  js  c++  java
  • hdu 1520 Anniversary party

    http://acm.hdu.edu.cn/showproblem.php?pid=1520

    类型:树dp简单题

    AC Code
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 using namespace std;
     5 #define maxn 6005
     6 struct node{
     7     int y,next;
     8 }ee[maxn<<1];
     9 int link[maxn],t;
    10 int f[maxn],g[maxn],val[maxn];
    11 void maketree(int a,int b)
    12 {
    13         ee[++t].y=a;
    14         ee[t].next=link[b];
    15         link[b]=t;
    16 }
    17 void dfs(int root,int father)
    18 {
    19     for(int i=link[root];i;i=ee[i].next){
    20         if(ee[i].y!=father){
    21         dfs(ee[i].y,root);
    22         f[root]+=g[ee[i].y];
    23         g[root]+=max(g[ee[i].y],f[ee[i].y]);
    24         }
    25     }
    26     f[root]+=val[root];
    27 }
    28 int main()
    29 {
    30     int n,a,b;
    31     while(~scanf("%d",&n)){
    32         t=0;
    33         for(int i=1;i<=n;i++)
    34         scanf("%d",val+i);
    35         memset(link,0,sizeof(link));
    36         while(scanf("%d%d",&a,&b)&&(a||b)){
    37         maketree(a,b);
    38         maketree(b,a);
    39         }
    40         memset(f,0,sizeof(f));
    41         memset(g,0,sizeof(g));
    42         dfs(1,1);
    43         printf("%d\n",max(f[1],g[1]));
    44     }
    45     return 0;
    46 }
  • 相关阅读:
    Careercup
    Careercup
    Careercup
    Careercup
    Careercup
    Careercup
    Careercup
    Careercup
    Careercup
    Careercup
  • 原文地址:https://www.cnblogs.com/kim888168/p/3011143.html
Copyright © 2011-2022 走看看