結果
問題 | No.1582 Vertexes vs Edges |
ユーザー | shotoyoo |
提出日時 | 2021-07-02 21:54:10 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 197 ms / 2,000 ms |
コード長 | 1,009 bytes |
コンパイル時間 | 156 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 89,600 KB |
最終ジャッジ日時 | 2024-06-29 11:25:57 |
合計ジャッジ時間 | 5,784 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 36 |
ソースコード
import sys input = lambda : sys.stdin.readline().rstrip() sys.setrecursionlimit(2*10**5+10) write = lambda x: sys.stdout.write(x+"\n") debug = lambda x: sys.stderr.write(x+"\n") writef = lambda x: print("{:.12f}".format(x)) ### 木の読み込み yomikomi n = int(input()) ns = [[] for _ in range(n)] for _ in range(n-1): u,v = map(int, input().split()) u -= 1 v -= 1 ns[u].append(v) ns[v].append(u) d0 = [0]*n d1 = [0]*n # 深さ優先探索 (巻き戻しあり) dfs tree q = [(0, -1)] while q: u,prv = q.pop() if u<0: # 返るときの処理 u = ~u v0 = 0 v1 = 1 for v in ns[u]: if v==prv: continue v0 += d1[v] v1 += min(d0[v], d1[v]) d0[u] = v0 d1[u] = v1 else: q.append((~u,prv)) for v in ns[u]: # 進むときの処理 if v==prv: continue q.append((v,u)) ans = min(d0[0], d1[0]) print(ans)