結果

問題 No.1817 Reversed Edges
ユーザー rlangevinrlangevin
提出日時 2023-01-31 08:55:48
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 816 bytes
コンパイル時間 1,059 ms
コンパイル使用メモリ 86,928 KB
実行使用メモリ 96,916 KB
最終ジャッジ日時 2023-09-13 03:46:10
合計ジャッジ時間 8,745 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,404 KB
testcase_01 AC 89 ms
71,720 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 92 ms
71,792 KB
testcase_05 WA -
testcase_06 AC 89 ms
71,788 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 216 ms
95,528 KB
testcase_23 AC 214 ms
96,916 KB
testcase_24 AC 203 ms
92,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

def bfs(G, s, N):
    Q = deque([])
    dist = [-1] * N
    par = [-1] * N
    sz = [1] * N
    dist[s] = 0
    rev = [0] * N
    for u in G[s]:
        par[u] = s
        dist[u] = 1
        sz[u] += sz[s]
        Q.append(u)
 
    while Q:
        u = Q.popleft()
        for v in G[u]:
            if dist[v] != -1:
                continue
            if v < u:
                rev[v] += 1
            dist[v] = dist[u] + 1
            par[v] = u
            sz[v] += sz[u]
            Q.append(v)
            
    return rev, dist


N = int(input())
G = [[] for i in range(N)]
for i in range(N - 1):
    A, B = map(int, input().split())
    A, B = A - 1, B - 1
    G[A].append(B)
    G[B].append(A)

R, D = bfs(G, 0, N)
S = sum(R)
for i in range(N):
    print(S - 2 * R[i] + D[i])
0