#P9469. [EGOI 2023] Sopsug / 垃圾处理

    ID: 14381 远端评测题 5000ms 1024MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>2023Special JudgeO2优化EGOI(欧洲/女生)

[EGOI 2023] Sopsug / 垃圾处理

Background

Day 2 Problem C.

This statement is translated from EGOI2023 sopsug.

CC BY-SA 3.0

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 NN buildings, numbered from 00 to N1N-1. Your task is to connect some pairs of buildings with pipes. If you connect a pipe from building uu to vv, then uu will send all its waste to vv (but not the other way around). Your goal is to build a network of N1N-1 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 MM 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 KK 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 uu to vv, it may still be possible to build a pipe from vv to uu.

Input Format

The first line contains three integers N,M,KN, M, K.

The next MM lines each contain two distinct integers ai,bia_i, b_i, meaning there is already a pipe from aia_i to bib_i.

The next KK lines each contain two distinct integers ci,dic_i, d_i, meaning it is impossible to build a pipe from cic_i to did_i.

All M+KM+K ordered pairs are pairwise distinct. Note that (u,v)(u, v) and (v,u)(v, u) are considered two different pairs.

Output Format

If there is no solution, output NO.

Otherwise, output N1N-1 lines, each containing two integers ui,viu_i, v_i, meaning there is a pipe from uiu_i to viv_i. You may output the pipes in any order. If there are multiple solutions, you may output any of them. Remember that all MM 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 11 and 22.

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 11 and the solution given by the sample output. Black edges indicate newly built pipes. In this network, all waste will be collected in building 00. This is not the only solution; for example, the pipe from 11 to 33 can be replaced by the pipe from 00 to 11, and the result is still a valid solution.

For Sample 22, from the right figure we can see that due to the cycle (2,3,4)(2, 3, 4), the problem has no solution.


Constraints

For all testdata, 2N3×1052 \le N \le 3 \times 10^5, 0M3×1050 \le M \le 3 \times 10^5, 0K3×1050 \le K \le 3 \times 10^5, 0ai,biN10 \le a_i, b_i \le N-1, 0ci,diN10 \le c_i, d_i \le N-1.

  • Subtask 1 (1212 points): M=0M = 0, K=1K = 1.
  • Subtask 2 (1010 points): M=0M = 0, K=2K = 2.
  • Subtask 3 (1919 points): K=0K = 0.
  • Subtask 4 (1313 points): N100N \le 100.
  • Subtask 5 (1717 points): It is guaranteed that there exists a solution with 00 as the root.
  • Subtask 6 (1111 points): M=0M = 0.
  • Subtask 7 (1818 points): No additional constraints.

Translated by ChatGPT 5