結果

問題 No.399 動的な領主
ユーザー mkawa2mkawa2
提出日時 2023-01-12 10:42:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 581 ms / 2,000 ms
コード長 2,750 bytes
コンパイル時間 323 ms
コンパイル使用メモリ 86,848 KB
実行使用メモリ 110,224 KB
最終ジャッジ日時 2023-08-24 19:08:03
合計ジャッジ時間 8,204 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
71,228 KB
testcase_01 AC 78 ms
71,364 KB
testcase_02 AC 79 ms
71,228 KB
testcase_03 AC 80 ms
71,076 KB
testcase_04 AC 118 ms
77,500 KB
testcase_05 AC 213 ms
81,412 KB
testcase_06 AC 543 ms
107,168 KB
testcase_07 AC 581 ms
106,280 KB
testcase_08 AC 561 ms
108,016 KB
testcase_09 AC 537 ms
106,260 KB
testcase_10 AC 138 ms
77,768 KB
testcase_11 AC 195 ms
81,608 KB
testcase_12 AC 451 ms
105,676 KB
testcase_13 AC 445 ms
105,744 KB
testcase_14 AC 273 ms
108,588 KB
testcase_15 AC 288 ms
108,340 KB
testcase_16 AC 327 ms
110,224 KB
testcase_17 AC 544 ms
105,516 KB
testcase_18 AC 539 ms
106,304 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