luogu#P11111. [ROI 2023] 生产计划 (Day 2)

    ID: 9011 远端评测题 3000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2023交互题Special JudgeROI(俄罗斯)

[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 nn cities in the country, and each city has a factory that participates in car production. The factory in the ii-th city can hire between lil_i and rir_i 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 [a1,a2,,an][a_1, a_2, \dots , a_n], where aia_i is the number of workers in the ii-th factory and liairil_i\le a_i\le r_i. 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 [a1,a2,,an][a_1, a_2, \dots , a_n].

Problem Description

You need to determine whether a plan with efficiency viv_i 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 v1,v2,,vqv_1,v_2,\dots,v_q. You must determine whether there exists a plan whose efficiency is viv_i. If it does not exist, output -1; if it exists, output a non-negative integer kik_i.

The definition of kk is as follows: given three integers x,y,mx,y,m, define the value kk of a plan [a1,a2,,an][a_1, a_2, \dots , a_n] as $k = \bigoplus\limits^n_{j=1} ( (x\times j + y\times a_j^2) \bmod m )$, where \oplus is bitwise XOR.

Since there may be more than one plan that meets the requirement, there are also many possible values of kik_i. You need to output any valid kik_i.

In the second phase, the feasibility of some plans will be checked. In each query, your program is given an integer ii (1iq1\le i\le q). For this query, if it is impossible to create a plan with efficiency viv_i, you must output -1; otherwise, you must output nn integers a1,a2,,ana_1,a_2,\dots,a_n (liairil_i\le a_i\le r_i), representing a plan. The value of kk computed from this plan must be equal to the previously output kik_i, and its efficiency must be viv_i.

Before printing each kk, you do not know which plans will be checked. Therefore, you must ensure that the kik_i you output in the first phase are correct.

Input Format

This problem has multiple test cases. The first line contains an integer tt (1t1041\le t\le10^4), the number of test cases.

In each test case:

The first line contains two integers n,qn,q (2n2×1052 \le n \le 2 \times 10^5, 1q2×1051 \le q \le 2 \times 10^5), the number of cities and the number of requested efficiencies.

The second line contains three integers x,y,mx,y,m (11m23011 \le m \le 2^{30}, 0x,y<m0 \le x,y < m), the parameters used to compute kk.

The next n1n-1 lines describe the road network between the cities. In these n1n-1 lines, the ii-th line contains two integers si,fis_i,f_i (1si,fin1 \le s_i,f_i \le n, sifis_i \ne f_i), meaning the ii-th road connects cities sis_i and fif_i with a two-way road. It is guaranteed that these roads form a tree.

The next nn lines: the ii-th line contains two integers li,ril_i,r_i (0liri1090 \le l_i \le r_i \le 10^9), the worker limits in the ii-th city.

The next line contains qq integers v1,v2,,vqv_1,v_2,\dots,v_q (0vii=1nri0 \le v_i \le \sum\limits^n_{i=1}r_i), the efficiencies to be queried. It is guaranteed that all viv_i 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 ii (0iq0\le i\le q). When i0i\ne0, ii 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 ii. When i=0i=0, 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 n,q2×105\sum n,\sum q\le2\times10^5. In each test case, the total number of checked plans cc is at most 10410^4, and n×c106\sum n\times c\le10^6.

Output Format

After reading the first phase input of each test case, you need to output qq integers k1,k2,,kqk_1,k_2,\dots,k_q (1ki<230-1 \le k_i < 2^{30}). If ki=1k_i = -1, it means it is impossible to create a plan with efficiency viv_i. After you output them, the interactive library will provide the second phase input.

After reading the plan index ii to be checked in the second phase (and i0i\ne0), if the ii-th plan cannot be created, i.e. ki=1k_i=-1, output -1. Otherwise, output nn integers a1,a2,,ana_1,a_2,\dots,a_n (liairil_i \le a_i \le r_i), representing the created plan. The kk of this plan must equal kik_i, and its efficiency must equal viv_i.

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: a=[4,2,5,3,2,6,3,4,3]a = [4, 2, 5, 3, 2, 6, 3, 4, 3]. Its efficiency is 1919, and the computed kk is 1010.

The second sample has three test cases.

  • In the first test case:
    • There is a unique plan with efficiency 00: a=[0,0,0]a = [0, 0, 0]. Its kk is 1212.
    • A plan with efficiency 11 exists, for example a=[1,0,0]a = [1, 0, 0]. Its kk is 88.
    • A plan with efficiency 22 exists, for example a=[1,0,1]a = [1, 0, 1]. Its kk is 33.
    • No plan with efficiency 33 exists.
    • Among these four requested plans, plan index i=2i = 2 (v2=1v_2 = 1) was checked.
  • In the second test case:
    • No plan with efficiency 00 exists.
    • No plan with efficiency 11 exists.
    • A plan with efficiency 22 exists, for example a=[1,1,1,1]a = [1, 1, 1, 1]. Its kk is 44.
    • A plan with efficiency 33 exists, for example a=[2,1,1,1]a = [2, 1, 1, 1]. Its kk is 1414.
    • There is a unique plan with efficiency 44: a=[2,1,1,2]a = [2, 1, 1, 2]. Its kk is 99.
    • No plan with efficiency 55 exists.
    • Among these six requested plans, plan index i=5i = 5 (v5=4v_5 = 4), i=2i = 2 (v2=1v_2 = 1), and i=3i = 3 (v3=2v_3 = 2) 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 v1=13v_1 = 13 does not exist): [2,5,4,4,6][2, 5, 4, 4, 6]; [2,5,4,1,6][2, 5, 4, 1, 6]; [2,4,4,4,4][2, 4, 4, 4, 4]; [2,4,4,3,4][2, 4, 4, 3, 4]; [2,4,4,2,4][2, 4, 4, 2, 4]; [1,1,0,1,4][1, 1, 0, 1, 4].
Subtask Score n,nn,\sum n q\sum q Other special properties
11 1111 li=ril_i=r_i
22 99 c=0c=0
33 1212 li=0,ri1l_i=0,r_i\le1
44 li=0l_i=0
55 88 si=1,fi=i+1s_i=1,f_i=i+1
66 55 n10,n1000n\le10,\sum n\le1000 Q1000Q\le1000 c=q,ri10,rili2c=q,r_i\le10,r_i-l_i\le2
77 22 c=q,ri10c=q,r_i\le10
88 1313 n1000\sum n\le1000 c=q,i=1nrili104c=q,\sum\limits^{n}_{i=1}r_i-l_i\le10^4
99 1111 c=qc=q
1010 66
1111 55 n1000\sum n\le1000
1212 n8000\sum n\le8000
1313 99

It is guaranteed that n,q2×105\sum n,\sum q\le2\times10^5. In each test case, the total number of checked plans cc is at most 10410^4, and n×c106\sum n\times c\le10^6.

Translated by ChatGPT 5