#P9245. [蓝桥杯 2023 省 B] 景区导游

    ID: 13252 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>2023最近公共祖先 LCA蓝桥杯省赛

[蓝桥杯 2023 省 B] 景区导游

Problem Description

A scenic area has a total of NN attractions, numbered from 11 to NN. There are N1N - 1 bidirectional shuttle routes between attractions, forming a tree structure. Traveling between attractions can only be done by these shuttles and costs a certain amount of time.

Xiaoming is an experienced tour guide in this scenic area. Every day, he takes tourists to visit KK attractions in a fixed order: A1,A2,,AKA_{1}, A_{2}, \ldots, A_{K}. Today, due to time limits, Xiaoming decides to skip exactly one attraction and only visit K1K - 1 attractions in order. Specifically, if Xiaoming chooses to skip AiA_{i}, then he will visit, in order, $A_{1}, A_{2}, \ldots, A_{i-1}, A_{i+1}, \ldots, A_{K}$ (1iK)(1 \leq i \leq K).

For each AiA_{i}, compute how much time Xiaoming needs to spend on shuttle rides between attractions if he skips this attraction.

Input Format

The first line contains 22 integers NN and KK.

The next N1N - 1 lines each contain 33 integers u,v,tu, v, t, indicating that there is a shuttle route between attraction uu and vv, and it takes tt units of time.

The last line contains KK integers A1,A2,,AKA_{1}, A_{2}, \ldots, A_{K}, representing the original planned tour route.

Output Format

Output KK integers. The ii-th integer represents the time spent on shuttles after skipping AiA_{i}.

6 4
1 2 1
1 3 1
3 4 2
3 5 2
4 6 3
2 6 5 1
10 7 13 14

Hint

[Sample Explanation]

The original route is 26512 \rightarrow 6 \rightarrow 5 \rightarrow 1.

When skipping 22, the route is 6516 \rightarrow 5 \rightarrow 1. Here, 656 \rightarrow 5 costs 3+2+2=73 + 2 + 2 = 7, and 515 \rightarrow 1 costs 2+1=32 + 1 = 3, so the total time is 1010.

When skipping 66, the route is 2512 \rightarrow 5 \rightarrow 1. Here, 252 \rightarrow 5 costs 1+1+2=41 + 1 + 2 = 4, and 515 \rightarrow 1 costs 2+1=32 + 1 = 3, so the total time is 77.

When skipping 55, the route is 2612 \rightarrow 6 \rightarrow 1. Here, 262 \rightarrow 6 costs 1+1+2+3=71 + 1 + 2 + 3 = 7, and 616 \rightarrow 1 costs 3+2+1=63 + 2 + 1 = 6, so the total time is 1313.

When skipping 11, the route is 2652 \rightarrow 6 \rightarrow 5. Here, 262 \rightarrow 6 costs 1+1+2+3=71 + 1 + 2 + 3 = 7, and 656 \rightarrow 5 costs 3+2+2=73 + 2 + 2 = 7, so the total time is 1414.

[Constraints and Notes for Test Cases]

For 20%20\% of the testdata, 2KN1002 \leq K \leq N \leq 100.

For 40%40\% of the testdata, 2KN1042 \leq K \leq N \leq 10^{4}.

For 100%100\% of the testdata, 2KN1052 \leq K \leq N \leq 10^{5}, 1u,v,AiN1 \leq u, v, A_{i} \leq N, 1t1051 \leq t \leq 10^{5}. It is guaranteed that all AiA_{i} are pairwise distinct.

Lanqiao Cup 2023 Provincial Contest B Group, Problem I.

Translated by ChatGPT 5