結果

問題 No.806 木を道に
ユーザー toyuzukotoyuzuko
提出日時 2020-05-08 22:11:10
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 268 ms / 2,000 ms
コード長 1,117 bytes
コンパイル時間 73 ms
コンパイル使用メモリ 10,952 KB
実行使用メモリ 42,880 KB
最終ジャッジ日時 2023-09-17 02:57:52
合計ジャッジ時間 4,054 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
8,200 KB
testcase_01 AC 12 ms
8,336 KB
testcase_02 AC 13 ms
8,220 KB
testcase_03 AC 13 ms
8,316 KB
testcase_04 AC 15 ms
8,340 KB
testcase_05 AC 16 ms
8,224 KB
testcase_06 AC 13 ms
8,228 KB
testcase_07 AC 13 ms
8,132 KB
testcase_08 AC 13 ms
8,136 KB
testcase_09 AC 13 ms
7,860 KB
testcase_10 AC 58 ms
15,488 KB
testcase_11 AC 52 ms
14,744 KB
testcase_12 AC 216 ms
38,084 KB
testcase_13 AC 147 ms
29,228 KB
testcase_14 AC 201 ms
36,040 KB
testcase_15 AC 217 ms
38,200 KB
testcase_16 AC 77 ms
18,548 KB
testcase_17 AC 187 ms
33,684 KB
testcase_18 AC 29 ms
10,708 KB
testcase_19 AC 59 ms
15,748 KB
testcase_20 AC 181 ms
33,700 KB
testcase_21 AC 106 ms
22,216 KB
testcase_22 AC 268 ms
42,880 KB
testcase_23 AC 261 ms
42,508 KB
testcase_24 AC 123 ms
27,024 KB
testcase_25 AC 163 ms
36,652 KB
testcase_26 AC 67 ms
18,528 KB
testcase_27 AC 209 ms
41,424 KB
testcase_28 AC 19 ms
9,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Tree():
    def __init__(self, n, edge):
        self.n = n
        self.tree = [[] for _ in range(n)]
        for e in edge:
            self.tree[e[0] - 1].append(e[1] - 1)
            self.tree[e[1] - 1].append(e[0] - 1)

    def setroot(self, root):
        self.root = root
        self.parent = [None for _ in range(self.n)]
        self.parent[root] = -1
        self.depth = [None for _ in range(self.n)]
        self.depth[root] = 0
        self.order = []
        self.order.append(root)
        stack = [root]
        while stack:
            node = stack.pop()
            for adj in self.tree[node]:
                if self.parent[adj] is None:
                    self.parent[adj] = node
                    self.depth[adj] = self.depth[node] + 1
                    self.order.append(adj)
                    stack.append(adj)

import sys
input = sys.stdin.readline

N = int(input())
E = [tuple(map(int, input().split())) for _ in range(N - 1)]

tree = Tree(N, E)
tree.setroot(0)

res = 0

for node in range(N):
    if len(tree.tree[node]) > 2:
        res += len(tree.tree[node]) - 2

print(res)
0