結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
11,008 KB
testcase_01 AC 30 ms
11,008 KB
testcase_02 AC 35 ms
11,008 KB
testcase_03 AC 31 ms
11,136 KB
testcase_04 AC 39 ms
11,392 KB
testcase_05 AC 135 ms
16,248 KB
testcase_06 AC 1,574 ms
70,904 KB
testcase_07 AC 1,592 ms
70,228 KB
testcase_08 AC 1,559 ms
70,732 KB
testcase_09 AC 1,555 ms
68,872 KB
testcase_10 AC 43 ms
11,520 KB
testcase_11 AC 133 ms
16,424 KB
testcase_12 AC 1,427 ms
66,844 KB
testcase_13 AC 1,437 ms
66,504 KB
testcase_14 AC 1,082 ms
66,692 KB
testcase_15 AC 1,161 ms
71,476 KB
testcase_16 AC 1,252 ms
72,096 KB
testcase_17 AC 1,495 ms
66,880 KB
testcase_18 AC 1,535 ms
68,836 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