結果

問題 No.1817 Reversed Edges
ユーザー ShirotsumeShirotsume
提出日時 2022-01-21 23:09:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 412 ms / 2,000 ms
コード長 892 bytes
コンパイル時間 275 ms
コンパイル使用メモリ 86,812 KB
実行使用メモリ 100,108 KB
最終ジャッジ日時 2023-08-17 07:42:01
合計ジャッジ時間 9,406 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
71,500 KB
testcase_01 AC 90 ms
71,620 KB
testcase_02 AC 92 ms
71,748 KB
testcase_03 AC 91 ms
71,524 KB
testcase_04 AC 93 ms
71,524 KB
testcase_05 AC 94 ms
71,612 KB
testcase_06 AC 93 ms
71,680 KB
testcase_07 AC 345 ms
95,608 KB
testcase_08 AC 227 ms
87,120 KB
testcase_09 AC 349 ms
94,304 KB
testcase_10 AC 257 ms
88,232 KB
testcase_11 AC 300 ms
90,348 KB
testcase_12 AC 410 ms
97,244 KB
testcase_13 AC 400 ms
97,392 KB
testcase_14 AC 412 ms
97,620 KB
testcase_15 AC 383 ms
97,712 KB
testcase_16 AC 372 ms
98,028 KB
testcase_17 AC 363 ms
97,680 KB
testcase_18 AC 358 ms
97,624 KB
testcase_19 AC 374 ms
97,404 KB
testcase_20 AC 401 ms
97,544 KB
testcase_21 AC 391 ms
97,272 KB
testcase_22 AC 238 ms
97,792 KB
testcase_23 AC 247 ms
100,108 KB
testcase_24 AC 236 ms
95,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
graph = [[] for _ in range(n)]
edge = []
for _ in range(n - 1):
    a, b = map(int,input().split())
    a -= 1; b -= 1
    edge.append((a, b))
    graph[a].append(b)
    graph[b].append(a)

value = [0] * n
visit = [-1] * n
visit[0] = 0
from collections import deque
q = deque()
q.append(0)
while q:
    now = q.popleft()
    for to in graph[now]:
        if visit[to] < 0:
            visit[to] = visit[now] + 1
            q.append(to)
depth = visit
for a, b in edge:
    if a > b: a, b = b, a
    #a < bを仮定
    if depth[a] > depth[b]:
        value[a] -= 1
        value[0] += 1
    else:
        value[b] += 1
        
        
visit = [False] * n
visit[0] = True
q = deque()
q.append(0)
while q:
    now = q.pop()
    for to in graph[now]:
        if depth[to] > depth[now]:
            value[to] += value[now]
            q.append(to)

for v in value:
    print(v)
0