結果

問題 No.1817 Reversed Edges
ユーザー roarisroaris
提出日時 2022-03-02 18:53:39
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,029 bytes
コンパイル時間 370 ms
コンパイル使用メモリ 87,036 KB
実行使用メモリ 97,964 KB
最終ジャッジ日時 2023-09-23 10:46:11
合計ジャッジ時間 10,338 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
71,848 KB
testcase_01 AC 93 ms
71,820 KB
testcase_02 WA -
testcase_03 AC 91 ms
71,500 KB
testcase_04 AC 91 ms
71,700 KB
testcase_05 WA -
testcase_06 WA -
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 194 ms
97,820 KB
testcase_23 AC 195 ms
97,964 KB
testcase_24 AC 183 ms
93,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import *

def bfs1():
    q = deque([0])
    dist = [-1]*N
    dist[0] = 0
    
    while q:
        v = q.popleft()
        
        for nv in G[v]:
            if dist[nv]==-1:
                if v<nv:
                    add[nv] += 1
                else:
                    sub[nv] -= 1
                
                dist[nv] = dist[v]+1
                q.append(nv)

def bfs2():
    q = deque([0])
    dist = [-1]*N
    dist[0] = 0
    
    while q:
        v = q.popleft()
        
        for nv in G[v]:
            if dist[nv]==-1:
                res1[nv] = res1[v]+add[nv]
                res2[nv] = res2[v]+sub[nv]
                dist[nv] = dist[v]+1
                q.append(nv)

N = int(input())
G = [[] for _ in range(N)]

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

add = [0]*N
sub = [0]*N
bfs1()
res1 = [0]*N
res2 = [0]*N
bfs2()
m = min(res2)

for v in range(N):
    print(res1[v]+res2[v]-m)
0