#P9932. [NFLSPC #6] 树
[NFLSPC #6] 树
Background
Please do not submit using C++14 (GCC 9).
Problem Description
Given a tree with nodes, labeled from to , each node has a color between and .
There are queries. For each query, you need to find, among the ancestors of , the node whose color is and is closest to (i.e., with the greatest depth), and output its label. The queries are strictly online.
After the testdata is generated, the node colors are randomly shuffled once (i.e., a uniformly random permutation is applied).
Input Format
Because the input size is large, this problem is judged as an interactive problem.
You do not need and should not implement the main function main. You need to implement the following two functions:
extern "C" void init(int n,vector<int> fa,vector<int> col)
- : the number of nodes.
- : an array of length , where is the parent of node . In particular, .
- : an array of length , where is the color of node . Emphasis again: is randomly shuffled once after the testdata is generated.
- This function will be called exactly once, providing the tree information. You may precompute any needed information during this call.
extern "C" int query(int x,int c)
- : the node to query.
- : the color to query.
- You should return the label of the closest ancestor of whose color is . If no such node exists, return
-1. - This function is called exactly times.
- When each call happens, you do not know future queries, so the queries are strictly online.
When using the provided grader test code, the input format is:
- The first line contains two integers .
- The second line contains integers .
- The third line contains integers .
- The next lines each contain two integers , representing one query.
Output Format
When running with the test code, the output format is:
- lines, each containing one integer, the answer.
- Then the provided grader will output your running time (note that correctness is not checked there).
Note: the input/output format given here is the one used by the provided grader, only for local testing. Your implemented functions should not perform any operations on standard input/output streams.
5 25
-1 0 1 1 0
0 1 0 2 2
0 0
0 1
0 2
0 3
0 4
1 0
1 1
1 2
1 3
1 4
2 0
2 1
2 2
2 3
2 4
3 0
3 1
3 2
3 3
3 4
4 0
4 1
4 2
4 3
4 4
0
-1
-1
-1
-1
0
1
-1
-1
-1
2
1
-1
-1
-1
0
1
3
-1
-1
0
-1
4
-1
-1
Hint
For all testdata, , , , , , and for any , .
- Subtask 1 ( points): .
- Subtask 2 ( points): .
- Subtask 3 ( points): .
- Subtask 4 ( points): no special constraints.
The scoring for each subtask is the minimum score among all test points within that subtask. The interactive library runs in at most 600ms and uses at most 140MB of memory.
The time limit shown on the OJ is not the real time limit. If your code does not run normally within the required time and memory and correctly answers all queries, that test point gets 0 points. If the total running time of your functions is not less than 2400ms, you will get Time Limit Exceeded (in the actual test, this is implemented by making the grader loop forever). Otherwise, if the total running time is () milliseconds, your score is:
- If , return
Acceptedand get full points for that test point. - If , return
Partially Correctand get times the score for that test point.
The time shown in the test point information is not the real running time; the one shown in the SPJ details is the running time of your implementation.
Source: NFLSPC #6 H by asmend
Translated by ChatGPT 5