#P9469. [EGOI 2023] Sopsug / 垃圾处理
[EGOI 2023] Sopsug / 垃圾处理
Background
Day 2 Problem C.
This statement is translated from EGOI2023 sopsug.
Problem Description
Gravel Heap is an undeveloped residential area on the outskirts of Lund. Currently, all necessary infrastructure is being built, including the most important one: waste disposal. As in many parts of Sweden, an automatic vacuum collection system will be used to collect waste. The idea is to use air pressure to transport waste through underground pipes.
In Gravel Heap, there are buildings, numbered from to . Your task is to connect some pairs of buildings with pipes. If you connect a pipe from building to , then will send all its waste to (but not the other way around). Your goal is to build a network of pipes so that all waste eventually ends up in a single building. In other words, you want the whole network to be a rooted tree with all edges pointing towards the root.
However, there are already pipes built between buildings. These pipes must be included in your network. These pipes are directed and can only transport waste in one direction.
In addition, there are ordered pairs of buildings between which it is impossible to build a pipe. These pairs are ordered, so if it is impossible to build a pipe from to , it may still be possible to build a pipe from to .
Input Format
The first line contains three integers .
The next lines each contain two distinct integers , meaning there is already a pipe from to .
The next lines each contain two distinct integers , meaning it is impossible to build a pipe from to .
All ordered pairs are pairwise distinct. Note that and are considered two different pairs.
Output Format
If there is no solution, output NO.
Otherwise, output lines, each containing two integers , meaning there is a pipe from to . You may output the pipes in any order. If there are multiple solutions, you may output any of them. Remember that all existing pipes must be included in the answer.
5 1 8
4 1
3 1
3 4
3 2
0 2
0 4
2 4
1 0
2 0
4 1
3 0
1 3
2 3
5 4 0
1 0
2 3
3 4
4 2
NO
3 0 1
0 1
1 0
2 0
4 0 2
0 1
1 0
2 0
3 0
1 3
Hint
Explanation for Samples and .
The figure below shows the first two samples. Blue edges indicate existing pipes, and red dashed edges indicate pipes that cannot be built.
The left figure shows Sample and the solution given by the sample output. Black edges indicate newly built pipes. In this network, all waste will be collected in building . This is not the only solution; for example, the pipe from to can be replaced by the pipe from to , and the result is still a valid solution.
For Sample , from the right figure we can see that due to the cycle , the problem has no solution.

Constraints
For all testdata, , , , , .
- Subtask 1 ( points): , .
- Subtask 2 ( points): , .
- Subtask 3 ( points): .
- Subtask 4 ( points): .
- Subtask 5 ( points): It is guaranteed that there exists a solution with as the root.
- Subtask 6 ( points): .
- Subtask 7 ( points): No additional constraints.
Translated by ChatGPT 5
