結果

問題 No.768 Tapris and Noel play the game on Treeone
ユーザー H3PO4
提出日時 2021-02-01 10:58:51
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 767 ms / 2,000 ms
コード長 888 bytes
コンパイル時間 256 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 54,920 KB
最終ジャッジ日時 2024-06-29 22:47:38
合計ジャッジ時間 12,688 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

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