結果

問題 No.763 Noelちゃんと木遊び
ユーザー silversmithsilversmith
提出日時 2019-05-05 01:14:54
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,736 bytes
コンパイル時間 271 ms
コンパイル使用メモリ 86,760 KB
実行使用メモリ 78,696 KB
最終ジャッジ日時 2023-09-05 19:16:59
合計ジャッジ時間 5,390 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import numpy as np
class sol(object):
    def __init__(self, N, tree):
        self.N = N
        self.tree = tree
        self.dp = np.ndarray([N,2], 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 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:
                if len(childs[v]) > 0:
                    t_dp = self.dp[childs[v]]
                    self.dp[v, 1] = np.sum(np.max(t_dp, axis=1))
                    t_dp[:,0] -= 1
                    self.dp[v, 0] = 1 + np.sum(np.max(t_dp, axis=1))
                else:
                    self.dp[v,1] = 0
                    self.dp[v,0] = 1
        return

    def ans(self):
        self.dns(0)
        return max(self.dp[0,0],self.dp[0,1])


def main():
    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())

line_prof = False
if line_prof:
    from line_profiler import LineProfiler
    profiler = LineProfiler()
    profiler.add_function(sol.dns)
    profiler.runcall(main)
    profiler.print_stats()
else:
    main()
0