結果
問題 |
No.763 Noelちゃんと木遊び
|
ユーザー |
![]() |
提出日時 | 2019-05-04 16:55:34 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,423 bytes |
コンパイル時間 | 129 ms |
コンパイル使用メモリ | 12,672 KB |
実行使用メモリ | 118,904 KB |
最終ジャッジ日時 | 2024-06-23 02:29:11 |
合計ジャッジ時間 | 48,080 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | WA * 10 TLE * 11 |
ソースコード
import numpy as np class sol(object): def __init__(self, N, tree): self.N = N self.tree = tree self.dp0 = np.array([0] * N, dtype=int) self.dp1 = np.array([0] * N, dtype=int) self.visit_cnt = [0] * N self.top_root = np.array([True] * N, dtype=bool) for child in tree: self.top_root[child] = False self.top_root = np.where(self.top_root == True)[0] def dns(self, cur): h = [] h.append(cur) while len(h) > 0: v = h.pop() self.visit_cnt[v] += 1 if len(self.tree[v]) == 0: self.dp0[v] = 1 self.dp1[v] = 0 elif self.visit_cnt[v] -1 < len(self.tree[v]): h.append(v) h.append(self.tree[v][self.visit_cnt[v] -1]) else: self.dp1[v] = sum(max(t) for t in zip(self.dp0[self.tree[v]], self.dp1[self.tree[v]])) self.dp0[v] = 1 + sum([max(t[0] -1, t[1]) for t in zip(self.dp0[self.tree[v]], self.dp1[self.tree[v]])]) return def ans(self): for v in self.top_root: self.dns(v) return sum([max(t) for t in zip(self.dp0[self.top_root], self.dp1[self.top_root])]) N=int(input()) tree = [[] for i in range(N)] for i in range(N -1): e = list(map(int, input().split())) tree[e[0] -1].append(e[1] -1) print(sol(N, tree).ans())