Submission #1748707


Source Code Expand

import sys
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
INF = float("inf")
sys.setrecursionlimit(10**7)

# 4近傍(右, 下, 左, 上)
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]


def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W


def dfs(now, parent, h_list, graph):
    use = h_list[now] * 2
    ans = 0
    for c in graph[now]:
        if c == parent:
            continue
        cost = dfs(c, now, h_list, graph)
        if cost > 0:
            use = 2
            ans += cost
    return use + ans


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

    ans = dfs(x, -1, h_list, graph)
    print(max(0, ans - 2))


if __name__ == '__main__':
    main()

Submission Info

Submission Time
Task B - ツリーグラフ
User MitI_7
Language Python (3.4.3)
Score 100
Code Size 1168 Byte
Status AC
Exec Time 36 ms
Memory 4016 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 2
AC × 20
Set Name Test Cases
Sample subtask0_sample_01.txt, subtask0_sample_02.txt
All subtask0_sample_01.txt, subtask0_sample_02.txt, subtask1_line01.txt, subtask1_line02.txt, subtask1_line03.txt, subtask1_line04.txt, subtask1_line05.txt, subtask1_line06.txt, subtask1_random01.txt, subtask1_random02.txt, subtask1_random03.txt, subtask1_random04.txt, subtask1_random05.txt, subtask1_random06.txt, subtask1_random07.txt, subtask1_random08.txt, subtask1_special01.txt, subtask1_special02.txt, subtask1_special03.txt, subtask1_special04.txt
Case Name Status Exec Time Memory
subtask0_sample_01.txt AC 36 ms 4016 KB
subtask0_sample_02.txt AC 25 ms 3824 KB
subtask1_line01.txt AC 26 ms 3824 KB
subtask1_line02.txt AC 26 ms 3820 KB
subtask1_line03.txt AC 26 ms 3820 KB
subtask1_line04.txt AC 26 ms 3824 KB
subtask1_line05.txt AC 25 ms 3820 KB
subtask1_line06.txt AC 26 ms 3820 KB
subtask1_random01.txt AC 26 ms 3824 KB
subtask1_random02.txt AC 25 ms 3824 KB
subtask1_random03.txt AC 25 ms 3820 KB
subtask1_random04.txt AC 26 ms 3824 KB
subtask1_random05.txt AC 26 ms 3824 KB
subtask1_random06.txt AC 25 ms 3820 KB
subtask1_random07.txt AC 26 ms 3820 KB
subtask1_random08.txt AC 26 ms 3820 KB
subtask1_special01.txt AC 25 ms 3824 KB
subtask1_special02.txt AC 26 ms 3820 KB
subtask1_special03.txt AC 26 ms 3820 KB
subtask1_special04.txt AC 25 ms 3820 KB