#P9705. 「TFOI R1」Unknown Graph
「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 nodes. The nodes are labeled . You know the in-degree and out-degree of each node.
In addition, there are constraints. Each constraint gives two nodes and , meaning that the directed edge 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 , the number of nodes.
The second line contains integers , where the in-degree of node is .
The third line contains integers , where the out-degree of node is .
The fourth line contains an integer , the number of constraints.
In the next lines, each line contains two positive integers , representing one constraint.
Output Format
The first line contains a positive integer , the number of edges in a graph that satisfies the constraints.
In the next lines, each line contains two positive integers and , meaning there is a directed edge from node to node .
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): .
- Subtask 2 (10 points): , , .
- Subtask 3 (20 points): .
- Subtask 4 (60 points): no special constraints.
Constraints: For all testdata, , , , , .
Translated by ChatGPT 5