#P16435. [APIO 2026 中国赛区] 集宝
[APIO 2026 中国赛区] 集宝
Background
When submitting, please choose a language standard higher than C++17, and do not include the header file gems.h.
Problem Description
The 1000th Gem Collecting Contest has begun.
The audience is tired of gem collecting problems on linear sequences, so this contest has a clear innovation compared to previous ones: contestants now need to collect gems on an unrooted tree with nodes.
There are gems on the tree. The -th gem () has a pair of parameters , meaning that when a contestant is currently at a node whose simple path to node contains at most edges, they may choose to collect this gem immediately (of course, they may also choose not to collect it).
At the same time, many contestants have signed up, with a total of contestants. For each contestant, they are assigned a starting node and a gem interval . They need to complete the following task: starting from node , repeatedly choose an adjacent edge of the current node and move across it, and collect gems from the -th to the -th in order, i.e., collect all gems in the order .
Since each contestant encounters more or less route planning difficulties during the contest, the organizer has found you to provide a reasonable scoring method. For each contestant, compute the minimum total number of edges they need to traverse to complete the collection task.
Implementation Details
You do not need to, and should not, implement the main function.
You must ensure that the submitted program includes the header file gems.h, i.e., add the following code at the beginning of the program:
#include "gems.h"
You need to implement the following two functions in the submission source file gems.cpp:
void gems(int c, int n, int m, std::vector<int> u, std::vector<int> v, std::vector<int> a, std::vector<int> d);
- represent the test point ID, the number of nodes in the tree, and the number of gems, respectively. means this test point is the sample.
- For , represent an edge of the tree.
- For , represent the two parameters of the -th gem.
- For each test point, this function will be called by the interaction library exactly once, and before any
queryfunction calls.
long long query(int x, int l, int r);
- represent a contestant’s starting node and gem interval.
- This function should return the minimum total number of edges the contestant needs to traverse when starting from node and collecting gems from the -th to the -th in order.
- For each test point, this function will be called by the interaction library exactly times.
Testing Program Method
You can compile an executable in this problem directory using the following command:
g++ grader.cpp gems.cpp -o gems -O2 -std=c++14 -static
Input Format
For the compiled executable:
- The executable will read data from standard input in the following format:
- The first line contains three non-negative integers .
- Line () contains two positive integers .
- Line contains positive integers .
- Line contains non-negative integers .
- Line contains one positive integer .
- Line () contains three positive integers .
Output Format
The executable will output data to standard output in the following format:
- Output a total of lines, each containing one non-negative integer, which is the return value of the
queryfunction.
0 5 4
1 2
1 3
2 4
2 5
4 1 5 3
1 0 1 2
2
2 2 4
4 1 2
2
2
Hint
Sample 1 Explanation
For the first contestant, they need to start from node and collect gems . One possible collection path is , passing through edges in total.
For the second contestant, they need to start from node and collect gems . One possible collection path is , passing through edges in total.
Constraints
For all testdata, we have:
- $2 \le n \le 3 \times 10^5, 1 \le m \le 3 \times 10^5$.
- For all , , and all edges form a tree.
- For all , and .
- .
- .
::cute-table{tuack} | Test Point ID | | | Special Property | |:---:|:---:|:---:|:---:| | | | | None | | | | | ^ | | | ^ | | ^ | | | | | A | | | ^ | ^ | B | | | ^ | ^ | C | | | | | None | | | | | ^ |
- Special Property A: The tree is a chain, i.e., there is no node with degree greater than .
- Special Property B: For all , .
- Special Property C: .
Translated by ChatGPT 5