結果

問題 No.1098 LCAs
ユーザー toyuzukotoyuzuko
提出日時 2020-08-08 00:06:10
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,508 bytes
コンパイル時間 95 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 91,008 KB
最終ジャッジ日時 2024-04-27 10:50:51
合計ジャッジ時間 26,545 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
10,880 KB
testcase_01 AC 34 ms
11,008 KB
testcase_02 AC 30 ms
10,880 KB
testcase_03 AC 30 ms
11,008 KB
testcase_04 AC 30 ms
10,880 KB
testcase_05 AC 29 ms
10,880 KB
testcase_06 AC 29 ms
11,008 KB
testcase_07 AC 29 ms
11,008 KB
testcase_08 AC 29 ms
10,880 KB
testcase_09 AC 30 ms
10,880 KB
testcase_10 AC 30 ms
10,880 KB
testcase_11 AC 29 ms
11,008 KB
testcase_12 AC 30 ms
10,880 KB
testcase_13 AC 36 ms
11,136 KB
testcase_14 AC 38 ms
11,136 KB
testcase_15 AC 37 ms
11,136 KB
testcase_16 AC 38 ms
11,136 KB
testcase_17 AC 37 ms
11,136 KB
testcase_18 AC 1,831 ms
78,720 KB
testcase_19 AC 1,835 ms
78,720 KB
testcase_20 AC 1,847 ms
78,848 KB
testcase_21 AC 1,874 ms
78,720 KB
testcase_22 AC 1,875 ms
78,720 KB
testcase_23 AC 1,567 ms
68,456 KB
testcase_24 AC 1,593 ms
68,448 KB
testcase_25 AC 1,502 ms
67,448 KB
testcase_26 AC 1,579 ms
79,996 KB
testcase_27 AC 1,614 ms
79,928 KB
testcase_28 TLE -
testcase_29 TLE -
testcase_30 TLE -
権限があれば一括ダウンロードができます

ソースコード

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]

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