結果

問題 No.399 動的な領主
ユーザー mkawa2mkawa2
提出日時 2023-01-12 10:42:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 407 ms / 2,000 ms
コード長 2,750 bytes
コンパイル時間 157 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 108,080 KB
最終ジャッジ日時 2024-06-02 08:42:17
合計ジャッジ時間 5,979 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,992 KB
testcase_01 AC 39 ms
52,864 KB
testcase_02 AC 38 ms
53,120 KB
testcase_03 AC 38 ms
53,504 KB
testcase_04 AC 92 ms
74,368 KB
testcase_05 AC 165 ms
80,612 KB
testcase_06 AC 402 ms
105,856 KB
testcase_07 AC 399 ms
106,276 KB
testcase_08 AC 407 ms
106,368 KB
testcase_09 AC 389 ms
106,368 KB
testcase_10 AC 96 ms
76,980 KB
testcase_11 AC 147 ms
79,328 KB
testcase_12 AC 360 ms
105,344 KB
testcase_13 AC 336 ms
105,140 KB
testcase_14 AC 215 ms
106,612 KB
testcase_15 AC 228 ms
106,488 KB
testcase_16 AC 260 ms
108,080 KB
testcase_17 AC 393 ms
105,528 KB
testcase_18 AC 399 ms
106,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(1000005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = (1 << 63)-1
# inf = (1 << 31)-1
md = 10**9+7
# md = 998244353

class LCA:
    def __init__(self, parents, depth):
        self.n = len(parents)
        self.parents = parents
        self.depth = depth
        self.max_level = max(self.depth).bit_length()
        self.ancestor = [self.parents]+[[-1]*self.n for _ in range(self.max_level)]
        row0 = self.ancestor[0]
        for lv in range(self.max_level):
            row1 = self.ancestor[lv+1]
            for u in range(self.n):
                if row0[u] == -1: continue
                row1[u] = row0[row0[u]]
            row0 = row1

    def anc(self, u, v):
        diff = self.depth[u]-self.depth[v]
        if diff < 0: u, v = v, u
        diff = abs(diff)
        u = self.up(u, diff)
        if u == v: return u
        for lv in range(self.depth[u].bit_length()-1, -1, -1):
            anclv = self.ancestor[lv]
            if anclv[u] != anclv[v]: u, v = anclv[u], anclv[v]
        return self.parents[u]

    def up(self, u, dist):
        lv = 0
        while dist and u != -1:
            if dist & 1: u = self.ancestor[lv][u]
            lv, dist = lv+1, dist >> 1
        return u

n=II()
to=[[] for _ in range(n)]
for _ in range(n-1):
    u,v=LI1()
    to[u].append(v)
    to[v].append(u)

parent, depth, vis = [-1]*n, [0]*n, [0]*n

def dfs(root):
    uu = []
    vis[root] = 1
    stack = [root]
    while stack:
        iu = stack.pop()
        i, u = divmod(iu, n)
        if i == 0:
            uu.append(u)
        # care to's format
        while i < len(to[u]) and vis[to[u][i]]: i += 1
        if i < len(to[u]):
            stack.append((i+1)*n+u)
            # care to's format
            v = to[u][i]
            vis[v] = 1
            stack.append(v)
            parent[v] = u
            depth[v] = depth[u]+1

    return uu

uu=dfs(0)
lca=LCA(parent,depth)
dp=[0]*(n+1)
for _ in range(II()):
    u,v=LI1()
    a=lca.anc(u,v)
    dp[u]+=1
    dp[v]+=1
    dp[a]-=1
    dp[parent[a]]-=1

for u in uu[::-1]:
    v=parent[u]
    dp[v]+=dp[u]

ans=0
for c in dp[:-1]:
    ans+=c*(c+1)//2

print(ans)
0