結果

問題 No.898 tri-βutree
ユーザー maspymaspy
提出日時 2020-02-29 18:49:03
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 2,027 ms / 4,000 ms
コード長 2,112 bytes
コンパイル時間 139 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 153,168 KB
最終ジャッジ日時 2024-04-26 09:19:38
合計ジャッジ時間 39,584 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,524 ms
139,636 KB
testcase_01 AC 513 ms
44,640 KB
testcase_02 AC 491 ms
44,632 KB
testcase_03 AC 492 ms
44,380 KB
testcase_04 AC 495 ms
44,628 KB
testcase_05 AC 506 ms
44,260 KB
testcase_06 AC 500 ms
44,512 KB
testcase_07 AC 1,826 ms
152,524 KB
testcase_08 AC 1,938 ms
152,704 KB
testcase_09 AC 1,825 ms
152,960 KB
testcase_10 AC 1,900 ms
152,704 KB
testcase_11 AC 1,839 ms
152,580 KB
testcase_12 AC 1,901 ms
153,168 KB
testcase_13 AC 1,929 ms
152,880 KB
testcase_14 AC 1,884 ms
152,876 KB
testcase_15 AC 1,795 ms
152,328 KB
testcase_16 AC 2,026 ms
152,584 KB
testcase_17 AC 2,027 ms
152,672 KB
testcase_18 AC 1,902 ms
153,008 KB
testcase_19 AC 1,839 ms
152,708 KB
testcase_20 AC 1,921 ms
152,612 KB
testcase_21 AC 1,867 ms
153,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
# %%
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


# %%
import numpy as np

# %%
N = int(readline())
UVW = tuple(tuple(map(int, readline().split()) for _ in range(N - 1)))
Q = int(readline())
XYZ = np.array(read().split(), np.int64) + 1
X = XYZ[::3]
Y = XYZ[1::3]
Z = XYZ[2::3]


# %%
G = [[] for _ in range(N + 1)]
for u, v, w in UVW:
    G[u + 1].append((v + 1, w))
    G[v + 1].append((u + 1, w))


# %%
def EulerTour(graph, root=1):
    V = len(graph)
    par = [0] * V
    depth = [0] * V
    weight = [0] * V
    depth[root] = 0
    tour = [root]
    st = [root]
    while st:
        x = st[-1]
        if not graph[x]:
            st.pop()
            tour.append(par[x])
            continue
        y, c = graph[x].pop()
        if y == par[x]:
            continue
        par[y] = x
        depth[y] = depth[x] + 1
        weight[y] = weight[x] + c
        st.append(y)
        tour.append(y)
    return par, tour, depth, weight


# %%
par, tour, depth, weight = EulerTour(G)


# %%
Ltour = len(tour)
tour_arr = np.array(tour)
depth_arr = np.array(depth)
tour_d = depth_arr[tour_arr]
idx = np.arange(len(depth))
idx[tour_arr] = np.arange(Ltour)

# %%
sp = np.empty((Ltour.bit_length(), Ltour), np.int32)
sp[0] = np.arange(Ltour)
for n in range(1, Ltour.bit_length()):
    prev, width = sp[n - 1], 1 << (n - 1)
    x = prev[:-width]
    y = prev[width:]
    condition = tour_d[x] > tour_d[y]
    sp[n] = prev
    sp[n, :-width][condition] = y[condition]


# %%
def LCA(A, B):
    AB = np.vstack([A, B]).T
    LR = idx[AB]
    LR.sort(axis=1)
    # [L,R] におけるRmQ
    L = LR[:, 0]
    R = LR[:, 1]
    x = R - L
    n = np.zeros_like(x)  # 2^n <= R-L
    for _ in range(20):
        x >>= 1
        n[x > 0] += 1
    x = sp[n, L]
    y = sp[n, R - (1 << n) + 1]
    return np.where(tour_d[x] < tour_d[y], tour_arr[x], tour_arr[y])


# %%
XY = LCA(X, Y)
XZ = LCA(X, Z)
YZ = LCA(Z, Y)


# %%
A = np.array(weight)
answer = A[X] + A[Y] + A[Z] - A[XY] - A[XZ] - A[YZ]
print('\n'.join(answer.astype(str)))
0