luogu#P8975. 『GROI-R1』 湖底之城
『GROI-R1』 湖底之城
Background
That year, you and I were still flawless teenagers.
In the quiet backyard at night, we laughed and talked about life without any worries.
— Missing those days with no suspicion at all.
Problem Description
Yue, Ling, and Ying are wandering in the City at the Bottom of the Lake. The roads there are complicated and form a tree with nodes.
Each of them has a counter, all starting at . Whenever they arrive at a node, Yue and Ying’s counters will automatically increase by the weight of the edge they just walked through, while Ling’s counter will increase by exactly . Also, during the whole process, they never pass through any node more than once.
Because their counters cannot store very large values, when Ling’s counter is a multiple of the “Lake Number” , Yue may subtract the value of Ying’s counter from her own counter, and then Ling and Ying’s counters will both be reset to zero immediately.
Qi does not know their starting point and ending point, so for every pair of start and end nodes, the naturally strong-at-calculation Qi computed the minimum possible value that could appear on Yue’s counter at the ending node. Qi denotes this value as , meaning they walked from node to node . However, Qi believes that without the red spider lily, Han surely cannot compute these answers. So he asks Han to solve an easier problem. Qi gives Han a sequence of length , and asks Han to compute for every node .
Formal Statement
Given a tree with nodes and a positive integer . Each edge has an integer weight .
We define as the minimum value of when that can be obtained after performing the operation extension several times on , where initially and .
An extension is defined as performing the following operations in order:
-
Choose any edge such that , then set ;
-
If , you may set .
In particular, in each extension, you cannot choose a node that has been chosen before.
Given the sequence , for every node , find .
Input Format
The first line contains three integers , meaning the tree has nodes labeled , the “Lake Number” is , and the length of sequence is .
The next lines each contain three integers , meaning there is an edge connecting nodes and with weight .
The next line contains integers representing , i.e. the starting nodes.
Output Format
Let . You need to output , where denotes the absolute value of .
6 2 2
1 2 -2
1 3 1
1 4 2
2 5 -3
2 6 10
1 5
4
Hint
Sample Explanation

-
If they start from node , first we have . When they walk to nodes , the values on Yue’s counter are respectively, so . When they walk to nodes , the values on Yue’s counter are respectively. Since Ling’s counter is at this time, which is a multiple of , Yue may choose to subtract Ying’s counter from her counter. It is not hard to get .
-
If they start from node , similarly we can get $f(5,5)=0,f(5,2)=-3,f(5,6)=0,f(5,1)=-5,f(5,4)=-3,f(5,3)=-4$.
Therefore, . We can compute that .
Constraints
This problem uses bundled testdata.
| Subtask ID | Constraints | Special Property | Score |
|---|---|---|---|
| , | |||
| , | |||
| ,, | |||
| , | |||
| , | Yes | ||
Special property: it is guaranteed that the tree degenerates into a chain.
For of the data, , , , .
Translated by ChatGPT 5