• Codeforces 274B Zero Tree


    题意:给你一颗树,每个节点上面都有一个值,每一次可以进行一次操作,一次操作包含以下两步

    1)选择一颗包含 1 节点的子树。

    2)对这颗子树进行加一或者减一的操作。

    问你最后使得这颗树 所有点上面的值全部变为  0 的操作数为多少。

    解题思路:dp[i][0/1] 表示 到了这个点  加 和 减的最大值。

    解题代码:

     1 // File Name: 274b.cpp
     2 // Author: darkdream
     3 // Created Time: 2015年03月12日 星期四 21时26分55秒
     4 
     5 #include<vector>
     6 #include<list>
     7 #include<map>
     8 #include<set>
     9 #include<deque>
    10 #include<stack>
    11 #include<bitset>
    12 #include<algorithm>
    13 #include<functional>
    14 #include<numeric>
    15 #include<utility>
    16 #include<sstream>
    17 #include<iostream>
    18 #include<iomanip>
    19 #include<cstdio>
    20 #include<cmath>
    21 #include<cstdlib>
    22 #include<cstring>
    23 #include<ctime>
    24 #define LL long long
    25 #define maxn 100105
    26 using namespace std;
    27 vector<int> mp[maxn];
    28 LL v[maxn];
    29 LL dp[maxn][2];
    30 LL ans = 0 ;
    31 void dfs(int k , int la)
    32 {
    33   
    34    LL maxi = 0 ; 
    35    LL maxd = 0 ; 
    36   for(int i = 0 ;i < mp[k].size();i ++)
    37    {
    38        if(mp[k][i] == la)
    39            continue;
    40        dfs(mp[k][i],k);
    41        maxi = max(maxi,dp[mp[k][i]][1]);
    42        maxd = max(maxd,dp[mp[k][i]][0]);
    43    }
    44    v[k] += maxi;
    45    v[k] -= maxd;
    46    dp[k][0] = maxd;
    47    dp[k][1] = maxi;
    48    if(v[k] > 0)
    49        dp[k][0] += v[k];
    50    else dp[k][1] += (-v[k]);
    51 }
    52 int main(){
    53    int n;
    54    scanf("%d",&n);
    55    for(int i = 1;i <n;i ++)
    56    {
    57       int ta , tb; 
    58       scanf("%d %d",&ta,&tb);
    59       mp[ta].push_back(tb);
    60       mp[tb].push_back(ta);
    61    }
    62    for(int i = 1;i <= n;i ++)
    63    {
    64       cin >> v[i];
    65    }
    66    dfs(1,0);
    67   // for(int i = 1;i <= n;i ++)
    68   // {
    69   //    cout << dp[i][0] << " " << dp[i][1] << endl; 
    70   // }
    71    cout << dp[1][0] + dp[1][1] ;
    72 return 0;
    73 }
    View Code
    没有梦想,何谈远方
  • 相关阅读:
    JSON连载java目的
    2014百度之星预赛(第二场)——Best Financing
    推断值的数组
    Codeforces 437E The Child and Polygon(间隔DP)
    ruby简单的基本 3
    定义和实现二叉树
    C++11并行编程-条件变量(condition_variable)详细说明
    【Bootstrap】自己主动去适应PC、平面、手机Bootstrap网格系统
    使用代码自定义UIView注意一二三
    关于 android 中 postDelayed方法的讲解
  • 原文地址:https://www.cnblogs.com/zyue/p/4334015.html
Copyright © 2020-2023  润新知