結果

問題 No.1817 Reversed Edges
ユーザー customaddonecustomaddone
提出日時 2022-01-22 00:13:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 458 ms / 2,000 ms
コード長 1,876 bytes
コンパイル時間 202 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 184,064 KB
最終ジャッジ日時 2024-05-04 17:59:41
合計ジャッジ時間 9,376 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
60,928 KB
testcase_01 AC 68 ms
61,056 KB
testcase_02 AC 56 ms
61,312 KB
testcase_03 AC 55 ms
61,184 KB
testcase_04 AC 56 ms
61,056 KB
testcase_05 AC 55 ms
61,312 KB
testcase_06 AC 57 ms
60,928 KB
testcase_07 AC 388 ms
90,112 KB
testcase_08 AC 249 ms
84,096 KB
testcase_09 AC 387 ms
89,600 KB
testcase_10 AC 279 ms
84,608 KB
testcase_11 AC 327 ms
87,168 KB
testcase_12 AC 435 ms
92,288 KB
testcase_13 AC 438 ms
92,032 KB
testcase_14 AC 447 ms
92,160 KB
testcase_15 AC 458 ms
92,672 KB
testcase_16 AC 424 ms
91,904 KB
testcase_17 AC 447 ms
92,160 KB
testcase_18 AC 433 ms
92,160 KB
testcase_19 AC 416 ms
91,520 KB
testcase_20 AC 446 ms
92,032 KB
testcase_21 AC 439 ms
92,032 KB
testcase_22 AC 172 ms
92,032 KB
testcase_23 AC 175 ms
92,160 KB
testcase_24 AC 413 ms
184,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict, deque, Counter
from heapq import heapify, heappop, heappush
import math
from copy import deepcopy
from itertools import combinations, permutations, product, combinations_with_replacement
from bisect import bisect_left, bisect_right

import sys

def input():
    return sys.stdin.readline().rstrip()
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getListGraph():
    return list(map(lambda x:int(x) - 1, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]

mod = 10 ** 9 + 7
MOD = 998244353
sys.setrecursionlimit(10000000)
inf = float('inf')
eps = 10 ** (-15)
dy = [0, 1, 0, -1]
dx = [1, 0, -1, 0]

#############
# Main Code #
#############

"""
ある辺について
x1-1-2-x2
x2の方の頂点からスタートする場合はその辺は逆張りになる
x2の頂点にそれぞれ+1していけばいい
dfsする 子の方が大きかったら+1する
累積木dp
"""

N = getN()
E = [[] for i in range(N)]
for _ in range(N - 1):
    a, b = getNM()
    E[a - 1].append(b - 1)
    E[b - 1].append(a - 1)

fore = [0] * N
back = [0] * N

def dfs1(u, p):
    global fore, back
    for v in E[u]:
        if v != p:
            # 行きがけ 下るときになんか書く
            dfs1(v, u)
            # 帰りがけ 登るときになんか書く
            if u > v:
                back[u] += 1
            back[u] += back[v]

dfs1(0, -1)

 # 親の分 - 自分の分 + (親 < 自分)
def dfs2(u, p):
    global back
    for v in E[u]:
        if v != p:
            back[v] = back[u] # 引き継ぐ
            # 行きがけ
            if u < v:
                back[v] += 1
            else:
                back[v] -= 1
            dfs2(v, u)

dfs2(0, -1)
for a in back:
    print(a)
0