結果

問題 No.2677 Minmax Independent Set
ユーザー NokonoKotlin
提出日時 2024-03-13 20:59:57
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
実行時間 -
コード長 1,248 bytes
コンパイル時間 529 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 144,416 KB
最終ジャッジ日時 2024-09-30 00:13:28
合計ジャッジ時間 4,668 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 2 TLE * 1 -- * 58
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#!/usr/bin/python3
import sys
sys.setrecursionlimit(10**6)
N = int(input())
to = [[] for i in range(N)]
for i in range(N-1):
x, y = map(int, input().split())
x -= 1
y -= 1
to[x].append(y)
to[y].append(x)
# https://snuke.hatenablog.com/entry/2019/01/16/203626
pi = [0] * N
deg = [0] * N
dp = [[] for i in range(N)]
ans = [0] * N
def dfs(v, p=-1):
deg[v] = len(to[v])
res = (0, 0)
dp[v] = [0]*deg[v]
for i in range(deg[v]):
u = to[v][i]
if u == p:
pi[v] = i
continue
dp[v][i] = dfs(u, v)
val = (max(dp[v][i]), dp[v][i][0])
res = (res[0] + val[0], res[1] + val[1])
return (res[0], res[1] + 1)
def bfs(v, res_p=0, p=-1):
if p != -1:
dp[v][pi[v]] = res_p
dpl = [(0, 0)]*(deg[v]+1)
for i in range(deg[v]):
dpl[i+1] = (dpl[i][0] + max(dp[v][i]), dpl[i][1] + dp[v][i][0])
dpr = [(0, 0)]*(deg[v]+1)
for i in range(deg[v]-1,-1,-1):
dpr[i] = (dpr[i+1][0] + max(dp[v][i]), dpr[i+1][1] + dp[v][i][0])
ans[v] = (dpr[0][0], dpr[0][1] + 1)
for i in range(deg[v]):
u = to[v][i]
if u == p:
continue
val = (dpl[i][0] + dpr[i+1][0], dpl[i][1] + dpr[i+1][1] + 1)
bfs(u, val, v)
dfs(0)
bfs(0)
#
print(min([x[1] for x in ans]))
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0