zoukankan      html  css  js  c++  java
  • poj 2342 Anniversary party (树形dp入门)

    http://poj.org/problem?id=2342

    树形dp:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<vector>
     5 using namespace std;
     6 
     7 const int mx=6006;
     8 int dp[mx][2];
     9 vector<int>g[mx];
    10 int n;
    11 
    12 void dfs(int x)
    13 {
    14    for (int i=0;i<g[x].size();i++)
    15    {
    16        int j=g[x][i];
    17        dfs(j);
    18        dp[x][1]+=dp[j][0];
    19        dp[x][0]+=max(dp[j][1],dp[j][0]);
    20    }
    21 }
    22 
    23 int main()
    24 {
    25     while (~scanf("%d",&n))
    26     {
    27         if (!n)
    28         {
    29             scanf("%d",&n);
    30             if (!n) return 0;
    31         }
    32         memset(dp,0,sizeof(dp));
    33         for (int i=1;i<=n;i++) scanf("%d",&dp[i][1]),g[i].clear();
    34         int l,k,root=1;
    35         for (int i=1;i<n;i++)
    36         {
    37             scanf("%d%d",&l,&k);
    38             g[k].push_back(l);
    39             if (root==l) root=k;
    40         }
    41         dfs(root);
    42         printf("%d
    ",max(dp[root][1],dp[root][0]));
    43     }
    44     return 0;
    45 }
  • 相关阅读:
    网络流24题题解
    NOIP2018游记
    AGC016题解
    雅礼集训总结
    数学相关【真·NOIP】
    NOIP2018系列
    洛咕P4180 严格次小生成树
    矩阵乘法学习笔记
    一些比较神奇的思路
    点分治复习记
  • 原文地址:https://www.cnblogs.com/pblr/p/5253816.html
Copyright © 2011-2022 走看看