European Girls' Olympiad in Informatics: 2023 Day 2 Problem 3
Grushög is an unfinished residential area in the outskirts of Lund. Right now, all necessary infrastructure is being constructed, including the most important thing of all: garbage disposal. Like in many areas of Sweden, a sopsug (automated vacuum collection system) will be used to collect garbage. The idea is to transport garbage underground through tubes using air pressure.
There are buildings in Grushög, numbered from to . Your task is to connect some pairs of buildings with tubes. If you build a tube from building to some other building , will send all of its garbage to (but not in the other direction). Your goal is to create a network of tubes such that all garbage ends up in one building. In other words, you want the network to form a rooted tree, where the edges are directed toward the root.
However, tubes have already been constructed between buildings. These must be used in your network. These tubes are directed, so they can only be used in one direction.
Furthermore, there are pairs of buildings between which it is impossible to build a tube. These pairs are ordered, so if it is impossible to build a tube from to , it may still be possible to build one from to .
Input Specification
The first line of input contains the three integers, , , and .
The following lines each contain two distinct integers and , meaning that there is already a tube from to .
The following lines each contain two distinct integers and , meaning that it is impossible to build a tube from to .
All of the ordered pairs in the input will be distinct. Note that and are regarded as different pairs.
Output Specification
If there is no solution, print NO
.
Otherwise, print lines, each containing two integers and , meaning that there should be a tube directed from to . You may print the tubes in any order. If there are multiple solutions, you may print any of them. Remember that all the already existing tubes must be included in your solution.
Limits and Scoring
- .
- .
- .
- for .
- for .
Your solution will be tested on a set of test groups, each worth a number of points. Each test group contains a set of test cases. To get the points for a test group you need to solve all test cases in the test group.
Group | Score | Limits |
---|---|---|
1 | 12 | and |
2 | 10 | and |
3 | 19 | |
4 | 13 | |
5 | 17 | It is guaranteed that there is a solution with as the root |
6 | 11 | |
7 | 18 | No additional constraints |
Example
The following figures show the first and second sample test cases. The blue edges mark tubes which are already constructed, and the dashed red edges mark tubes which are impossible to build.
The figure to the left shows the first sample with the solution from the sample output, showing tubes with black edges (in addition to the already constructed tube from to which is blue). In this network, all garbage will be collected in building . This is not the only solution; for example, the tube from to can be replaced by a tube from to and it is still a valid solution.
For the second sample input, we can see in the right figure that it is impossible to construct a solution because of the cycle .
Sample Input 1
5 1 8
4 1
3 1
3 4
3 2
0 2
0 4
2 4
1 0
2 0
Sample Output 1
4 1
3 0
1 3
2 3
Sample Input 2
5 4 0
1 0
2 3
3 4
4 2
Sample Output 2
NO
Sample Input 3
3 0 1
0 1
Sample Output 3
1 0
2 0
Sample Input 4
4 0 2
0 1
1 0
Sample Output 4
2 0
3 0
1 3
Comments