#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 (T,I,SV,SE,ι)(T, I, S_V, S_E, \iota), where:

  • TT is a rooted tree with nn nodes, T=(V,E)T = (V, E), where VV is the vertex set of TT and EE is the edge set of TT. The nodes are numbered 1,2,,n1, 2, \ldots, n, and the root is node 11.
  • II is a set whose elements are called information. There are two distinct special elements: the identity element ϵ\epsilon and the invalid information \bot.

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 oI{ϵ,}o \in I \setminus \{ \epsilon, \bot \}, the vertex set of oo is a 2-element subset of VV, denoted by SV(o)S_V(o), satisfying SV(o)VS_V(o) \subseteq V and SV(o)=2\lvert S_V(o) \rvert = 2. For two sets A,BA, B, the difference ABA \setminus B is defined as $A \setminus B = \{ x \in A \hspace{3mu}\vert\hspace{3mu} x \notin B \}$.
  • For information oI{}o \in I \setminus \{ \bot \}, the edge set of oo is a subset of EE, denoted by SE(o)S_E(o), satisfying SE(o)ES_E(o) \subseteq E. The edge set of the identity element is defined to be empty, i.e. SE(ϵ)=S_E(\epsilon) = \varnothing.
  • For any edge eEe \in E on the tree, write e=(u,v)e = (u, v). There exists information ι(e)I\iota(e) \in I about ee, whose vertex set is its endpoints and whose edge set is itself, i.e. SV(ι(e))={u,v}S_V(\iota(e)) = \{ u, v \} and SE(ι(e))={e}S_E(\iota(e)) = \{ e \}.

There are two ways to merge information, denoted by RR and CC. For a,bI\forall a, b \in I, let r=R(a,b),c=C(a,b)r = R(a, b), c = C(a, b), where r,cIr, c \in I. Then:

  • Merging the identity element with any information yields the other one. That is, if a=ϵa = \epsilon then r=c=br = c = b; if b=ϵb = \epsilon then r=c=ar = c = a.
  • Merging invalid information with any information yields invalid information. That is, if a=a = \bot or b=b = \bot, then r=c=r = c = \bot.
  • 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 11, then the result is invalid information. That is, if SE(a)SE(b)S_E(a) \cap S_E(b) \ne \varnothing or SV(a)SV(b)1\lvert S_V(a) \cap S_V(b) \rvert \ne 1, then r=c=r = c = \bot.
  • Otherwise,SE(r)=SE(c)=SE(a)SE(b),S_E(r) = S_E(c) = S_E(a) \cup S_E(b) \text{,} SV(r)=SV(a),S_V(r) = S_V(a) \text{,} SV(c)=SV(a)SV(b),S_V(c) = S_V(a) \oplus S_V(b) \text{,} where \oplus denotes symmetric difference, i.e. AB=(AB)(AB)A \oplus B = (A \cup B) \setminus (A \cap B).

Define the distance between two vertices in TT as the number of edges on the unique simple path between them.

Given a scoring parameter MM and qq queries, each query provides a node uu on the tree and a non-negative integer dd. Let the vertex set XX be the set of all vertices in TT whose distance to uu is at most dd. Let the edge set $Y = \{ (a, b) \in E \hspace{3mu}\vert\hspace{3mu} a, b \in X \}$ be the set of edges inside XX. It can be proven that, starting from ϵ\epsilon and all ι(e)\iota(e) (eEe \in E), it is always possible, through a finite number of calls to RR and CC, to obtain information oo \ne \bot such that SE(o)=YS_E(o) = Y.

For each query, you need to construct such information oo under the constraint that the total number of calls to RR and CC is at most MM. In particular, if d=0d = 0, you can directly return the identity element ϵ\epsilon.


[Implementation Details]

Please make sure your program begins with #include "count.h".

The header count.h implements the following:

  1. Defines the data type info corresponding to information.

  2. Defines the constant emptyinfo of type info corresponding to ϵ\epsilon, which you can use directly in your program.

  3. 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 R(a,b)R(a, b) and C(a,b)C(a, b), respectively.

    You must guarantee that when calling R(a,b)\boldsymbol{R(a, b)} and C(a,b)\boldsymbol{C(a, b)}, the result is not \boldsymbol{\bot}, otherwise your program may behave abnormally.

  4. 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 aa 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);
  • T is the test point id, n is the number of nodes, q is the number of queries, and M is the scoring parameter for this test point.
  • The lengths of fa and e are both n1n - 1. For 0i<n10 \le i < n - 1, fa[i]fa[i] and i+2i + 2 are the endpoints of the ii-th edge eie_i, and e[i] is the info element corresponding to ι(ei)\iota(e_i) mentioned in the statement. The data guarantees that fa[i]fa[i] is less than i+2i + 2.
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 qq times. The interactive library uses a special implementation: a single variable of type info always consumes 1212 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 108{10}^8 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 id,n,q,Mid, n, q, M, representing the test point id, the number of nodes, the number of queries, and the scoring parameter.
    • The second line contains n1n - 1 integers p2,p3,,pnp_2, p_3, \ldots, p_n, representing the parent of nodes 22 to nn. When debugging locally, you need to ensure that i[2,n]\forall i \in [2, n], pi<ip_i < i.
    • The next qq lines each contain two integers u,du, d, 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 C1,C2,C3C_1, C_2, C_3, where:
      • C1C_1 is the total number of calls to interactive-library functions made in init.
      • C2C_2 is the total number of calls to interactive-library functions made during the whole run.
      • C3C_3 is the maximum, over the qq calls to ask, of the number of calls to interactive-library functions made within a single ask.
      • For these three statistics, only calls to MR and MC are counted; calls to isempty are not counted.
  • When linking different files, the checks performed are also different:
    • grader.o: it does not check whether the information returned by ask is 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 by ask is correct, and can also help contestants verify whether the interactive operations meet the requirements. It also checks whether the information returned by ask is 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 info is not guaranteed to be emptyinfo.
  • 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 MR or MC before init is called, otherwise undefined behavior may occur.
  • You may only access variables you define yourself and variables of type info returned 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 C1C_1 and C3C_3 be the number of calls to interactive-library functions in init, and the maximum, over all qq calls to ask, of the number of calls to interactive-library functions within a single ask. If C13107C_1 \le 3 \cdot {10}^7 and C3C_3 does not exceed the scoring parameter MM 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 C1C_1 and C3C_3, 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, 1n2×1051 \le n \le 2 \times {10}^5, 1q1061 \le q \le {10}^6. In each query, 1un1 \le u \le n, 1dn11 \le d \le n - 1.

Test point n=n= q=q= Special property M=M=
11 10001000 10410^4 500500
22 20002000
3,43,4 10510^5 10610^6 A 55
5,65,6 6×1046 \times 10^4 6×1046\times 10^4 B 5050
77 6×1046 \times 10^4 55
88 10510^5
99 75007500 5×1045 \times 10^4 C 500500
1010 10410^4
1111 1.5×1041.5 \times 10^4
1212 2×1042 \times 10^4 5050
1313 2.5×1042.5 \times 10^4 55
1414 3×1043 \times 10^4 10510^5
1515 6×1046 \times 10^4 10610^6 D
1616
1717 8×1048 \times 10^4
1818 10510^5
1919 1.5×1051.5 \times 10^5
2020 2×1052 \times 10^5 11

Special property A: It is guaranteed that i[1,n1]\forall i \in [1, n - 1], the parent of node i+1i + 1 is node ii.
Special property B: It is guaranteed that all queries satisfy u=1u = 1.
Special property C: It is guaranteed that all queries satisfy d100d \le 100.
Special property D: It is guaranteed that all queries satisfy d1000d \ge 1000.

Translated by ChatGPT 5