結果

問題 No.901 K-ary εxtrεεmε
ユーザー 👑 rin204rin204
提出日時 2022-04-17 16:31:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 746 ms / 3,000 ms
コード長 2,944 bytes
コンパイル時間 184 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 131,004 KB
最終ジャッジ日時 2024-06-07 23:14:46
合計ジャッジ時間 18,508 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 393 ms
131,004 KB
testcase_01 AC 35 ms
52,352 KB
testcase_02 AC 82 ms
76,288 KB
testcase_03 AC 80 ms
76,288 KB
testcase_04 AC 82 ms
76,160 KB
testcase_05 AC 82 ms
76,672 KB
testcase_06 AC 82 ms
76,812 KB
testcase_07 AC 585 ms
123,220 KB
testcase_08 AC 579 ms
123,264 KB
testcase_09 AC 577 ms
123,008 KB
testcase_10 AC 579 ms
123,392 KB
testcase_11 AC 571 ms
122,880 KB
testcase_12 AC 575 ms
123,128 KB
testcase_13 AC 566 ms
123,008 KB
testcase_14 AC 580 ms
123,176 KB
testcase_15 AC 567 ms
123,436 KB
testcase_16 AC 571 ms
123,136 KB
testcase_17 AC 721 ms
124,088 KB
testcase_18 AC 693 ms
123,220 KB
testcase_19 AC 680 ms
123,264 KB
testcase_20 AC 746 ms
123,944 KB
testcase_21 AC 709 ms
122,880 KB
testcase_22 AC 620 ms
125,044 KB
testcase_23 AC 587 ms
124,252 KB
testcase_24 AC 591 ms
124,044 KB
testcase_25 AC 590 ms
124,284 KB
testcase_26 AC 595 ms
124,904 KB
testcase_27 AC 671 ms
123,264 KB
testcase_28 AC 667 ms
123,008 KB
testcase_29 AC 651 ms
122,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class WeightedLCA:
    def __init__(self, n, edges, e, ope, root = 0):
        self.n = n
        self.e = e
        self.ope = ope
        self.logn = (n - 1).bit_length()
        self.root = root
        self.depth = [-1] * self.n
        self.parent = [[-1] * n for _ in range(self.logn)]
        self.weight = [[-1] * n for _ in range(self.logn)]
        self.dfs(edges)
        self.doubling()
    
    def dfs(self, edges):
        stack = [self.root]
        self.depth[self.root] = 0
        while stack:
            pos = stack.pop()
            for npos, w in edges[pos]:
                if self.depth[npos] != -1:
                    continue
                self.depth[npos] = self.depth[pos] + 1
                self.parent[0][npos] = pos
                self.weight[0][npos] = w
                stack.append(npos)
    
    def doubling(self):
        for i in range(1, self.logn):
            for j in range(self.n):
                if self.parent[i - 1][j] != -1:
                    p = self.parent[i - 1][j]
                    self.parent[i][j] = self.parent[i - 1][p]
                    self.weight[i][j] = self.ope(self.weight[i - 1][j], self.weight[i - 1][p])
    
    def get(self, u, v):
        if self.depth[v] < self.depth[u]:
            u, v = v, u
        du = self.depth[u]
        dv = self.depth[v]
        ret = self.e
        
        for i in range(self.logn):
            if (dv - du) >> i & 1:
                ret = self.ope(ret, self.weight[i][v])
                v = self.parent[i][v]
        
        if u == v:
            return ret, u
            
        for i in range(self.logn - 1, -1, -1):
            pu = self.parent[i][u]
            pv = self.parent[i][v]
            if pu != pv:
                ret = self.ope(ret, self.weight[i][u])
                ret = self.ope(ret, self.weight[i][v])
                u, v = pu, pv
        
        ret = self.ope(ret, self.weight[0][u])
        ret = self.ope(ret, self.weight[0][v])
        u = self.parent[0][u]
        return ret, u

def EulerTour(n, edges, root=0):
    L = [-1] * n
    R = [0] * n
    stack = [(root, 1), (root, 0)]
    ind = 0
    while stack:
        pos, t = stack.pop()
        if t == 0:
            L[pos] = ind
            ind += 1
            for npos, _ in edges[pos]:
                if L[npos] != -1:
                    continue
                stack.append((npos, 1))
                stack.append((npos, 0))
        else:
            R[pos] = ind
    
    return L, R

n = int(input())
edges = [[] for _ in range(n)]
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    edges[u].append((v, w))
    edges[v].append((u, w))
L, _ = EulerTour(n, edges)
lca = WeightedLCA(n, edges, 0, lambda x, y: x + y)
Q = int(input())
for _ in range(Q):
    k, *X = map(int, input().split())
    X.sort(key = lambda x:L[x])
    ans = 0
    for i in range(k):
        ans += lca.get(X[i], X[i - 1])[0]
    print(ans // 2)
0