結果

問題 No.763 Noelちゃんと木遊び
ユーザー silversmithsilversmith
提出日時 2019-05-05 15:50:16
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 692 ms / 2,000 ms
コード長 1,691 bytes
コンパイル時間 210 ms
コンパイル使用メモリ 10,804 KB
実行使用メモリ 56,084 KB
最終ジャッジ日時 2023-09-07 07:44:26
合計ジャッジ時間 15,449 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 625 ms
56,084 KB
testcase_01 AC 307 ms
36,492 KB
testcase_02 AC 571 ms
45,604 KB
testcase_03 AC 423 ms
40,596 KB
testcase_04 AC 341 ms
37,276 KB
testcase_05 AC 410 ms
39,988 KB
testcase_06 AC 678 ms
49,044 KB
testcase_07 AC 665 ms
48,312 KB
testcase_08 AC 450 ms
41,000 KB
testcase_09 AC 336 ms
37,024 KB
testcase_10 AC 209 ms
32,644 KB
testcase_11 AC 692 ms
49,468 KB
testcase_12 AC 631 ms
47,380 KB
testcase_13 AC 625 ms
47,052 KB
testcase_14 AC 565 ms
45,104 KB
testcase_15 AC 433 ms
40,184 KB
testcase_16 AC 181 ms
31,464 KB
testcase_17 AC 425 ms
40,068 KB
testcase_18 AC 673 ms
49,052 KB
testcase_19 AC 631 ms
47,528 KB
testcase_20 AC 635 ms
47,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import numpy as np
class sol(object):
    def __init__(self, N, tree):
        self.N = N
        self.tree = tree
        self.dp0 = [0] * N
        self.dp1 = [0] * N
        self.visit = [False] * N
        self.parent = np.array([-1] * N, dtype=int)
    
    def dns(self, cur):
        h = []
        h.append(cur)
        while len(h) > 0:
            v = h.pop()
            if len(self.tree[v]) == 1 and self.parent[v] == self.tree[v][0]:
                self.dp1[v] = 0
                self.dp0[v] = 1
                self.dp1[self.parent[v]] += 1
                self.dp0[self.parent[v]] += 0
            elif self.visit[v] == False:
                h.append(v)
                for child in self.tree[v]:
                    if child != self.parent[v]:
                        h.append(child)
                        self.parent[child] = v
            else:
                self.dp0[v] += 1
                if self.parent[v] != -1:
                    self.dp1[self.parent[v]] += max(self.dp0[v], self.dp1[v])
                    self.dp0[self.parent[v]] += max(self.dp0[v] - 1, self.dp1[v])
            self.visit[v] = True

        return

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


def main():
    N=int(input())
    tree = [[] for i in range(N)]
    for _ 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