#P15247. [WC2026] 画树
[WC2026] 画树
Background
1 s, 1 GB.
When submitting on Luogu, please use a language version not lower than C++17, and you do not need to include the paint.h header file. However, you should add the following declarations:
void setting(int k, std::vector<int> p);
void alter(int t, int x, int y);
Problem Description
Little F learned how to draw trees in art class. He drew a tree with nodes, numbered from to .
Since he is not satisfied with the current tree, Little F plans to modify it using pairs of pencils and erasers, where each pencil is paired one-to-one with an eraser. Initially, he places each pair of pencil and eraser on some node of the tree. Specifically, for , let the node where the pencil is located be , and the node where the eraser is located be . Initially, . At any moment, multiple pencils and multiple erasers may be placed on the same node.
In each modification, Little F first chooses one pair of pencil and eraser. Suppose he chooses the -th () pair. Then he performs the modification as follows:
- Choose any node (), move the pencil to node , and draw the edge formed by the movement, i.e., add an edge between and , then set .
- Choose a node () that is directly connected to by an edge (this edge may be the one newly drawn in the previous step), move the eraser to node , and erase the edge passed through during the movement, i.e., delete the edge between and , then set .
Of course, Little F must ensure that after each modification, the resulting graph is still a tree.
Little F hopes to use as few pencil-and-eraser pairs as possible to modify tree into another tree . You need to help Little F construct a set of modification plans. Specifically, you need to determine the number of pairs , specify the initial position for each pair (), and then construct a sequence of modifications (), such that after performing these modifications in order, tree can be transformed into tree .
Implementation Details
Contestants do not need to, and should not, implement the main function.
Contestants need to ensure that the submitted program includes the header file paint.h, i.e., add the following code at the beginning of the program:
#include "paint.h"
Contestants need to implement the following two functions in the submitted source file paint.cpp:
void init(int c, int t);
- represent the test point number and the number of testdata groups, respectively. means this test point is the sample.
- For each test point, this function will be called by the interactive library exactly once when the program starts running.
void paint(int n, std::vector<int> u, std::vector<int> v);
- is the number of nodes in tree .
- For , represent an edge of tree .
- For , represent an edge of tree .
- For each test point, this function will be called by the interactive library exactly times.
Contestants can set the number of pencil-and-eraser pairs and the initial position of each pair by calling the following function:
void setting(int k, std::vector<int> p);
- is the number of pencil-and-eraser pairs used. Contestants must ensure .
- For , is the initial position of the -th pencil-and-eraser pair. Contestants must ensure that the length of is , and for all , .
- Contestants must ensure that each time the interactive library calls
paint, this function is called exactly once.
Contestants can perform one modification by calling the following function:
void alter(int t, int x, int y);
- are the index of the chosen pencil-and-eraser pair and the node indices, respectively, with meanings as described in the Description. Contestants must ensure , , and that after the modification the resulting graph is still a tree.
- Contestants must ensure that each time the interactive library calls
paint, the number of calls to this function does not exceed , and all calls to this function occur after callingsetting.
Note: In any case, the time required by the interactive library will not exceed seconds. Its memory usage is fixed-size and does not exceed MiB.
How to Run the Test Program
In the problem directory, grader.cpp is a reference implementation of the interactive library. The interactive library used in the final tests is different from this reference implementation, so contestants' solutions should not depend on the interactive library implementation.
You can compile an executable in this problem directory using the following command:
g++ grader.cpp paint.cpp -o paint -O2 -std=c++14 -static
Input Format
For the compiled executable program:
- The executable will read input from standard input in the following format:
- The first line contains two non-negative integers , representing the test point number and the number of testdata groups.
- Then for each group of testdata, in order:
- The first line contains a positive integer , representing the number of nodes in tree .
- Line () contains two positive integers , representing an edge of tree .
- Line () contains two positive integers , representing an edge of tree .
Output Format
- The executable will output to standard output in the following format:
- For each group of testdata:
- The first line contains two non-negative integers , representing the number of pencil-and-eraser pairs used and the length of the modification sequence.
- The second line contains positive integers , representing the initial positions of each pencil-and-eraser pair.
- Line () contains three positive integers , representing, for the -th modification, the index of the chosen pencil-and-eraser pair and the node indices.
- For each group of testdata:
0 2
5
2 3
3 1
5 1
5 4
1 4
2 3
3 1
5 3
5
4 2
1 3
5 1
1 2
2 1
3 5
3 1
4 5
1 3
1
1 4 5
1 5 4
1 3 5
2 2
4 5
1 5 2
2 3 1
Hint
Sample 1 Explanation
This sample contains two groups of testdata.
For the first group of testdata, tree contains edges , and tree contains edges . One feasible modification plan is as follows:
- Initially, there is 1 pencil-and-eraser pair placed at node 1.
- In the first modification, choose , , add edge , delete edge . The resulting tree contains edges . At this time, , .
- In the second modification, choose , , add edge , delete edge . At this time, , .
- In the third modification, choose , , add edge , delete edge . The resulting tree contains edges , which is exactly tree .
Notes on the Provided Files
In this problem directory:
grader.cppis the provided reference implementation of the interactive library.paint.his the header file; contestants do not need to care about its contents.template_paint.cppis the provided sample code; contestants may refer to it and implement their own code.
Contestants should back up all provided files. In the final evaluation, only paint.cpp in this problem directory will be tested, and modifications to files other than this program will not affect the evaluation result.
Constraints
For all testdata:
- .
- .
- For all , , and all edges form a tree.
- For all , , and all edges form a tree.
- Both and are generated independently and uniformly at random among all trees on nodes.
::cute-table{tuack}
| Test Point Number | Score | |
|---|---|---|
Scoring
Note:
- Contestants should not obtain internal information of the interactive library by illegal means, such as interacting directly with the standard input/output streams. Such behavior will be regarded as cheating.
- The interactive library used in the final evaluation is different from the sample interactive library implementation.
This problem is first subject to the same limits as traditional problems. For example, a compilation error will cause the whole problem to get points; a runtime error, exceeding the time limit, exceeding the memory limit, etc., will cause the corresponding test point to get points. Contestants may only access variables defined by themselves and variables provided by the interactive library. Attempting to access other address spaces may cause a compilation error or runtime error.
Each time the paint function is called, if the setting function or the alter function is called illegally, or the number of calls to alter exceeds , then the corresponding test point gets points.
Based on the above conditions:
- In test points and , the program gets full score if and only if, when each call to the
paintfunction returns, tree has been transformed into tree . - In test point , for each group of testdata, let be the minimum number of pencil-and-eraser pairs used.
- If, when the
paintfunction returns, tree has not been transformed into tree , then the score is . - Otherwise, the score is .
- If, when the
The program's score is the minimum score over all testdata groups.
- In test point , for each group of testdata, let be the minimum number of pencil-and-eraser pairs used.
- If , the score is .
- Otherwise, if, when the
paintfunction returns, tree has not been transformed into tree , then the score is . - Otherwise, the score is $\lfloor 4 + 33 \cdot 0.99^{\max(\lfloor m / 2000 \rfloor - 5, 0)} \rfloor$.
The program's score is the minimum score over all testdata groups.
Translated by ChatGPT 5