結果

問題 No.3514 Majority Driven Tree
コンテスト
ユーザー ゼット
提出日時 2026-04-24 22:42:07
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
WA  
実行時間 -
コード長 862 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 187 ms
コンパイル使用メモリ 85,248 KB
実行使用メモリ 83,968 KB
最終ジャッジ日時 2026-04-24 22:42:22
合計ジャッジ時間 5,729 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other WA * 37
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

N=int(input())
G=[[] for i in range(N)]
for i in range(N-1):
  a,b=map(int,input().split())
  G[a-1].append(b-1)
  G[b-1].append(a-1)
from collections import deque
S=deque()
dist=[-1]*N
dist[0]=0
L=[0]
S.append(0)
while S:
  x=S.pop()
  for y in G[x]:
    if dist[y]==-1:
      dist[y]=dist[x]+1
      S.append(y)
      L.append(y)
L=L[::-1]
dp=[0]*N
h=[]
for pos in L:
  for y in G[pos]:
    if dist[y]<dist[pos]:
      continue
    dp[pos]+=dp[y]
  if len(G[pos])-2*dp[pos]>2:
    dp[pos]+=1
    h.append(pos)
v=[0]*N
for i in range(N):
  v[i]=len(G[i])
used=[False]*N
S=deque()
for pos in h:
  S.append(pos)
  used[pos]=True
while S:
  x=S.pop()
  for y in G[x]:
    if used[y]==True:
      continue
    v[y]-=2
    if v[y]<=0:
      used[y]=True
      S.append(y)
result=len(h)
for i in range(N):
  if used[i]==False:
    result+=1
    break
print(result)
  
0