結果
| 問題 | No.3514 Majority Driven Tree |
| コンテスト | |
| ユーザー |
yt142857
|
| 提出日時 | 2026-03-25 13:55:47 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
WA
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 1,222 bytes |
| 記録 | |
| コンパイル時間 | 376 ms |
| コンパイル使用メモリ | 85,396 KB |
| 実行使用メモリ | 84,480 KB |
| 最終ジャッジ日時 | 2026-04-24 20:50:10 |
| 合計ジャッジ時間 | 7,562 ms |
|
ジャッジサーバーID (参考情報) |
judge2_1 / judge3_1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 14 WA * 23 |
ソースコード
import sys
sys.setrecursionlimit(10**6)
n = int(input())
graph = []
for i in range(n+1):
graph.append([])
for i in range(n-1):
a,b = map(int,input().split())
graph[a].append(b)
graph[b].append(a)
dp1 = [10**18]*(n+1) #その頂点を根とする部分木をすべて黒色にするために塗る必要のある頂点数の最小値
dp2 = [10**18]*(n+1) #「その頂点の親が黒色に塗られていると仮定した時に、その部分木を塗りきれる」、という状態にするために
#塗る必要のある頂点数の最小値
visited = [False]*(n+1)
def dfs(node):
visited[node] = True
children = []
for nei in graph[node]:
if visited[nei] == False:
dp1[nei],dp2[nei] = dfs(nei)
children.append((dp1[nei]-dp2[nei],dp1[nei],dp2[nei]))
if len(children) == 0:
return (1,0)
else:
children.sort()
children_dp1 = [i[1] for i in children]
children_dp2 = [i[2] for i in children]
cnt = len(graph[node])//2 + 1
node_dp1 = min(sum(children_dp1[:cnt]) + sum(children_dp2[cnt:]) ,1 + sum(children_dp2))
node_dp2 = min(node_dp1, sum(children_dp1[:cnt-1]) + sum(children_dp2[cnt-1:]))
return (node_dp1,node_dp2)
print(dfs(1)[0])
yt142857