結果

問題 No.1817 Reversed Edges
ユーザー roarisroaris
提出日時 2022-03-02 19:01:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 318 ms / 2,000 ms
コード長 1,010 bytes
コンパイル時間 3,908 ms
コンパイル使用メモリ 86,864 KB
実行使用メモリ 96,456 KB
最終ジャッジ日時 2023-09-23 10:49:40
合計ジャッジ時間 8,800 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
71,836 KB
testcase_01 AC 87 ms
71,488 KB
testcase_02 AC 86 ms
71,600 KB
testcase_03 AC 86 ms
71,676 KB
testcase_04 AC 85 ms
71,692 KB
testcase_05 AC 85 ms
71,580 KB
testcase_06 AC 87 ms
71,612 KB
testcase_07 AC 237 ms
90,876 KB
testcase_08 AC 176 ms
84,524 KB
testcase_09 AC 254 ms
90,184 KB
testcase_10 AC 184 ms
85,256 KB
testcase_11 AC 212 ms
87,596 KB
testcase_12 AC 305 ms
93,204 KB
testcase_13 AC 318 ms
92,812 KB
testcase_14 AC 299 ms
93,028 KB
testcase_15 AC 291 ms
93,388 KB
testcase_16 AC 311 ms
93,164 KB
testcase_17 AC 266 ms
93,072 KB
testcase_18 AC 287 ms
92,756 KB
testcase_19 AC 316 ms
93,480 KB
testcase_20 AC 301 ms
93,176 KB
testcase_21 AC 297 ms
93,444 KB
testcase_22 AC 179 ms
96,376 KB
testcase_23 AC 177 ms
96,456 KB
testcase_24 AC 173 ms
91,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

def bfs1():
    global all_add
    
    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:
                    all_add += 1
                    add[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:
                res[nv] = res[v]+add[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)

all_add = 0
add = [0]*N
bfs1()
res = [0]*N
bfs2()

for v in range(N):
    print(res[v]+all_add)
0