結果

問題 No.1817 Reversed Edges
ユーザー lloyzlloyz
提出日時 2022-01-22 00:02:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 292 ms / 2,000 ms
コード長 570 bytes
コンパイル時間 374 ms
コンパイル使用メモリ 82,128 KB
実行使用メモリ 115,340 KB
最終ジャッジ日時 2024-11-26 06:57:36
合計ジャッジ時間 6,697 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

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

D = [0 for _ in range(n)]
root_res = 0
seen = set([0])
Stack = [0]
while Stack:
    curr = Stack.pop()
    for np in edge[curr]:
        if np in seen:
            continue
        seen.add(np)
        Stack.append(np)
        D[np] = D[curr] + (1 if np > curr else -1)
        if np < curr:
            root_res += 1
for i in range(n):
    print(root_res + D[i])
0