結果

問題 No.763 Noelちゃんと木遊び
ユーザー silversmithsilversmith
提出日時 2019-05-04 23:35:54
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,499 bytes
コンパイル時間 118 ms
コンパイル使用メモリ 10,892 KB
実行使用メモリ 60,588 KB
最終ジャッジ日時 2023-09-05 16:48:45
合計ジャッジ時間 34,687 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,752 ms
60,588 KB
testcase_01 AC 769 ms
39,460 KB
testcase_02 AC 1,655 ms
52,320 KB
testcase_03 AC 1,155 ms
44,672 KB
testcase_04 AC 873 ms
40,600 KB
testcase_05 AC 1,110 ms
44,616 KB
testcase_06 TLE -
testcase_07 AC 1,913 ms
55,964 KB
testcase_08 AC 1,198 ms
45,824 KB
testcase_09 AC 886 ms
40,344 KB
testcase_10 AC 414 ms
33,732 KB
testcase_11 TLE -
testcase_12 AC 1,792 ms
54,396 KB
testcase_13 AC 1,731 ms
54,196 KB
testcase_14 AC 1,566 ms
51,604 KB
testcase_15 AC 1,163 ms
44,920 KB
testcase_16 AC 308 ms
32,348 KB
testcase_17 AC 1,152 ms
44,840 KB
testcase_18 TLE -
testcase_19 AC 1,817 ms
54,788 KB
testcase_20 AC 1,812 ms
55,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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.parent = np.array([-1] * N, dtype=int)
    
    def dns(self, cur):
        h = []
        h.append(cur)
        childs = [[] for i in range(self.N)]
        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]):
                child = self.tree[v][self.visit_cnt[v] -1]
                if child != self.parent[v]:
                    h.append(v)
                    h.append(child)
                    childs[v].append(child)
                    self.parent[child] = v
                else:
                    h.append(v)
            else:
                self.dp1[v] = sum([max(t) for t in zip(self.dp0[childs[v]], self.dp1[childs[v]])])
                self.dp0[v] = 1 + sum([max(t[0] -1, t[1]) for t in zip(self.dp0[childs[v]], self.dp1[childs[v]])])
        return

    def ans(self):
        self.dns(0)
        return max(self.dp0[0],self.dp1[0])

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)
    tree[e[1] -1].append(e[0] -1)
print(sol(N, tree).ans())
0