結果

問題 No.1098 LCAs
ユーザー toyuzukotoyuzuko
提出日時 2020-08-08 00:06:59
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,451 ms / 2,000 ms
コード長 1,547 bytes
コンパイル時間 113 ms
コンパイル使用メモリ 12,016 KB
実行使用メモリ 90,068 KB
最終ジャッジ日時 2023-10-25 06:28:07
合計ジャッジ時間 19,574 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
10,216 KB
testcase_01 AC 30 ms
10,216 KB
testcase_02 AC 29 ms
10,216 KB
testcase_03 AC 30 ms
10,216 KB
testcase_04 AC 30 ms
10,216 KB
testcase_05 AC 29 ms
10,216 KB
testcase_06 AC 30 ms
10,216 KB
testcase_07 AC 28 ms
10,216 KB
testcase_08 AC 29 ms
10,216 KB
testcase_09 AC 28 ms
10,216 KB
testcase_10 AC 29 ms
10,216 KB
testcase_11 AC 29 ms
10,216 KB
testcase_12 AC 29 ms
10,216 KB
testcase_13 AC 32 ms
10,568 KB
testcase_14 AC 32 ms
10,568 KB
testcase_15 AC 33 ms
10,568 KB
testcase_16 AC 33 ms
10,568 KB
testcase_17 AC 33 ms
10,540 KB
testcase_18 AC 1,323 ms
77,852 KB
testcase_19 AC 1,327 ms
77,852 KB
testcase_20 AC 1,321 ms
77,852 KB
testcase_21 AC 1,331 ms
77,852 KB
testcase_22 AC 1,327 ms
77,848 KB
testcase_23 AC 1,127 ms
67,576 KB
testcase_24 AC 1,153 ms
67,572 KB
testcase_25 AC 1,061 ms
66,612 KB
testcase_26 AC 1,105 ms
79,176 KB
testcase_27 AC 1,097 ms
79,164 KB
testcase_28 AC 1,438 ms
90,068 KB
testcase_29 AC 1,451 ms
90,040 KB
testcase_30 AC 1,409 ms
90,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

    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)
        self.size = [1 for _ in range(self.n)]
        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)
        for node in self.order[::-1]:
            for adj in self.tree[node]:
                if self.parent[node] == adj:
                    continue
                self.size[node] += self.size[adj]

import sys
input = sys.stdin.readline

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

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

for node in range(N):
    size_sum = 0
    size_sq_sum = 0
    for adj in t.tree[node]:
        if adj == t.parent[node]:
            continue
        size_sum += t.size[adj]
        size_sq_sum += t.size[adj]**2
    print(size_sum**2 - size_sq_sum + size_sum * 2 + 1)
0