luogu#P11111. [ROI 2023] 生产计划 (Day 2)
[ROI 2023] 生产计划 (Day 2)
Background
Translated from ROI 2023 D2T4.
This is an IO interactive problem.
A country is going to create a car production plan.
There are cities in the country, and each city has a factory that participates in car production. The factory in the -th city can hire between and workers.
Some pairs of cities are connected by two-way roads, and the road network forms a tree. Therefore, between any two cities there is exactly one path that does not repeat any city.
A production plan is defined as an integer sequence , where is the number of workers in the -th factory and . After creating the plan, some factories will be chosen as assembly factories, which assemble the cars, while the other factories produce parts. If no two assembly factories are located in cities directly connected by a road, then the plan is valid. Among all possible valid plans, the one that maximizes the total number of workers in the assembly factories is chosen. This maximum is called the efficiency of the plan .
Problem Description
You need to determine whether a plan with efficiency exists. If such a plan exists, you may need to output one.
The process of creating plans consists of two phases.
In the first phase, you are given the values . You must determine whether there exists a plan whose efficiency is . If it does not exist, output -1; if it exists, output a non-negative integer .
The definition of is as follows: given three integers , define the value of a plan as $k = \bigoplus\limits^n_{j=1} ( (x\times j + y\times a_j^2) \bmod m )$, where is bitwise XOR.
Since there may be more than one plan that meets the requirement, there are also many possible values of . You need to output any valid .
In the second phase, the feasibility of some plans will be checked. In each query, your program is given an integer (). For this query, if it is impossible to create a plan with efficiency , you must output -1; otherwise, you must output integers (), representing a plan. The value of computed from this plan must be equal to the previously output , and its efficiency must be .
Before printing each , you do not know which plans will be checked. Therefore, you must ensure that the you output in the first phase are correct.
Input Format
This problem has multiple test cases. The first line contains an integer (), the number of test cases.
In each test case:
The first line contains two integers (, ), the number of cities and the number of requested efficiencies.
The second line contains three integers (, ), the parameters used to compute .
The next lines describe the road network between the cities. In these lines, the -th line contains two integers (, ), meaning the -th road connects cities and with a two-way road. It is guaranteed that these roads form a tree.
The next lines: the -th line contains two integers (), the worker limits in the -th city.
The next line contains integers (), the efficiencies to be queried. It is guaranteed that all are distinct.
Next, you need to output what is required in the first phase (see “Output Format” for details). Only after you finish outputting can you read the second phase input.
Then follow several lines, each containing one integer (). When , is the index of the plan to be checked. After you output according to the required format, the interactive library will give you the next . When , the second-phase checking ends, i.e. this test case is finished. If this is not the last test case, your program should immediately start reading the next one.
It is guaranteed that . In each test case, the total number of checked plans is at most , and .
Output Format
After reading the first phase input of each test case, you need to output integers (). If , it means it is impossible to create a plan with efficiency . After you output them, the interactive library will provide the second phase input.
After reading the plan index to be checked in the second phase (and ), if the -th plan cannot be created, i.e. , output -1. Otherwise, output integers (), representing the created plan. The of this plan must equal , and its efficiency must equal .
For IO interactive problems, you should flush the output buffer after printing.
If you use cout << ... << endl in C++, System.out.println in Java, print in Python, or writeln in Pascal, the output buffer is flushed automatically and no extra action is needed.
If you use other output methods, it is recommended to flush the output buffer manually. To flush the output buffer, you can use fflush(stdout) in C and C++, flush(output) in Pascal, System.out.flush() in Java, and sys.stdout.flush() in Python.
1
9 3
4 7 15
1 2
2 4
2 5
1 3
3 6
3 7
6 8
6 9
4 4
2 2
5 5
3 3
2 2
6 6
3 3
4 4
3 3
18 19 20
1
2
3
0
-1 10 -1
-1
4 2 5 3 2 6 3 4 3
-1
3
3 4
3 4 11
1 2
2 3
0 1
0 1
0 1
0 1 2 3
2
0
4 6
1 2 11
1 2
2 3
3 4
0 2
1 1
1 1
1 2
0 1 2 3 4 5
5
2
3
0
5 7
11 31 101
1 2
2 3
2 4
3 5
1 2
1 5
0 4
1 4
4 6
13 12 11 10 9 8 6
3
5
7
0
12 8 3 -1
1 0 0
-1 -1 4 14 9 -1
2 1 1 2
-1
1 1 1 1
-1 127 23 58 13 90 91
2 5 4 1 6
2 4 4 3 4
1 1 0 1 4
Hint
In the samples, the interaction between the program and the interactive library is separated by blank lines. This is only for easier understanding, so you can clearly see which message replies to which. In the actual test, there will be no extra blank lines (only newlines).
In the first sample, there is only one way to create a plan: . Its efficiency is , and the computed is .
The second sample has three test cases.
- In the first test case:
- There is a unique plan with efficiency : . Its is .
- A plan with efficiency exists, for example . Its is .
- A plan with efficiency exists, for example . Its is .
- No plan with efficiency exists.
- Among these four requested plans, plan index () was checked.
- In the second test case:
- No plan with efficiency exists.
- No plan with efficiency exists.
- A plan with efficiency exists, for example . Its is .
- A plan with efficiency exists, for example . Its is .
- There is a unique plan with efficiency : . Its is .
- No plan with efficiency exists.
- Among these six requested plans, plan index (), (), and () were checked.
- In the first phase of the third test case, the following plans were requested (starting from the second one, because a plan with efficiency does not exist): ; ; ; ; ; .
| Subtask | Score | Other special properties | ||
|---|---|---|---|---|
It is guaranteed that . In each test case, the total number of checked plans is at most , and .
Translated by ChatGPT 5