Mocha and Diana (Hard Version) (Codeforces 1559D2)
Tags: Greedy, dsu

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: 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: 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.

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)$$$.

Sample Inputs:
3 2 2
1 2
2 3
1 2
1 3

5 3 2
5 4
2 1
4 3
4 3
1 4

8 1 2
1 7
2 6
1 5

Sample Outputs:
0

1
2 4

5
5 2
2 3
3 4
4 7
6 8

Solution (Python):
from sys import setrecursionlimit, stdin
import threading
input = stdin.buffer.readline

class DisjointSet:
    """"""

    def __init__(self, n):
        self.node = [x for x in range(n)]

    def find(self, v):
        """"""
        if self.node[v] == v:
            return v
        self.node[v] = self.find(self.node[v])
        return self.node[v]

    def merge(self, v1, v2):
        """"""
        v1 = self.find(v1)
        v2 = self.find(v2)
        if v1 != v2:
            self.node[v2] = v1

def work():
    (n, m1, m2) = map(int, input().split())
    forest1 = DisjointSet(n)
    forest2 = DisjointSet(n)
    for _ in range(m1):
        (a, b) = map(lambda x: int(x) - 1, input().split())
        forest1.merge(a, b)
    for _ in range(m2):
        (a, b) = map(lambda x: int(x) - 1, input().split())
        forest2.merge(a, b)
    ans = []
    for i in range(n):
        if forest1.find(i) != forest1.find(0) and forest2.find(i) != forest2.find(0):
            forest1.merge(i, 0)
            forest2.merge(i, 0)
            ans.append((1, i + 1))
    visited = [0 for x in range(n)]
    visited[0] = 1
    l = 0
    r = 0
    while True:
        while l < n and (forest1.find(l) == forest1.find(0) or visited[forest1.find(l)]):
            l += 1
        while r < n and (forest2.find(r) == forest2.find(0) or visited[forest2.find(r)]):
            r += 1
        if l == n or r == n:
            break
        visited[forest1.find(l)] = visited[forest2.find(r)] = 1
        ans.append((l + 1, r + 1))
    print(len(ans))
    for _ in ans:
        print(*_)
setrecursionlimit(10 ** 6)
threading.stack_size(10 ** 8)
threading.Thread(target=work).start()

Solution (Python):
from sys import setrecursionlimit, stdin
import threading
input = stdin.buffer.readline

class DisjointSet:
    """"""

    def __init__(self, n):
        self.node = [x for x in range(n)]

    def find(self, v):
        """"""
        if self.node[v] == v:
            return v
        self.node[v] = self.find(self.node[v])
        return self.node[v]

    def merge(self, v1, v2):
        """"""
        v1 = self.find(v1)
        v2 = self.find(v2)
        if v1 != v2:
            self.node[v2] = v1

def work():
    (n, m1, m2) = map(int, input().split())
    forest1 = DisjointSet(n)
    forest2 = DisjointSet(n)
    for _ in range(m1):
        (a, b) = map(lambda x: int(x) - 1, input().split())
        forest1.merge(a, b)
    for _ in range(m2):
        (a, b) = map(lambda x: int(x) - 1, input().split())
        forest2.merge(a, b)
    ans = []
    for i in range(n):
        if forest1.find(i) != forest1.find(0) and forest2.find(i) != forest2.find(0):
            forest1.merge(i, 0)
            forest2.merge(i, 0)
            ans.append((1, i + 1))
    visited = [0 for x in range(n)]
    visited[0] = 1
    l = 0
    r = 0
    while True:
        while l < n and (forest1.find(l) == forest1.find(0) or visited[forest1.find(l)]):
            l += 1
        while r < n and (forest2.find(r) == forest2.find(0) or visited[forest2.find(r)]):
            r += 1
        if l == n or r == n:
            break
        visited[forest1.find(l)] = visited[forest2.find(r)] = 1
        ans.append((l + 1, r + 1))
    print(len(ans))
    for _ in ans:
        print(*_)
setrecursionlimit(10 ** 6)
threading.stack_size(10 ** 8)
threading.Thread(target=work).start()

Solution (Python):
from sys import setrecursionlimit, stdin
import threading
input = stdin.buffer.readline

class DisjointSet:
    """"""

    def __init__(self, n):
        self.node = [x for x in range(n)]

    def find(self, v):
        """"""
        if self.node[v] == v:
            return v
        self.node[v] = self.find(self.node[v])
        return self.node[v]

    def merge(self, v1, v2):
        """"""
        v1 = self.find(v1)
        v2 = self.find(v2)
        if v1 != v2:
            self.node[v2] = v1

def work():
    (n, m1, m2) = map(int, input().split())
    forest1 = DisjointSet(n)
    forest2 = DisjointSet(n)
    for _ in range(m1):
        (a, b) = map(lambda x: int(x) - 1, input().split())
        forest1.merge(a, b)
    for _ in range(m2):
        (a, b) = map(lambda x: int(x) - 1, input().split())
        forest2.merge(a, b)
    ans = []
    for i in range(n):
        if forest1.find(i) != forest1.find(0) and forest2.find(i) != forest2.find(0):
            forest1.merge(i, 0)
            forest2.merge(i, 0)
            ans.append((1, i + 1))
    visited = [0 for x in range(n)]
    visited[0] = 1
    l = 0
    r = 0
    while True:
        while l < n and (forest1.find(l) == forest1.find(0) or visited[forest1.find(l)]):
            l += 1
        while r < n and (forest2.find(r) == forest2.find(0) or visited[forest2.find(r)]):
            r += 1
        if l == n or r == n:
            break
        visited[forest1.find(l)] = visited[forest2.find(r)] = 1
        ans.append((l + 1, r + 1))
    print(len(ans))
    for _ in ans:
        print(*_)
setrecursionlimit(10 ** 6)
threading.stack_size(10 ** 8)
threading.Thread(target=work).start()