#P9705. 「TFOI R1」Unknown Graph

    ID: 9557 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>网络流Special JudgeO2优化图论建模

「TFOI R1」Unknown Graph

Background

Little A drifted to an archipelago. These islands are connected by one-way bridges. There are no two bridges with the same starting island and ending island, and there is also no bridge that connects an island to itself.

However, everything is covered in fog. On one island, Little A can only see how many bridges are connected to this island.

Little A wants to know the overall shape of the archipelago, but he cannot, so he came to you for help.

If there are multiple possible cases, you only need to tell Little A any one of them.

Problem Description

There is a directed graph with no multiple edges and no self-loops (it may be disconnected) with nn nodes. The nodes are labeled 1n1 \sim n. You know the in-degree and out-degree of each node.

In addition, there are mm constraints. Each constraint gives two nodes xix_{i} and yiy_{i}, meaning that the directed edge (xi,yi)(x_{i}, y_{i}) does not exist in the graph. Please find one graph structure that satisfies all requirements.

If there are multiple solutions, output any one. It is guaranteed that a solution exists.

Input Format

The first line contains a positive integer nn, the number of nodes.

The second line contains nn integers aia_{i}, where the in-degree of node ii is aia_{i}.

The third line contains nn integers bib_{i}, where the out-degree of node ii is bib_{i}.

The fourth line contains an integer mm, the number of constraints.

In the next mm lines, each line contains two positive integers xi,yix_{i}, y_{i}, representing one constraint.

Output Format

The first line contains a positive integer kk, the number of edges in a graph that satisfies the constraints.

In the next kk lines, each line contains two positive integers uiu_{i} and viv_{i}, meaning there is a directed edge from node uiu_{i} to node viv_{i}.

4
2 3 2 3
2 3 2 3
1
1 3
10
1 2
2 1
2 3
3 2
2 4
4 2
4 1
1 4
4 3
3 4

Hint

This problem uses bundled tests.

  • Subtask 1 (10 points): n10n \leqslant 10.
  • Subtask 2 (10 points): n=103n = 10^3, ai=bi=1a_{i} = b_{i} = 1, m=0m = 0.
  • Subtask 3 (20 points): n100n \leqslant 100.
  • Subtask 4 (60 points): no special constraints.

Constraints: For all testdata, 2n1032 \leqslant n \leqslant 10^{3}, 0ai,bi<n0 \leqslant a_{i}, b_{i} < n, 1ai1051\leqslant \sum{a_i} \leqslant 10^{5}, 0m5×1040 \leqslant m \leqslant 5 \times 10^4, 1xi,yin1 \leqslant x_i,y_i \leqslant n.

Translated by ChatGPT 5