結果

問題 No.1817 Reversed Edges
ユーザー hirakuhiraku
提出日時 2022-01-22 09:17:37
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 700 ms / 2,000 ms
コード長 865 bytes
コンパイル時間 942 ms
コンパイル使用メモリ 10,728 KB
実行使用メモリ 29,996 KB
最終ジャッジ日時 2023-08-18 00:59:27
合計ジャッジ時間 15,023 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
7,816 KB
testcase_01 AC 16 ms
7,860 KB
testcase_02 AC 15 ms
7,792 KB
testcase_03 AC 16 ms
7,848 KB
testcase_04 AC 15 ms
7,796 KB
testcase_05 AC 15 ms
7,872 KB
testcase_06 AC 15 ms
7,784 KB
testcase_07 AC 602 ms
26,512 KB
testcase_08 AC 272 ms
17,024 KB
testcase_09 AC 573 ms
25,468 KB
testcase_10 AC 328 ms
18,180 KB
testcase_11 AC 421 ms
21,100 KB
testcase_12 AC 683 ms
29,056 KB
testcase_13 AC 699 ms
28,908 KB
testcase_14 AC 696 ms
29,040 KB
testcase_15 AC 690 ms
28,892 KB
testcase_16 AC 683 ms
28,936 KB
testcase_17 AC 678 ms
28,984 KB
testcase_18 AC 700 ms
28,992 KB
testcase_19 AC 680 ms
28,980 KB
testcase_20 AC 700 ms
28,892 KB
testcase_21 AC 694 ms
28,932 KB
testcase_22 AC 506 ms
29,996 KB
testcase_23 AC 513 ms
26,828 KB
testcase_24 AC 504 ms
28,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
graph = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

# 始めに頂点位置のスコアを求める。
score = 0
q = [(0, -1)]  # now, parent
while q:
    now, par = q.pop()
    if par > now:
        score += 1
    for nxt in graph[now]:
        if nxt != par:
            q.append((nxt, now))

# 今いる頂点nowから隣接する頂点nxtに移動してnow<nxtならスコア+1, now>nxtならスコア-1する
ans = [0] * n
ans[0] = score

q = [(0, -1)]  # now, parent
while q:
    now, par = q.pop()
    for nxt in graph[now]:
        if nxt != par:
            if nxt > now:
                ans[nxt] = ans[now] + 1
            else:
                ans[nxt] = ans[now] - 1
            q.append((nxt, now))

for a in ans:
    print(a)
0