#P8520. [IOI 2021] 喷泉公园
[IOI 2021] 喷泉公园
Background
Due to technical limitations, please do not use C++ 14 (GCC 9) to submit this problem.
This is an interactive problem. You only need to implement the function required in the code.
Your code does not need to include any additional header files, and you do not need to implement the main function. However, your code must declare the function void build(std::vector<int> u, std::vector<int> v, std::vector<int> a, std::vector<int> b).
Specifically, the following is a template:
#include <vector>
void build(std::vector<int> u, std::vector<int> v, std::vector<int> a, std::vector<int> b);
int construct_roads(std::vector<int> x, std::vector<int> y) {
// Code here...
}
Due to Luogu technical limitations, this problem only includes some of the official IOI test points.
Problem Description
In a nearby park, there are fountains, numbered from to . We treat the fountains as points on a 2D plane. That is, fountain is the point , where and are even numbers. All fountain positions are distinct.
The architect Timothy is hired to plan the construction of some roads, and the placement of a bench for each road. Each road is a horizontal or vertical line segment of length , whose endpoints are two different fountains. Visitors should be able to travel between any two fountains by walking along these roads. Initially, there are no roads in the park.
For each road, exactly one bench must be placed in the park and assigned to (i.e., facing) this road. Each bench must be placed at a point , where both and are odd numbers. All bench positions must be distinct. A bench at can only be assigned to a road whose two endpoints are one of the four points $(a - 1, ~ b - 1), (a - 1, ~ b + 1), (a + 1, ~ b - 1)$, and . For example, a bench at can only be assigned to one of the four roads represented by the following segments: $(2, ~ 2), - (2, ~ 4), ~ (2 , ~ 4) - (4, ~ 4), ~ (4, ~ 4) - (4, ~ 2), ~ (4, ~ 2) - (2, ~ 2)$.
Help Timothy determine whether it is possible to build the roads and place and assign the benches while satisfying all the requirements above. If it is possible, provide one feasible solution. If there are multiple solutions, you may output any one of them.
Input Format
You need to implement the following function:
int construct_roads(std::vector<int> x, std::vector<int> y)
- : Two arrays of length . For all , fountain is at point , where are even numbers.
- If there exists a construction plan, the function should call
build(see below) exactly once to report the plan, and then immediately return . - Otherwise, the function should return and must not call
build. - This function will be called exactly once.
Your function may call the following function to provide a feasible road construction and bench placement plan:
void build(std::vector<int> u, std::vector<int> v, std::vector<int> a, std::vector<int> b)
- Let be the number of roads in the plan.
- : Two arrays of length , describing the roads to be built. The roads are numbered from to . For all , road connects fountains and . Each road must be a horizontal or vertical segment of length . Any two different roads may share at most one common endpoint (a fountain). After building these roads, it must be possible to travel between any two fountains by walking along them.
- : Two arrays of length , describing the benches. For all , a bench will be placed at and assigned to road . Different benches must not be placed at the same position.
5
4 4
4 6
6 4
4 2
2 4
1
4
0 2 5 5
0 1 3 5
3 0 5 3
4 0 3 3
2
2 2
4 6
0
Hint
Example 1
Consider the following call:
construct_roads([4, 4, 6, 4, 2], [4, 6, 4, 2, 4])
This means there are fountains in total:
- Fountain is located at .
- Fountain is located at .
- Fountain is located at .
- Fountain is located at .
- Fountain is located at .
We can build the following roads, where each road connects two fountains and has its assigned bench.
| Road ID | Fountain IDs connected by the road | Assigned bench position |
|---|---|---|
This plan corresponds to the figure below:

To report this plan, construct_roads should make the following call:
build([0, 0, 3, 4], [2, 1, 0, 0], [5, 3, 5, 3], [5, 5, 3, 3])
Then it should return .
Note that in this example, there are multiple valid plans, and any of them is considered correct.
Example 2
Consider the following call:
construct_roads([2, 4], [2, 6])
Fountain is located at , and fountain is located at . Since it is impossible to build roads that satisfy the requirements,
construct_roads should return and must not call build.
Constraints
- are even numbers.
- No two fountains share the same position.
Subtasks
- ( points)
- ( points)
- ( points)
- ( points) There is at most one road construction plan that allows visitors to travel between any two fountains along the roads.
- ( points) No four fountains form the four vertices of a square.
- ( points) No additional constraints.
Translated by ChatGPT 5