luogu#P8004. Welcome to Lunatic City

Welcome to Lunatic City

Problem Description

Country M consists of nn cities and n1n-1 bidirectional high-speed railways. The nn cities can reach each other through the railways.

Because Country M has the most advanced technology, in order to speed up transportation, it can place some paired portals on the railways, with the total number of pairs not exceeding LL. When a train runs on a railway, whenever it encounters a portal, it will be teleported. Each portal has a front side and a back side: if the train enters from the front side, it will exit from the front side of the portal paired with it; if it enters from the back side, it will exit from the back side. A portal can be placed anywhere in the middle of a railway. (If anything in this description is unclear, please refer to the sample explanation.)

Also, since Country M has extremely fast trains, travel time is only spent on stops. Therefore, the distance from city ii to city jj, dis(i,j)dis(i,j), is defined as the number of cities the train passes through (including the destination but excluding the start).

Now Country M has designated mm important cities x1,x2,,xmx_1,x_2,\dots,x_m. Please place at most LL pairs of portals in a proper way so that the sum of distances from the capital city 11 to these cities is minimized, i.e., minimize i=1mdis(1,xi)\sum_{i=1}^m dis(1,x_i), and output a construction. Meanwhile, the placed portals must ensure that all cities are still mutually reachable.

(Due to some properties of the special judge, please do not place more than LL portals on a single railway, otherwise you will be judged WA.)

Input Format

The first line contains an integer TT, the number of test cases.

For each test case:

The first line contains three integers nn, mm, and LL, representing the number of cities, the length of xx, and the maximum number of portal pairs that can be placed.

The next n1n-1 lines: the ii-th line contains two positive integers ui,viu_i, v_i, indicating that the ii-th railway connects these two cities.

The next line contains mm integers representing xx.

Output Format

For each test case:

The first line contains one integer, the minimum sum of distances.

The next n1n-1 lines: the ii-th line starts with an integer numnum, the number of portals on the ii-th railway. Then follow numnum pairs of integers x,fx, f, describing in order the portal id and its orientation on the direction from uiu_i to viv_i. Here f=0f=0 means the front side faces city uiu_i, and f=1f=1 means the front side faces city viv_i.

Portal ids are 1num21\sim \frac{\sum num}{2}, and each id must appear exactly twice.

For each test case, the total number of portal pairs must not exceed LL, i.e., num2L\frac{\sum num}{2}\le L.

2
4 3 100
1 2
2 3
3 4
2 3 4
5 2 100
1 2
2 3
3 4
3 5
4 5
6
0
0
0
5
1 1 0
3 4 0 3 1 1 1
1 2 0
3 2 1 3 0 4 1

Hint

Sample Explanation

For the second test case in the sample, the shape of the tree and the portal placement are as follows:

The red portals have id 11, the green ones have id 22, the blue ones have id 33, and the purple ones have id 44. The arrows indicate the direction that the front side of each portal faces. The bold nodes are important cities.

The shortest path from city 11 to city 55 is as follows:

First, starting from city 11, enter the front side of the red portal and arrive at city 33.

Then start from city 33, enter from the front side of the green portal, arrive at a segment of railway between the green and blue portals on the road 353-5, then enter the front side of the blue portal, arrive at a segment of railway between the blue and red portals on the road 232-3, then enter the back side of the red portal, and finally arrive at city 22.

Finally, enter from the front side of the purple portal and arrive at city 55. Along the way, excluding city 11, it passes through cities 3,2,53, 2, 5, so under this portal placement dis(1,5)=3dis(1,5)=3.

Also dis(1,4)=2dis(1,4)=2. It can be proven that there is no placement that makes dis(1,4)+dis(1,5)dis(1,4)+dis(1,5) smaller.

Constraints

This problem uses bundled testdata.

Subtask ID Score Special Constraints
00 1515 n9n\le 9L=100L=100
11 1010 m=n1m=n-1L=5nL=5n
22 2020 n70n\le 70 and there are at most five test cases with n>30n>30, L=n2L=n^2
33 n1000n\le 1000 and there are at most five test cases with n>100n>100, L=100nL=100n
44 3030 L=5nL=5n
55 L=nL=n

For all testdata, it is guaranteed that 1T1001\le T\le 100, 1n1051\le n\le 10^5, 1n5×1051\le \sum n\le 5\times 10^5, 0m<n0\le m<n, 2xin2\le x_i\le n, and all xix_i are distinct.

Translated by ChatGPT 5