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 nn nodes.

Each of them has a counter, all starting at 00. 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 11. 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” pp, 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 f(u,v)f(u,v), meaning they walked from node uu to node vv. 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 ss of length mm, and asks Han to compute mini=1m{f(si,u)}\min\limits_{i=1}^m\{f(s_i,u)\} for every node uu.

Formal Statement

Given a tree (V,E)(V,E) with nn nodes and a positive integer pp. Each edge has an integer weight wiw_i.

We define f(s,v)f(s,v) as the minimum value of aa when u=vu=v that can be obtained after performing the operation extension several times on u,a,b,cu,a,b,c, where initially usu\gets s and a,b,c0a,b,c\gets0.

An extension is defined as performing the following operations in order:

  • Choose any edge (u,v,w)E(u',v,w)\in E such that u=uu=u', then set uv,aa+w,bb+1,cc+wu\gets v,a\gets a+w,b\gets b+1,c\gets c+w;

  • If pbp\mid b, you may set aac,b0,c0a\gets a-c,b\gets 0,c\gets0.

In particular, in each extension, you cannot choose a node that has been chosen before.

Given the sequence {sm}\{s_m\}, for every node uu, find mini=1m{f(si,u)}\min\limits_{i=1}^m\{f(s_i,u)\}.

Input Format

The first line contains three integers n,m,pn,m,p, meaning the tree has nn nodes labeled 1n1\sim n, the “Lake Number” is pp, and the length of sequence ss is mm.

The next n1n-1 lines each contain three integers u,v,wu,v,w, meaning there is an edge connecting nodes uu and vv with weight ww.

The next line contains mm integers representing s1ms_{1\sim m}, i.e. the mm starting nodes.

Output Format

Let ansu=mini=1m{f(si,u)}ans_u=\min\limits_{i=1}^m\{f(s_i,u)\}. You need to output xori=1nansi\text{xor}_{i=1}^n |ans_i|, where a|a| denotes the absolute value of aa.

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 11, first we have f(1,1)=0f(1,1)=0. When they walk to nodes 2,3,42,3,4, the values on Yue’s counter are 2,1,2-2,1,2 respectively, so f(1,2)=2,f(1,3)=1,f(1,4)=2f(1,2)=-2,f(1,3)=1,f(1,4)=2. When they walk to nodes 5,65,6, the values on Yue’s counter are 5,8-5,8 respectively. Since Ling’s counter is 22 at this time, which is a multiple of pp, Yue may choose to subtract Ying’s counter from her counter. It is not hard to get f(1,5)=5,f(1,6)=0f(1,5)=-5,f(1,6)=0.

  • If they start from node 55, 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, {ansn}={5,3,4,3,5,0}\{ans_n\}=\{-5,-3,-4,-3,-5,0\}. We can compute that xori=1nansi=4\text{xor}_{i=1}^n |ans_i|=4.

Constraints

This problem uses bundled testdata.

Subtask ID Constraints Special Property Score
Subtask1\text{Subtask1} mn100m\le n\le100p20p\le20 1010
Subtask2\text{Subtask2} mn103m\le n\le10^3p100p\le100 1515
Subtask3\text{Subtask3} n105n\le10^5p100p\le100m=1m=1 1010
Subtask4\text{Subtask4} mn105m\le n\le10^5p=1p=1 2020
Subtask5\text{Subtask5} mn105m\le n\le10^5p100p\le100 Yes 1010
Subtask6\text{Subtask6} 3535

Special property: it is guaranteed that the tree degenerates into a chain.

For 100%100\% of the data, 1mn1051\le m\le n\le10^5, 1p1001\le p\le100, 104w104-10^4\le w\le10^4, 1u,v,sin1\le u,v,s_i\le n.

Translated by ChatGPT 5