結果

問題 No.901 K-ary εxtrεεmε
ユーザー mkawa2mkawa2
提出日時 2023-03-21 22:28:22
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 545 ms / 3,000 ms
コード長 2,925 bytes
コンパイル時間 1,348 ms
コンパイル使用メモリ 81,632 KB
実行使用メモリ 111,784 KB
最終ジャッジ日時 2024-09-18 14:33:27
合計ジャッジ時間 16,121 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 340 ms
111,784 KB
testcase_01 AC 38 ms
52,992 KB
testcase_02 AC 96 ms
76,672 KB
testcase_03 AC 88 ms
76,116 KB
testcase_04 AC 92 ms
76,272 KB
testcase_05 AC 91 ms
76,260 KB
testcase_06 AC 102 ms
76,704 KB
testcase_07 AC 457 ms
105,180 KB
testcase_08 AC 453 ms
103,668 KB
testcase_09 AC 498 ms
103,256 KB
testcase_10 AC 472 ms
104,896 KB
testcase_11 AC 480 ms
105,004 KB
testcase_12 AC 470 ms
105,184 KB
testcase_13 AC 478 ms
104,656 KB
testcase_14 AC 493 ms
104,936 KB
testcase_15 AC 470 ms
104,628 KB
testcase_16 AC 482 ms
103,468 KB
testcase_17 AC 484 ms
104,536 KB
testcase_18 AC 476 ms
104,008 KB
testcase_19 AC 468 ms
104,248 KB
testcase_20 AC 475 ms
104,272 KB
testcase_21 AC 495 ms
104,708 KB
testcase_22 AC 528 ms
104,664 KB
testcase_23 AC 524 ms
105,084 KB
testcase_24 AC 543 ms
109,748 KB
testcase_25 AC 545 ms
105,296 KB
testcase_26 AC 533 ms
105,152 KB
testcase_27 AC 399 ms
104,408 KB
testcase_28 AC 382 ms
104,044 KB
testcase_29 AC 381 ms
103,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
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

def dfs(root=0):
    uu = [root]
    dfsOrder[root] = 0
    stack = [(u, root, w) for u, w in to[root]]
    while stack:
        u, p, w = stack.pop()
        if u >= 0 and dfsOrder[u] == -1:
            dist[u] = dist[p]+w
            parent[u] = p
            depth[u] = depth[p]+1
            dfsOrder[u] = len(uu)
            uu.append(u)
            stack.append((-1, u, 0))
            to_par = False
            for v, w in to[u]:
                if v != p or to_par: stack.append((v, u, w))
                if v == p: to_par = True

    return uu

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

parent, depth, dfsOrder = [-1]*n, [0]*n, [-1]*n
dist = [0]*n
dfs()

lca = LCA(parent, depth)

for _ in range(II()):
    k, *xx = LI()
    xx.sort(key=lambda x: dfsOrder[x])
    r = u = xx[0]
    ans = 0
    for x in xx[1:]:
        a = lca.anc(u, x)
        if depth[a] < depth[r]:
            ans += dist[x]+dist[r]-2*dist[a]
            r = a
        else:
            ans += dist[x]-dist[a]
        u = x
    print(ans)
0