結果

問題 No.1928 Make a Binary Tree
ユーザー tnodino
提出日時 2022-05-06 22:48:21
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 773 bytes
コンパイル時間 149 ms
コンパイル使用メモリ 82,204 KB
実行使用メモリ 336,784 KB
最終ジャッジ日時 2024-07-06 00:13:01
合計ジャッジ時間 13,133 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 4 WA * 29 TLE * 1 -- * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import setrecursionlimit
setrecursionlimit(10**6)

def dfs1(pos):
    ret = 1
    for nxt in G1[pos]:
        ret += dfs1(nxt)
    Dist[pos] = ret
    return ret

N = int(input())
G1 = [[] for _ in range(N)]
G2 = [-1] * N
for _ in range(N-1):
    x,y = map(int,input().split())
    x -= 1
    y -= 1
    G1[x].append(y)
    G2[y] = x
Dist = [-1] * N
dfs1(0)
for i in range(N):
    Dist[i] = [Dist[i], i]
Dist.sort(reverse=True)
Cnt = [0] * N
Flg = [1] * N
for i in range(1,N):
    x = Dist[i][1]
    pos = G2[x]
    Queue = []
    while pos != -1:
        Queue.append(pos)
        if Cnt[pos] < 2:
            Cnt[pos] += 1
            break
        pos = G2[pos]
    if pos == -1:
        for k in Queue:
            G2[k] = -1
        Flg[x] = 0
print(sum(Flg))
0