#P8498. [NOI2022] 树上邻域数点
[NOI2022] 树上邻域数点
Background
This is an interactive problem.
Before submitting this problem, please read the following carefully.
This problem only supports C++ submissions (it is recommended to use C++14, please do not use C++14 (GCC9)).
Due to Luogu’s special interactive mechanism, when submitting this problem, please remove the line #include "count.h" from your code, and paste the content of the provided file count_.h (note that it is NOT the count.h inside count.zip) at the very beginning of your code, then submit.
If you cannot open count_.h, you may copy the following content.
#ifndef CIRCLE_H
#define CIRCLE_H
#include<vector>
struct info{
unsigned val;
unsigned poi[2];
};
const info emptyinfo=info{0,(unsigned)-1,(unsigned)-1};
info MR(info a,info b);
info MC(info a,info b);
void init(int T,int n,int q,std::vector<int>dad,std::vector<info>ve,int M);
bool isempty(info a);
info ask(int x,int d);
#endif
If anything unexpected happens when you submit this problem, please contact the administrator.
Problem Description
Given a 5-tuple , where:
- is a rooted tree with nodes, , where is the vertex set of and is the edge set of . The nodes are numbered , and the root is node .
- is a set whose elements are called information. There are two distinct special elements: the identity element and the invalid information .
For ordinary information, it has two attributes: a vertex set and an edge set. In particular, the identity element only has the edge-set attribute, while the invalid information has neither of the above attributes.
- For information , the vertex set of is a 2-element subset of , denoted by , satisfying and . For two sets , the difference is defined as $A \setminus B = \{ x \in A \hspace{3mu}\vert\hspace{3mu} x \notin B \}$.
- For information , the edge set of is a subset of , denoted by , satisfying . The edge set of the identity element is defined to be empty, i.e. .
- For any edge on the tree, write . There exists information about , whose vertex set is its endpoints and whose edge set is itself, i.e. and .
There are two ways to merge information, denoted by and . For , let , where . Then:
- Merging the identity element with any information yields the other one. That is, if then ; if then .
- Merging invalid information with any information yields invalid information. That is, if or , then .
- In the remaining cases, if the intersection of the edge sets is non-empty, or the size of the intersection of the vertex sets is not , then the result is invalid information. That is, if or , then .
- Otherwise, where denotes symmetric difference, i.e. .
Define the distance between two vertices in as the number of edges on the unique simple path between them.
Given a scoring parameter and queries, each query provides a node on the tree and a non-negative integer . Let the vertex set be the set of all vertices in whose distance to is at most . Let the edge set $Y = \{ (a, b) \in E \hspace{3mu}\vert\hspace{3mu} a, b \in X \}$ be the set of edges inside . It can be proven that, starting from and all (), it is always possible, through a finite number of calls to and , to obtain information such that .
For each query, you need to construct such information under the constraint that the total number of calls to and is at most . In particular, if , you can directly return the identity element .
[Implementation Details]
Please make sure your program begins with #include "count.h".
The header count.h implements the following:
-
Defines the data type
infocorresponding to information. -
Defines the constant
emptyinfoof typeinfocorresponding to , which you can use directly in your program. -
Defines and implements the following two merge functions, which you can call directly:
info MR(info a,info b); info MC(info a,info b);- They return the information corresponding to and , respectively.
You must guarantee that when calling and , the result is not , otherwise your program may behave abnormally.
-
Defines and implements a function to determine whether an information object is the identity element, which you can call directly:
bool isempty(info a);- It returns true if and only if is the identity element.
You can check the reference interactive library for more implementation details.
You do not need, and should not, implement the main function. You need to implement the following functions:
void init(int T, int n, int q, vector<int> fa, vector<info> e, int M);
Tis the test point id,nis the number of nodes,qis the number of queries, andMis the scoring parameter for this test point.- The lengths of
faandeare both . For , and are the endpoints of the -th edge , ande[i]is theinfoelement corresponding to mentioned in the statement. The data guarantees that is less than .
info ask(int u, int d);
This gives one query; the meaning of the parameters is as described above. You need to return, at the end of the function, an information object that satisfies the requirements.
In the final test, for each test point, the interactive library will call init exactly once, and then call ask times. The interactive library uses a special implementation: a single variable of type info always consumes bytes of memory, which is different from the provided reference interactive library. To keep the runtime memory usage within the limit, you need to ensure that not too many variables of type info exist at the same time during execution.
It is guaranteed that, when satisfying the call limit and without calling isempty, the time needed by the final interactive library does not exceed 0.6 seconds, and the memory used by the interactive library itself does not exceed 16 MiB. It is guaranteed that, when only executing calls to isempty, the running time of the final interactive library does not exceed 0.25 seconds.
The provided files include count.cpp as an example program, and contestants may continue implementing this problem based on it. The provided files also include a backup file count_backup.h, which we guarantee is exactly the same as count.h.
[How to Run the Testing Program]
This problem directory provides two reference implementations of the interactive library, grader.o and checker.o, which are linkable files produced by compiling two different interactive libraries. The interactive library used in the final test is different from these implementations, so your solution must not depend on the specific implementation of the interactive library, and also must not depend on the specific implementation of the info type in count.h.
You need to modify the provided count.h to help with linking. Specifically, when linking the source code count.cpp with grader.o, you need to comment out line 5 of count.h and keep line 4. Linking with checker.o is similar: you need to comment out line 4 and keep line 5. You may modify the implementation in count.h to compile different programs.
After modification, you can compile an executable in this directory with the following commands:
g++ count.cpp -c -O2 -std=c++14 -lm && g++ count.o grader.o -o count
g++ count.cpp -c -O2 -std=c++14 -lm && g++ count.o checker.o -o count
The first command compiles count.cpp and links it with grader.o to produce an executable count. The second command compiles count.cpp and links it with checker.o to produce an executable count.
The executable count compiled as above runs as follows:
- It reads input from standard input in the following format:
- The first line contains four integers , representing the test point id, the number of nodes, the number of queries, and the scoring parameter.
- The second line contains integers , representing the parent of nodes to . When debugging locally, you need to ensure that , .
- The next lines each contain two integers , describing one query.
- After reading, the interactive library performs testing. If your program does not satisfy the library’s constraints, it will output the corresponding error message. Otherwise, for the linked executable, it outputs:
- A single line with three integers , where:
- is the total number of calls to interactive-library functions made in
init. - is the total number of calls to interactive-library functions made during the whole run.
- is the maximum, over the calls to
ask, of the number of calls to interactive-library functions made within a singleask. - For these three statistics, only calls to
MRandMCare counted; calls toisemptyare not counted.
- is the total number of calls to interactive-library functions made in
- A single line with three integers , where:
- When linking different files, the checks performed are also different:
grader.o: it does not check whether the information returned byaskis correct, but it can help contestants verify whether the interactive operations meet the requirements. Its running time is the closest to that of the interactive library in evaluation, so contestants can use it to test speed, but correctness is not guaranteed.checker.o: it checks whether the information returned byaskis correct, and can also help contestants verify whether the interactive operations meet the requirements. It also checks whether the information returned byaskis correct. This program can be used to check answer correctness.
When debugging, you need to ensure that the input to the executable count satisfies the input format above; otherwise, the correctness of the output is not guaranteed.
Input Format
See [How to Run the Testing Program].
Output Format
See [How to Run the Testing Program].
Hint
[Scoring]
In the final evaluation, only count.cpp will be collected. Modifying other files in your directory will not affect the evaluation result. Note:
- An uninitialized variable of type
infois not guaranteed to beemptyinfo. - Do not try to access or modify member variables of
info, otherwise it will be considered an attack on the interactive library. - Do not call
MRorMCbeforeinitis called, otherwise undefined behavior may occur. - You may only access variables you define yourself and variables of type
inforeturned by the interactive library. Attempting to access other memory may cause a compilation error or a runtime error.
This problem is first subject to the same constraints as traditional problems, e.g. a compilation error yields 0 points for the whole problem; runtime error, time limit exceeded, memory limit exceeded, etc. yield 0 points for the corresponding test point.
Besides the above conditions, within a test point, if the program performs illegal function calls or provides a wrong answer in a query operation, that test point will receive 0 points. Otherwise, let and be the number of calls to interactive-library functions in init, and the maximum, over all calls to ask, of the number of calls to interactive-library functions within a single ask. If and does not exceed the scoring parameter of that test point, you will get the score of that test point; otherwise, you will not get the score of that test point. Note that, when computing and , only calls to MR and MC are counted; calls to isempty are not counted.
[Sample #1]
See count/count1.in and count/count1.ans in the attachments.
[Sample #2]
See count/count2.in and count/count2.ans in the attachments.
This sample satisfies special property A in the Constraints.
[Sample #3]
See count/count3.in and count/count3.ans in the attachments.
This sample satisfies special property B in the Constraints.
[Sample #4]
See count/count4.in and count/count4.ans in the attachments.
[Constraints]
For all test points, , . In each query, , .
| Test point | Special property | |||
|---|---|---|---|---|
| A | ||||
| B | ||||
| C | ||||
| D | ||||
Special property A: It is guaranteed that , the parent of node is node .
Special property B: It is guaranteed that all queries satisfy .
Special property C: It is guaranteed that all queries satisfy .
Special property D: It is guaranteed that all queries satisfy .
Translated by ChatGPT 5