Submission #1748629
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(n, parent, depth, h_list, graph):
ans = 0
for c in graph[n]:
if c == parent:
continue
cost = dfs(c, n, depth + 1, h_list, graph)
if cost:
ans += cost - depth
if ans != 0:
return ans + depth
if ans == 0 and h_list[n - 1] == 1:
return depth
return 0
def main():
n, x = map(int, input().split())
h_list = list(map(int, input().split()))
h_list[x - 1] = 0
graph = defaultdict(list)
for _ in range(n - 1):
a, b = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
print(dfs(x, -1, 0, h_list, graph) * 2)
if __name__ == '__main__':
main()
Submission Info
Submission Time |
|
Task |
B - ツリーグラフ |
User |
MitI_7 |
Language |
Python (3.4.3) |
Score |
100 |
Code Size |
1179 Byte |
Status |
AC |
Exec Time |
26 ms |
Memory |
3832 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
100 / 100 |
Status |
|
|
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 |
25 ms |
3828 KB |
subtask0_sample_02.txt |
AC |
25 ms |
3832 KB |
subtask1_line01.txt |
AC |
25 ms |
3832 KB |
subtask1_line02.txt |
AC |
25 ms |
3828 KB |
subtask1_line03.txt |
AC |
25 ms |
3832 KB |
subtask1_line04.txt |
AC |
25 ms |
3828 KB |
subtask1_line05.txt |
AC |
25 ms |
3832 KB |
subtask1_line06.txt |
AC |
25 ms |
3828 KB |
subtask1_random01.txt |
AC |
25 ms |
3828 KB |
subtask1_random02.txt |
AC |
24 ms |
3828 KB |
subtask1_random03.txt |
AC |
25 ms |
3828 KB |
subtask1_random04.txt |
AC |
25 ms |
3832 KB |
subtask1_random05.txt |
AC |
25 ms |
3828 KB |
subtask1_random06.txt |
AC |
25 ms |
3828 KB |
subtask1_random07.txt |
AC |
25 ms |
3832 KB |
subtask1_random08.txt |
AC |
25 ms |
3832 KB |
subtask1_special01.txt |
AC |
25 ms |
3832 KB |
subtask1_special02.txt |
AC |
25 ms |
3828 KB |
subtask1_special03.txt |
AC |
26 ms |
3832 KB |
subtask1_special04.txt |
AC |
25 ms |
3828 KB |