• Codeforces Round #168 (Div. 2)D. Zero Tree(DP,中等难度)


    D. Zero Tree
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    A tree is a graph with n vertices and exactly n - 1 edges; this graph should meet the following condition: there exists exactly one shortest (by number of edges) path between any pair of its vertices.

    A subtree of a tree T is a tree with both vertices and edges as subsets of vertices and edges of T.

    You're given a tree with n vertices. Consider its vertices numbered with integers from 1 to n. Additionally an integer is written on every vertex of this tree. Initially the integer written on the i-th vertex is equal to vi. In one move you can apply the following operation:

    1. Select the subtree of the given tree that includes the vertex with number 1.
    2. Increase (or decrease) by one all the integers which are written on the vertices of that subtree.

    Calculate the minimum number of moves that is required to make all the integers written on the vertices of the given tree equal to zero.

    Input

    The first line of the input contains n (1 ≤ n ≤ 105). Each of the next n - 1 lines contains two integers ai and bi (1 ≤ ai, bi ≤ nai ≠ bi) indicating there's an edge between vertices ai and bi. It's guaranteed that the input graph is a tree.

    The last line of the input contains a list of n space-separated integers v1, v2, ..., vn (|vi| ≤ 109).

    Output

    Print the minimum number of operations needed to solve the task.

    Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

    Sample test(s)
    Input
    3
    1 2
    1 3
    1 -1 1
    Output
    3

    一开始题目理解错误:“includes the vertex with number 1.是指结点1而非其上integer为1的结点
    思路是先处理儿子再处理父亲。这是因为:设有结点x,x的val变为0所需操作次数x.count = in + de + r,这里in是x因为儿子结点的+1操作
    而随之产生的+1操作的次数(对儿子的操作会传递到父亲),而de就是-1操作,显然,in是x的所有儿子中+1次数的最大值,de是所有儿子中-1次
    数的最大值。而r是什么?在处理完儿子后确保儿子的val为0,但不保证x的val为0,r就是余下的对x操作的次数:r=x.val+in-de(这些操作是
    发生在x和他的父亲之间的)。由以上于可见,对x.count的统计能转化为对其儿子结点的相关信息的统计,故而此前说“先处理儿子后处理父亲”。
    结点1是最后处理的。
    这里还涉及如何保存一个结点的所有儿子的信息的问题,我开始用邻接表,超时,后来参考别人的代码才想起来可以用vector。

    AC Code:
     1 #include <iostream>
     2 #include <string>
     3 #include <set>
     4 #include <map>
     5 #include <vector>
     6 #include <stack>
     7 #include <queue>
     8 #include <cmath>
     9 #include <cstdio>
    10 #include <cstring>
    11 #include <algorithm>
    12 using namespace std;
    13 #define LL long long
    14 #define cti const int
    15 #define dg(i) cout << "*" << i << endl;
    16 
    17 cti MAXN = 100005;
    18 int n;
    19 bool vis[MAXN];
    20 LL v[MAXN];
    21 struct Count
    22 {
    23     LL in;  //结点i增1次数
    24     LL de;  //结点i减1次数
    25 }cnt[MAXN];
    26 vector<LL> node[MAXN];
    27 
    28 Count DP(int i)
    29 {
    30     vis[i] = true;
    31     LL in = 0, de = 0;  //in for increse, de for decrese
    32     Count t;
    33     for(vector<LL>::iterator it = node[i].begin(); it != node[i].end(); it++)
    34     {
    35         if(vis[*it]) continue;
    36         t = DP(*it);
    37         if(t.in > in) in = t.in;
    38         if(t.de > de) de = t.de;
    39     }
    40     cnt[i].de = de;
    41     cnt[i].in = in;
    42     int d = v[i] - cnt[i].de + cnt[i].in;
    43     if(d > 0) cnt[i].de += d;
    44     else cnt[i].in -= d;
    45     return cnt[i];
    46 }
    47 
    48 
    49 int main()
    50 {
    51     while(scanf("%d", &n) != EOF)
    52     {
    53         for(int i = 1; i <= n; i++)
    54         {
    55             vis[0] = false;
    56             while(!node[i].empty()) node[i].pop_back();
    57         }
    58         int a, b;
    59         for(int i = 1; i < n; i++)
    60         {
    61             scanf("%d %d", &a, &b);
    62             node[a].push_back(b);
    63             node[b].push_back(a);
    64         }
    65         for(int i = 1; i <= n; i++)
    66             scanf("%I64d", &v[i]);
    67         Count ans = DP(1);
    68         printf("%I64d\n", ans.de + ans.in);
    69     }
    70     return 0;
    71 }

  • 相关阅读:
    spring和mybatis整合
    mybatis(二)
    Django-model基础
    用户用户组管理:用户配置文件-组信息文件
    第一章:编译程序概论
    软件包管理:脚本安装包
    软件包管理:源码包管理-源码包安装过程
    软件包管理:源码包管理-源码包与RPM包的区别
    软件包管理:yum在线管理-yum命令
    软件包管理:rpm包管理-yum在线管理-IP地址配置和网络yum源
  • 原文地址:https://www.cnblogs.com/cszlg/p/2924068.html
Copyright © 2020-2023  润新知