結果

問題 No.1817 Reversed Edges
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2022-01-21 21:59:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 481 ms / 2,000 ms
コード長 651 bytes
コンパイル時間 494 ms
コンパイル使用メモリ 87,248 KB
実行使用メモリ 194,956 KB
最終ジャッジ日時 2023-08-17 05:57:40
合計ジャッジ時間 10,036 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
71,360 KB
testcase_01 AC 66 ms
71,548 KB
testcase_02 AC 69 ms
71,144 KB
testcase_03 AC 67 ms
71,056 KB
testcase_04 AC 69 ms
71,272 KB
testcase_05 AC 68 ms
71,300 KB
testcase_06 AC 67 ms
71,636 KB
testcase_07 AC 421 ms
91,388 KB
testcase_08 AC 276 ms
85,852 KB
testcase_09 AC 425 ms
90,988 KB
testcase_10 AC 298 ms
85,820 KB
testcase_11 AC 372 ms
88,052 KB
testcase_12 AC 464 ms
93,440 KB
testcase_13 AC 462 ms
93,704 KB
testcase_14 AC 456 ms
93,480 KB
testcase_15 AC 455 ms
93,668 KB
testcase_16 AC 478 ms
93,348 KB
testcase_17 AC 477 ms
93,816 KB
testcase_18 AC 481 ms
93,416 KB
testcase_19 AC 467 ms
92,784 KB
testcase_20 AC 467 ms
93,892 KB
testcase_21 AC 463 ms
92,988 KB
testcase_22 AC 218 ms
92,500 KB
testcase_23 AC 223 ms
92,828 KB
testcase_24 AC 451 ms
194,956 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(3*10**5)

n = int(input())
e = [[] for i in range(n)]
for i in range(n-1):
    a,b = [int(x)-1 for x in input().split()]
    e[a].append(b)
    e[b].append(a)


def dfs(x,p):
    count = 0
    for nex in e[x]:
        if nex == p:
            continue
        if nex < x:
            count += 1
        count += dfs(nex,x)

    return count

ans = [0]*n
ans[0] = dfs(0,-1)

def dfs2(x,p):
    if x != 0:
        if p > x:
            ans[x] = ans[p]-1
        else:
            ans[x] = ans[p]+1

    for nex in e[x]:
        if nex == p:
            continue
        dfs2 (nex,x)
dfs2(0,-1)
for i in ans:
    print(i)
0