luogu#P8004. Welcome to Lunatic City
Welcome to Lunatic City
Problem Description
Country M consists of cities and bidirectional high-speed railways. The 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 . 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 to city , , is defined as the number of cities the train passes through (including the destination but excluding the start).
Now Country M has designated important cities . Please place at most pairs of portals in a proper way so that the sum of distances from the capital city to these cities is minimized, i.e., minimize , 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 portals on a single railway, otherwise you will be judged WA.)
Input Format
The first line contains an integer , the number of test cases.
For each test case:
The first line contains three integers , , and , representing the number of cities, the length of , and the maximum number of portal pairs that can be placed.
The next lines: the -th line contains two positive integers , indicating that the -th railway connects these two cities.
The next line contains integers representing .
Output Format
For each test case:
The first line contains one integer, the minimum sum of distances.
The next lines: the -th line starts with an integer , the number of portals on the -th railway. Then follow pairs of integers , describing in order the portal id and its orientation on the direction from to . Here means the front side faces city , and means the front side faces city .
Portal ids are , and each id must appear exactly twice.
For each test case, the total number of portal pairs must not exceed , i.e., .
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 , the green ones have id , the blue ones have id , and the purple ones have id . The arrows indicate the direction that the front side of each portal faces. The bold nodes are important cities.
The shortest path from city to city is as follows:

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

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

Finally, enter from the front side of the purple portal and arrive at city . Along the way, excluding city , it passes through cities , so under this portal placement .
Also . It can be proven that there is no placement that makes smaller.
Constraints
This problem uses bundled testdata.
| Subtask ID | Score | Special Constraints |
|---|---|---|
| , | ||
| , | ||
| and there are at most five test cases with , | ||
| and there are at most five test cases with , | ||
For all testdata, it is guaranteed that , , , , , and all are distinct.
Translated by ChatGPT 5