結果

問題 No.399 動的な領主
ユーザー maspymaspy
提出日時 2020-04-22 17:20:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 485 ms / 2,000 ms
コード長 2,820 bytes
コンパイル時間 340 ms
コンパイル使用メモリ 82,496 KB
実行使用メモリ 139,800 KB
最終ジャッジ日時 2024-04-20 01:59:27
合計ジャッジ時間 7,363 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
52,864 KB
testcase_01 AC 37 ms
52,608 KB
testcase_02 AC 39 ms
53,120 KB
testcase_03 AC 40 ms
53,472 KB
testcase_04 AC 92 ms
76,736 KB
testcase_05 AC 181 ms
82,276 KB
testcase_06 AC 484 ms
135,232 KB
testcase_07 AC 469 ms
133,068 KB
testcase_08 AC 478 ms
136,668 KB
testcase_09 AC 467 ms
136,784 KB
testcase_10 AC 103 ms
77,532 KB
testcase_11 AC 175 ms
82,644 KB
testcase_12 AC 449 ms
139,800 KB
testcase_13 AC 436 ms
136,916 KB
testcase_14 AC 253 ms
139,008 KB
testcase_15 AC 281 ms
138,792 KB
testcase_16 AC 296 ms
132,584 KB
testcase_17 AC 485 ms
134,532 KB
testcase_18 AC 479 ms
136,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import itertools


def EulerTour(graph, root=1):
    N = len(graph) - 1
    parent = [0] * (N + 1)
    stack = [-root, root]
    tour = []
    ind_L = [0] * (N + 1)
    ind_R = [0] * (N + 1)
    i = -1
    while stack:
        v = stack.pop()
        tour.append(v)
        i += 1
        if v > 0:
            ind_L[v] = i
            p = parent[v]
            for w in graph[v]:
                if w == p:
                    continue
                parent[w] = v
                stack.append(-w)
                stack.append(w)
        else:
            ind_R[-v] = i
    return tour, ind_L, ind_R, parent


class SegTree:
    """ segment tree with point modification and range product access. """
    data_unit = None
    @classmethod
    def data_f(cls, x, y):
        if x is None:
            return y
        if y is None:
            return x
        if depth[x] > depth[y]:
            return y
        else:
            return x

    def __init__(self, N):
        self.N = N
        self.data = [self.data_unit] * (N + N)

    def build(self, raw_data):
        data = self.data
        f = self.data_f
        N = self.N
        data[N:] = raw_data[:]
        for i in range(N - 1, 0, -1):
            data[i] = f(data[i << 1], data[i << 1 | 1])

    def set_val(self, i, x):
        data = self.data
        f = self.data_f
        i += self.N
        data[i] = x
        while i > 1:
            i >>= 1
            data[i] = f(data[i << 1], data[i << 1 | 1])

    def fold(self, L, R):
        """ compute for [L, R) """
        vL = vR = self.data_unit
        data = self.data
        f = self.data_f
        L += self.N
        R += self.N
        while L < R:
            if L & 1:
                vL = f(vL, data[L])
                L += 1
            if R & 1:
                R -= 1
                vR = f(data[R], vR)
            L >>= 1
            R >>= 1
        return f(vL, vR)


N = int(readline())
graph = [[] for _ in range(N + 1)]
for _ in range(N - 1):
    u, v = map(int, readline().split())
    graph[u].append(v)
    graph[v].append(u)

tour, ind_L, ind_R, parent = EulerTour(graph)
depth = tuple(itertools.accumulate(1 if x > 0 else -1 for x in tour))

seg = SegTree(len(depth))
seg.build(range(len(depth)))

imos = [0] * (N + 1)
Q = int(readline())
m = map(int, read().split())
for a, b in zip(m, m):
    i, j = ind_L[a], ind_L[b]
    if i > j:
        i, j = j, i
    k = seg.fold(i, j + 1)
    lca = tour[k]
    if lca < 0:
        lca = parent[-lca]
    imos[a] += 1
    imos[b] += 1
    imos[lca] -= 1
    imos[parent[lca]] -= 1

for v in tour[::-1]:
    if v > 0:
        imos[parent[v]] += imos[v]

answer = sum(x * (x + 1) for x in imos) // 2
print(answer)
0