zoukankan      html  css  js  c++  java
  • 树形dp

    1 HDU 1520 Anniversary party

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <algorithm>
     6 #include <queue>
     7 #include <vector>
     8 #include <stack>
     9 #include <map>
    10 #include <set>
    11 #include <cmath>
    12 #include <cctype>
    13 #include <ctime>
    14 #include <bitset>
    15 
    16 using namespace std;
    17 
    18 #define REP(i, n) for (int i = 0; i < (n); ++i)
    19 #define eps 1e-9
    20 
    21 typedef long long ll;
    22 typedef pair<int, int> pii;
    23 
    24 const int INF = 0x7fffffff;
    25 const int maxn = 1e5;
    26 int n, edge;
    27 int head[maxn], to[maxn], Next[maxn], dp[2][maxn], in[maxn];
    28 
    29 inline void init() { edge = 0; memset(head, -1, sizeof(head)); }
    30 inline void add_edge(int u, int v) {
    31     to[edge] = v; Next[edge] = head[u]; head[u] = edge++;
    32 }
    33 void dfs(int u, int fa) {
    34     for (int i = head[u]; i != -1; i = Next[i]) {
    35         if (to[i] == fa) { continue; } dfs(to[i], u);
    36         dp[0][u] += max(dp[0][to[i]], dp[1][to[i]]);
    37         dp[1][u] += dp[0][to[i]];
    38     }
    39 }
    40 
    41 int main() {
    42 #ifdef __AiR_H
    43     freopen("in.txt", "r", stdin);
    44 //    freopen("out.txt", "w", stdout);
    45 #endif // __AiR_H
    46     while (scanf("%d", &n) != EOF) {
    47         init(); int a, b; memset(dp, 0, sizeof(dp));
    48         for (int i = 1; i <= n; ++i) { scanf("%d", &dp[1][i]); }
    49         while (scanf("%d %d", &a, &b) != EOF) {
    50             if (a == 0 && b == 0) { break; }
    51             add_edge(a, b); add_edge(b, a); ++in[a];  //b是a的顶头上司
    52         }
    53         for (int i = 1; i <= n; ++i) {
    54             if (in[i]) { continue; }
    55             dfs(i, 0); printf("%d
    ", max(dp[0][i], dp[1][i])); break;
    56         }
    57     }
    58 #ifdef __AiR_H
    59     printf("Time used = %.2fs
    ", (double)clock() / CLOCKS_PER_SEC);
    60 #endif // __AiR_H
    61     return 0;
    62 }
    View Code
  • 相关阅读:
    20200807日报
    20200806日报
    《大道至简》读书感悟
    20200805日报
    20200804日报
    20200803日报
    20200802日报
    vue中mounted内如何调完异步方法再渲染
    小程序画布识别iPhone11
    np.meshgrid() 生成网格坐标函数
  • 原文地址:https://www.cnblogs.com/zhaoyz/p/7590465.html
Copyright © 2011-2022 走看看