結果

問題 No.399 動的な領主
ユーザー maspymaspy
提出日時 2020-04-22 17:20:37
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,484 ms / 2,000 ms
コード長 2,820 bytes
コンパイル時間 104 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 72,220 KB
最終ジャッジ日時 2024-04-20 02:00:00
合計ジャッジ時間 15,918 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
11,008 KB
testcase_01 AC 28 ms
11,008 KB
testcase_02 AC 32 ms
11,136 KB
testcase_03 AC 33 ms
11,136 KB
testcase_04 AC 38 ms
11,520 KB
testcase_05 AC 130 ms
16,500 KB
testcase_06 AC 1,484 ms
71,028 KB
testcase_07 AC 1,421 ms
70,484 KB
testcase_08 AC 1,448 ms
70,736 KB
testcase_09 AC 1,418 ms
69,008 KB
testcase_10 AC 40 ms
11,520 KB
testcase_11 AC 118 ms
16,420 KB
testcase_12 AC 1,321 ms
66,716 KB
testcase_13 AC 1,300 ms
66,636 KB
testcase_14 AC 1,024 ms
66,740 KB
testcase_15 AC 1,075 ms
71,600 KB
testcase_16 AC 1,167 ms
72,220 KB
testcase_17 AC 1,398 ms
66,884 KB
testcase_18 AC 1,414 ms
68,968 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