Mocha and Diana (Hard Version)
Contest: Codeforces Round 738 (Div. 2)
Difficulty rating: 2500
Tags: brute force, constructive algorithms, dfs and similar, dsu, graphs, greedy, trees, two pointers

This is the hard version of the problem. The only difference between the two versions is the constraint on $$$n$$$. You can make hacks only if all versions of the problem are solved.

A forest is an undirected graph without cycles (not necessarily connected).

Mocha and Diana are friends in Zhijiang, both of them have a forest with nodes numbered from $$$1$$$ to $$$n$$$, and they would like to add edges to their forests such that:

- After adding edges, both of their graphs are still forests.
- They add the same edges. That is, if an edge $$$(u, v)$$$ is added to Mocha's forest, then an edge $$$(u, v)$$$ is added to Diana's forest, and vice versa.

Mocha and Diana want to know the maximum number of edges they can add, and which edges to add.

Input format:
The first line contains three integers $$$n$$$, $$$m_1$$$ and $$$m_2$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le m_1, m_2 < n$$$) — the number of nodes and the number of initial edges in Mocha's forest and Diana's forest.

Each of the next $$$m_1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le n$$$, $$$u \neq v$$$) — the edges in Mocha's forest.

Each of the next $$$m_2$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le n$$$, $$$u \neq v$$$) — the edges in Diana's forest.

Output format:
The first line contains only one integer $$$h$$$, the maximum number of edges Mocha and Diana can add.

Each of the next $$$h$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le n$$$, $$$u \neq v$$$) — the edge you add each time.

If there are multiple correct answers, you can print any one of them.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first example, we cannot add any edge.

In the second example, the initial forests are as follows.

We can add an edge $$$(2, 4)$$$.

Example input:
3 2 2
1 2
2 3
1 2
1 3

Example output:
0

Example input:
5 3 2
5 4
2 1
4 3
4 3
1 4

Example output:
1
2 4

Editorial:
To have a clearer understanding, let's visualize the problem with a grid where each row is a component in the left forest and each column is a component in the right forest. For example, the cell (i,j) contains vertexes which belongs to $$$i^{th}$$$ component in the left forest and $$$j^{th}$$$ component in the right tree. (Some cells may be empty.) An operation corresponds to finding two filled cells in different rows and different columns, merging the two rows, and merging the two columns.
Now we need to make operation rapidly. For each row, we maintain a set of the different columns with filled cells in this row. Symmetrically, for each column we maintain a set of the different rows with filled cells in this column.
To merge two rows, we insert the elements of the smaller set into the larger set. The smaller set is now useless, and we delete it from every column in the set and insert the larger one. Since the data structure is symmetric, merging two columns is similar.
Without loss of generality, assume there are fewer rows than columns. If there is a row whose set has more than 1 element, we can pick it and any other row, and find an operation we can make. Otherwise if all rows are singletons, then we know the sets are all disjoint because there are more columns than rows. So we can pick any two sets and make an operation, and then there's a set with more than 1 element. Maintaining which rows have sets with size more than 1 is not hard.
For each operation, we need to output the corresponding vertex in the original two forest. Firstly, choose a vertex as the representative of each cell, because all vertexes in a cell can be seen as equivalent. Then, when merging two rows or columns, we just insert the representative vertexes at the same time.
There can be at most $$$2n$$$ merge operations and the total complexity of them will be $$$O(n\log^2 n)$$$. This is because an element moves to a new row/column $$$O(\log n)$$$ times and each move is $$$O(\log n)$$$ time (using STL set in cpp).
Overall, it's $$$O(n\log^2n)$$$time and $$$O(n\log n)$$$ space.