結果
| 問題 | No.901 K-ary εxtrεεmε |
| コンテスト | |
| ユーザー |
detteiuu
|
| 提出日時 | 2026-01-27 02:21:44 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
AC
|
| 実行時間 | 1,001 ms / 3,000 ms |
| コード長 | 3,674 bytes |
| 記録 | |
| コンパイル時間 | 491 ms |
| コンパイル使用メモリ | 82,684 KB |
| 実行使用メモリ | 199,272 KB |
| 最終ジャッジ日時 | 2026-01-27 02:22:14 |
| 合計ジャッジ時間 | 23,476 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 29 |
ソースコード
from types import GeneratorType
from collections import defaultdict
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
class AuxiliaryTree:
def __init__(self, N, G):
self.N = N
self.G = G
self.IN = [0]*N
self.OUT = [0]*N
self.depth = [0]*N
self.depth2 = [0]*N
self.E = []
self.dfs(0, -1, 0, 0)
self.L = len(self.E)
self.lg = [0]*(self.L+1)
self.make_sparse_table()
@bootstrap
def dfs(self, n, p, d, d2):
self.depth[n] = d
self.depth2[n] = d2
self.IN[n] = len(self.E)
self.E.append(n)
for v, w in self.G[n]:
if v == p:
continue
yield self.dfs(v, n, d+1, d2+w)
self.E.append(n)
self.OUT[n] = len(self.E)
self.E.append(n)
yield
def make_sparse_table(self):
for i in range(2, self.L+1):
self.lg[i] = self.lg[i>>1]+1
self.st = [[self.L]*(self.L-(1<<i)+1) for i in range(self.lg[self.L]+1)]
self.st[0] = self.E[:]
b = 1
for i in range(self.lg[self.L]):
for j in range(self.L-(b<<1)+1):
self.st[i+1][j] = (self.st[i][j] if self.depth[self.st[i][j]] <= self.depth[self.st[i][j+b]] else self.st[i][j+b])
b <<= 1
def lca(self, u, v):
x, y = self.IN[u], self.IN[v]
l = self.lg[y-x+1]
return self.st[l][x] if self.depth[self.st[l][x]] <= self.depth[self.st[l][y-(1<<l)+1]] else self.st[l][y-(1<<l)+1]
def query(self, v):
nG = defaultdict(list)
V = v[:]
V.sort(key=self.IN.__getitem__)
K = len(V)
stack = [V[0]]
for i in range(K-1):
w = self.lca(V[i], V[i+1])
if w != V[i]:
last = stack.pop()
while stack and self.depth[w] < self.depth[stack[-1]]:
d = abs(self.depth[stack[-1]]-self.depth[last])
d2 = abs(self.depth2[stack[-1]]-self.depth2[last])
nG[stack[-1]].append((last, d, d2))
nG[last].append((stack[-1], d, d2))
last = stack.pop()
if not stack or stack[-1] != w:
stack.append(w)
V.append(w)
d = abs(self.depth[w]-self.depth[last])
d2 = abs(self.depth2[w]-self.depth2[last])
nG[w].append((last, d, d2))
nG[last].append((w, d, d2))
stack.append(V[i+1])
for i in range(len(stack)-1):
d = abs(self.depth[stack[i]]-self.depth[stack[i+1]])
d2 = abs(self.depth2[stack[i]]-self.depth2[stack[i+1]])
nG[stack[i]].append((stack[i+1], d, d2))
nG[stack[i+1]].append((stack[i], d, d2))
return nG, stack[0]
N = int(input())
G = [[] for _ in range(N)]
for _ in range(N-1):
u, v, w = map(int, input().split())
G[u].append((v, w))
G[v].append((u, w))
Q = int(input())
query = [list(map(int, input().split())) for _ in range(Q)]
aux = AuxiliaryTree(N, G)
for i in range(Q):
X = query[i][1:]
nG, _ = aux.query(X)
ans = 0
for n in nG.values():
ans += sum(a[2] for a in n)
print(ans//2)
detteiuu