In contest TL 3s, ML 2048MB.
DMOJ Configuration: 6s, ML 1GB (DMOJ only allows for a 1GB memory limit!)
THIS IS AN INTERACTIVE PROBLEM
Given a quintuple where:
is a rooted tree of n points , where is the set of points of and is the edge set of . The nodes of the tree are numbered , where the root node is numbered .
is a set, and the elements in the set are called information. There are two different special elements: the UNIT element and the ILLEGAL element .
For general information, it has two attributes: VERTEX SET and EDGE SET. For the special case of the identity element, it only has edge set attribute, while for the illegal information, it does not have either of these two attributes.
For information (the difference of two sets A, B is defined as ), the VERTEX SET of is a size two subset of V, denoted . That is, and .
For information , the EDGE SET o is a subset of E, denoted , such that . Define the edge set of the identity element is empty, that is, .
For any edge in the tree, denote , there is an information about , , which takes its endpoints its VERTEX SET and the edge itself as its EDGE SET, that is, , and .
There are two ways that information get combined. Denote them as R and C. They have the following properties
For all , shorthand , , such that .
Combining UNIT with any general information gives the other. That is if , then ; If , then .
Combining ILLEGAL with ANY information results in illegal information. That is, if or If , then .
For the remaining cases, if the intersection of the EDGE SET of the two information is non-empty, or the intersection of the POINT SET of the two information has size that's not 1, the combine results in ILLEGAL. That is, if or , then .
Otherwise, the operations are specified as
- ,
- ,
- .
where represents the symmetric difference operation of sets, that is, .
Define the on-tree distance of two points in as the number of edges on the tree that a unique simple path traversed by two points as endpoints.
Given the scoring parameter and queries, each query consisting a vertex of the tree and a non-negative integer . Denote to be the set of all vertices in whose distances to in the tree does not exceed , and to be the set of edges inside .
It can be shown that starting from and all (), a finite number of R, C calls produces an information such that and . In particular, if , you the output should be the UNIT element .
In each set of queries, you need to construct an information that satisfies this requirement, subject to the limit that the sum of the calls to and does not exceed .
Implementation Details
Files can be found here.
Make sure you #include count.h
at the beginning of your program.
The header file count.h implements the following:
- Define the data type
info
corresponding to the information; - Define the
info
type constantemptyinfo
corresponding to , which you can use directly in the program. - The following two information merging functions, which you can call directly in the program:
The two functions return the information corresponding to and , respectively. You need to ensure that calling or does not result in , otherwise the program may behave unexpectedly.1 info MR(info a, info b); 2 info MC(info a, info b);
- A function to determine whether a information is UNIT, you can call it directly in the program:
This function returns true if and only if a is an identity element. See the reference interaction library for more implementation details.bool isempty(info a);
You do not need to, and should not, implement main();
You need to implement the following functions:
void init(int T, int n, int q, vector<int> fa, vector<info> e, int M);
- is the test point number, is the number of points in the tree, is the number of queries, and is the scoring parameter for that test point.
- Both fa and e have length . For , fa[i] and i + 2 are the two endpoints of the i-th edge , and is the info type element corresponding to mentioned in the title description. The data guarantees that is less than .
info ask(int u, int d);
- Give a query, see the question description for the meaning of the parameters. You need to return a information that satisfies the condition of the question at the end of the function.
When testing, at each test case, the interactive library will call the init
function exactly once, and then call the ask
function times. The interactive library will use a special implementation, a single info type variable will constantly consume bytes of memory,
It is guaranteed that under the condition that the number of calls is satisfied and the isempty function is not called, the time required for the interactive library to run in the final test does not exceed seconds, and the memory consumed by the interactive library itself does not exceed 16 MiB. It is guaranteed that the final tested interactive library will take no more than seconds to run with at most isempty function calls.
A file named count.cpp is included in the issued files: you may use it.
Testing
The reference materials provides of two interactive libraries grader.o
and checker.o
are provided in this topic directory, which are linkable files generated by compiling two different interactive libraries. The implementation of the interaction library used in the final test is different from this implementation, so the solution of the contestant should not depend on the specific implementation of the interaction library, nor should it depend on the specific implementation of the info type in count.h.
You need to modify the distributed count.h
to help with linking. Specifically, when linking the source code count.cpp
with the program grader.o
, you need to comment out the 5th line of the count.h code and keep the 4th line of code. Linking the checker.o
method is similar, you need to comment out the 4th line of the count.h
code and keep the 5th line of code. Players can modify the implementation of count.h
by themselves to compile different programs.
After modification, the contestant can use the following command to compile the executable program in the directory of this question:
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 line will compile the current count.cpp and link it with grader.o
to generate the executable file count. The second line command will compile the current count.cpp
and link it with checker.o
to generate the executable file count.
The executable file count obtained by compiling the above method runs as follows:
- The executable will read data in the following format from standard input:
- The first line contains four integers , which represent the test point number, the number of points in the tree, the number of queries, and the scoring parameter;
- In the second line, n - 1 integers represent the parent node numbers from to , respectively. When debugging locally, you need to ensure that , ;
- The next lines contain two integers each, describing a query.
After being read in, the interactive library is tested. If your program does not meet the interactive library limit, the output will return the corresponding error. Otherwise, for the linked executable, the output is as follows:
- A total of three integers on one line, where: ∗ represents the total number of times the program calls the interactive library function in the init function; ∗ represents the total number of times the program calls interactive library functions during running; ∗ * represents the maximum number of times the program calls the interactive library function in q times of ask functions.
- For the above three statistics, we only count the number of calls of the MR and MC functions, but not the number of calls of the isempty function.
- When linking different files, the checks they can perform are also different, specifically:
grader.o
: It does not check whether the information returned by the ask function is correct at runtime, but it can help players determine whether the interaction meets the requirements. The running time of this program is closest to the interactive library at the time of evaluation, so players can use this program to test the running speed, but the correctness of the program is not guaranteed.checker.o
: It will check whether the information returned by the ask function is correct at runtime, and can also help players determine whether the interactive operation meets the requirements. At the same time, it will check whether the information returned by the ask function is correct. This program can check the correctness of the answers.
Samples
See count{1,2,3,4}.{in, ans}
Samples 2 & 3 satisfy special properties A and B (see below) respectively
NOTE:
- An uninitialized variable of type
info
is not guaranteed to beemptyinfo
. - Please do not try to access or modify member variables of type
info
, otherwise it will be regarded as attacking the interactive library. - Please do not call the
MR
andMC
functions before the init function call, otherwise undefined behavior may occur. - You can only access the variables defined by yourself and the
info
type variables returned by the interactive library. Trying to access other spaces may result in compilation errors or runtime errors.
This question will first be subject to the same restrictions as traditional questions. For example, compilation errors will result in 0 points for the entire question, and runtime errors, exceeding the time limit, exceeding the space limit, etc. will result in 0 points for the corresponding test points, etc. In addition to the above conditions, a test point will receive a score of 0 if the program executes an illegal function call or gives an incorrect answer in a query operation. Otherwise, denote and as the number of times your program calls the interactive library function in the init function, and the maximum number of times your program calls the interactive library function in all q ask functions. If and does not exceed the scoring parameter for that test point, you will get a score for that test point, otherwise you can't get a score for that test point. Note: When calculating and , only the number of calls of the MR and MC functions will be counted, but not the number of calls of the isempty function.
Constraints
For all test cases, , , for each query, , .
TestCase | Property | |||
---|---|---|---|---|
A | ||||
B | ||||
B | ||||
B | ||||
C | ||||
D | ||||
- Property A: For all , the parent of is .
- Property B: For all queries, .
- Property C: For all queries, .
- Property D: For all queries, .
Comments