#P9245. [蓝桥杯 2023 省 B] 景区导游
[蓝桥杯 2023 省 B] 景区导游
Problem Description
A scenic area has a total of attractions, numbered from to . There are 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 attractions in a fixed order: . Today, due to time limits, Xiaoming decides to skip exactly one attraction and only visit attractions in order. Specifically, if Xiaoming chooses to skip , then he will visit, in order, $A_{1}, A_{2}, \ldots, A_{i-1}, A_{i+1}, \ldots, A_{K}$ .
For each , compute how much time Xiaoming needs to spend on shuttle rides between attractions if he skips this attraction.
Input Format
The first line contains integers and .
The next lines each contain integers , indicating that there is a shuttle route between attraction and , and it takes units of time.
The last line contains integers , representing the original planned tour route.
Output Format
Output integers. The -th integer represents the time spent on shuttles after skipping .
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 .
When skipping , the route is . Here, costs , and costs , so the total time is .
When skipping , the route is . Here, costs , and costs , so the total time is .
When skipping , the route is . Here, costs , and costs , so the total time is .
When skipping , the route is . Here, costs , and costs , so the total time is .
[Constraints and Notes for Test Cases]
For of the testdata, .
For of the testdata, .
For of the testdata, , , . It is guaranteed that all are pairwise distinct.
Lanqiao Cup 2023 Provincial Contest B Group, Problem I.
Translated by ChatGPT 5