結果

問題 No.1817 Reversed Edges
ユーザー ygd.ygd.
提出日時 2022-01-21 22:03:28
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 524 ms / 2,000 ms
コード長 1,349 bytes
コンパイル時間 103 ms
コンパイル使用メモリ 10,808 KB
実行使用メモリ 32,204 KB
最終ジャッジ日時 2023-08-17 06:04:08
合計ジャッジ時間 9,866 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,248 KB
testcase_01 AC 17 ms
8,228 KB
testcase_02 AC 17 ms
8,332 KB
testcase_03 AC 17 ms
8,276 KB
testcase_04 AC 16 ms
8,300 KB
testcase_05 AC 16 ms
8,228 KB
testcase_06 AC 16 ms
8,356 KB
testcase_07 AC 452 ms
29,364 KB
testcase_08 AC 208 ms
18,544 KB
testcase_09 AC 430 ms
27,960 KB
testcase_10 AC 235 ms
19,812 KB
testcase_11 AC 316 ms
23,372 KB
testcase_12 AC 517 ms
32,180 KB
testcase_13 AC 516 ms
32,204 KB
testcase_14 AC 518 ms
32,132 KB
testcase_15 AC 519 ms
32,112 KB
testcase_16 AC 524 ms
32,136 KB
testcase_17 AC 515 ms
32,184 KB
testcase_18 AC 498 ms
32,080 KB
testcase_19 AC 515 ms
32,184 KB
testcase_20 AC 512 ms
32,204 KB
testcase_21 AC 518 ms
32,172 KB
testcase_22 AC 345 ms
26,444 KB
testcase_23 AC 346 ms
26,496 KB
testcase_24 AC 352 ms
31,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
#input = sys.stdin.readline #文字列につけてはダメ
#input = sys.stdin.buffer.readline #文字列につけてはダメ
#sys.setrecursionlimit(1000000)
#import bisect
#import itertools
#import random
from heapq import heapify, heappop, heappush
#from collections import defaultdict 
#from collections import deque
#import copy
#import math
#from functools import lru_cache
#@lru_cache(maxsize=None)
#MOD = pow(10,9) + 7
#MOD = 998244353
#dx = [1,0,-1,0]
#dy = [0,1,0,-1]


def main():
    N = int(input())
    G = [[] for _ in range(N)]
    for i in range(N-1):
        a,b = map(int,input().split())
        a -= 1; b -= 1
        G[a].append(b)
        G[b].append(a)
    
    ans = [-1]*N
    start = 0
    stack = [0]
    par = [-1]*N
    while stack:
        v = stack.pop()
        for u in G[v]:
            if par[v] == u: continue
            par[u] = v
            #print(u,v)
            if u < v:
                start += 1
            stack.append(u)
    ans[0] = start
    stack = [0]
    while stack:
        v = stack.pop()
        for u in G[v]:
            if par[v] == u: continue
            par[u] = v
            if u > v:
                ans[u] = ans[v] + 1
            else:
                ans[u] = ans[v] - 1
            stack.append(u)
    print(*ans,sep="\n")
    

if __name__ == '__main__':
    main()
0