#P6830. [IOI 2020] 连接擎天树
[IOI 2020] 连接擎天树
Description
Gardens by the Bay is a large nature park in Singapore. There are towers in the park, called “supertrees”. The towers are numbered from to . We want to build a set of bridges (the number of bridges is at least ). Each bridge connects two different towers and can be traveled in both directions. No two bridges connect the same pair of towers.
A path from tower to tower is a sequence of towers (the number of towers is at least ) that satisfies the following:
- The first element of the sequence is .
- The last element of the sequence is .
- All elements in the sequence are distinct.
Each pair of adjacent elements (towers) in the sequence is connected by some bridge.
Note that by definition, there is exactly one path from a tower to itself, and for any towers and , the number of different paths from tower to tower is the same as the number of different paths from tower to tower .
The chief designer wants the bridges to satisfy the following: for any given , there are exactly different paths from tower to tower , where .
Please construct a set of bridges that satisfies the designer’s requirements, or determine that such a set of bridges cannot exist.
Implementation Details
You need to implement the following function:
int construct(std::vector<std::vector<int> > p)
- : an array describing the designer’s requirements.
- If such a construction exists, this function should call
build(see below) exactly once to output the construction, and then return . - Otherwise, this function should return and must not call
build. - This function will be called exactly once.
The function build is defined as follows:
void build(std::vector<std::vector<int> > b)
- : an array, where means there is a bridge connecting tower and tower , and otherwise .
- Note that this array must satisfy: for all , ; and for all , .
Hint
Sample Explanation
Example 1
Consider the following call:
construct([[1, 1, 2, 2], [1, 1, 2, 2], [2, 2, 1, 2], [2, 2, 2, 1]])
This indicates that there is exactly one path from tower to tower . For all other pairs of towers , there are exactly two different paths connecting tower and tower . This can be achieved by building bridges: connecting the pairs , and .
To output this solution, the function construct should make the following call:
build([[0, 1, 0, 0], [1, 0, 1, 1], [0, 1, 0, 1], [0, 1, 1, 0]])

The function should return .
For this example, there are many different constructions that satisfy the requirements, and all of them are considered correct.
Example 2
Consider the following call:
construct([[1, 0], [0, 1]])
This indicates that it is impossible to travel between the two towers. This can only be satisfied by building no bridges.
Therefore, the function construct should make the following call:
build([[0, 0], [0, 0]])
Then, the function construct should return .
Example 3
Consider the following call:
construct([[1, 3], [3, 1]])
This indicates that there are exactly paths from tower to tower . These requirements cannot be satisfied. Therefore, the function construct should return and must not call build.
Constraints
- (for all )
- (for all )
- (for all )
Subtasks
- (11 points) (for all ).
- (10 points) (for all ).
- (19 points) (for all ).
- (35 points) (for all ) and there exists at least one construction that satisfies the requirements.
- (21 points) (for all ).
- (4 points) No additional constraints.
Example Grader
The example grader reads the input in the following format:
Line :
Line ():
The output format of the example grader is as follows:
Line : the return value of construct.
If the return value of construct is , the example grader will additionally print:
Line ():
Translated by ChatGPT 5
京公网安备 11011102002149号