Zero Tree (Codeforces 274B)
Tags: Greedy, trees

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:  Select the subtree of the given tree that includes the vertex with number 1.  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 ≤ n; ai ≠ 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 Inputs:
3
1 2
1 3
1 -1 1

Sample Outputs:
3

Solution (Python):
import threading
import sys
from sys import stdin
input = stdin.readline
sys.setrecursionlimit(10 ** 8)
from collections import defaultdict

def main():
    n = int(input())
    graph = [[] for _ in range(n)]
    for _ in range(n - 1):
        (a, b) = map(int, input().split())
        a -= 1
        b -= 1
        graph[a].append(b)
        graph[b].append(a)
    arr = list(map(int, input().split()))

    def dfs(node, par):
        pos = 0
        neg = 0
        for nie in graph[node]:
            if nie != par:
                (a, b) = dfs(nie, node)
                pos = max(pos, a)
                neg = max(neg, b)
        arr[node] += pos - neg
        if arr[node] > 0:
            neg += arr[node]
        else:
            pos -= arr[node]
        return (pos, neg)
    (a, b) = dfs(0, -1)
    print(a + b)
threading.stack_size(10 ** 8)
t = threading.Thread(target=main)
t.start()
t.join()

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(268435455)
import threading
threading.stack_size(67108864)

def main():
    n = int(input())
    graph = [[] for _ in range(n)]
    for _ in range(n - 1):
        (u, v) = map(int, input().split())
        graph[u - 1].append(v - 1)
        graph[v - 1].append(u - 1)
    vals = list(map(int, input().split()))
    (i, d) = ([0] * n, [0] * n)

    def dfs(cur, pre):
        for nxt in graph[cur]:
            if nxt != pre:
                (inc, dec) = dfs(nxt, cur)
                i[cur] = max(i[cur], inc)
                d[cur] = max(d[cur], dec)
        shift = vals[cur] + i[cur] - d[cur]
        if shift > 0:
            d[cur] += shift
        else:
            i[cur] -= shift
        return (i[cur], d[cur])
    dfs(0, 0)
    print(i[0] + d[0])
threading.Thread(target=main).start()

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(268435455)
import threading
threading.stack_size(67108864)

def main():
    n = int(input())
    graph = [[] for _ in range(n)]
    for _ in range(n - 1):
        (u, v) = map(int, input().split())
        graph[u - 1].append(v - 1)
        graph[v - 1].append(u - 1)
    vals = list(map(int, input().split()))
    (i, d) = ([0] * n, [0] * n)

    def dfs(cur, pre):
        for nxt in graph[cur]:
            if nxt != pre:
                (inc, dec) = dfs(nxt, cur)
                i[cur] = max(i[cur], inc)
                d[cur] = max(d[cur], dec)
        shift = vals[cur] + i[cur] - d[cur]
        if shift > 0:
            d[cur] += shift
        else:
            i[cur] -= shift
        return (i[cur], d[cur])
    dfs(0, 0)
    print(i[0] + d[0])
threading.Thread(target=main).start()