結果

問題 No.768 Tapris and Noel play the game on Treeone
ユーザー H3PO4H3PO4
提出日時 2021-02-01 10:58:51
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 648 ms / 2,000 ms
コード長 888 bytes
コンパイル時間 90 ms
コンパイル使用メモリ 10,788 KB
実行使用メモリ 52,352 KB
最終ジャッジ日時 2023-09-12 10:07:27
合計ジャッジ時間 11,157 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,548 KB
testcase_01 AC 19 ms
8,532 KB
testcase_02 AC 19 ms
8,616 KB
testcase_03 AC 22 ms
8,872 KB
testcase_04 AC 23 ms
8,916 KB
testcase_05 AC 23 ms
8,856 KB
testcase_06 AC 21 ms
8,856 KB
testcase_07 AC 365 ms
31,736 KB
testcase_08 AC 195 ms
20,076 KB
testcase_09 AC 227 ms
22,156 KB
testcase_10 AC 177 ms
19,088 KB
testcase_11 AC 593 ms
43,672 KB
testcase_12 AC 609 ms
43,896 KB
testcase_13 AC 572 ms
42,736 KB
testcase_14 AC 562 ms
42,684 KB
testcase_15 AC 619 ms
44,820 KB
testcase_16 AC 629 ms
44,964 KB
testcase_17 AC 648 ms
45,680 KB
testcase_18 AC 634 ms
52,008 KB
testcase_19 AC 643 ms
52,352 KB
testcase_20 AC 634 ms
50,384 KB
testcase_21 AC 611 ms
48,672 KB
20evil_special_uni1.txt AC 626 ms
46,588 KB
20evil_special_uni2.txt AC 619 ms
44,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

N = int(input())
T = [[] for _ in range(N)]
AB = [tuple(int(x) - 1 for x in input().split()) for _ in range(N - 1)]
for a, b in AB:
    T[a].append(b)
    T[b].append(a)

# dfs
dfs_order = []
d = deque([0])
nonvisited = [True] * N
nonvisited[0] = False
while d:
    v = d.pop()
    for x in T[v]:
        if nonvisited[x]:
            d.append(x)
            dfs_order.append((x, v))
            nonvisited[x] = False

# 子から親へ
dp0, dp1 = [0] * N, [0] * N
for child, v in reversed(dfs_order):
    if dp0[child] == len(T[child]) - 1:
        dp1[v] += 1
    else:
        dp0[v] += 1

# 親から子へ
win = set()
if not dp1[0]:
    win.add(0)
for child, v in dfs_order:
    if v in win:
        dp1[child] += 1
    if not dp1[child] and dp1[v] != 1:
        win.add(child)

print(len(win))
if win:
    print(*sorted(w + 1 for w in win), sep='\n')
0