Mocha and Diana (Easy Version) (Codeforces 1559D1)
Tags: Greedy, dsu

This is the easy 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 1000$$$, $$$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 (in each forest).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):
import sys
input = sys.stdin.readline

def solve():
    global s0, s1, c
    (n, m1, m2) = map(int, input().split())
    g1 = [[] for i in range(n + 1)]
    g2 = [[] for i in range(n + 1)]
    for i in range(m1):
        (u, v) = map(int, input().split())
        g1[u].append(v)
        g1[v].append(u)
    for i in range(m2):
        (u, v) = map(int, input().split())
        g2[u].append(v)
        g2[v].append(u)
    r = []
    w1 = [False] * (n + 1)
    w2 = [False] * (n + 1)
    c = [0] * (n + 1)
    s0 = set(list(range(1, n + 1)))
    s1 = set()
    i = 1
    q = [i]
    w1[i] = True
    c[i] += 1
    s0.remove(i)
    s1.add(i)
    qi = 0
    while qi < len(q):
        x = q[qi]
        qi += 1
        for v in g1[x]:
            if not w1[v]:
                w1[v] = True
                s0.remove(v)
                s1.add(v)
                c[v] += 1
                q.append(v)
    j = 0
    while j < len(q):
        t = q[j]
        if w2[t]:
            j += 1
            continue
        w2[t] = True
        c[t] += 1
        q1 = [t]
        qj = 0
        while qj < len(q1):
            x = q1[qj]
            qj += 1
            for v in g2[x]:
                if not w2[v]:
                    w2[v] = True
                    if c[v] == 0:
                        s0.remove(v)
                        s1.add(v)
                    c[v] += 1
                    q1.append(v)
        while len(s0) != 0:
            y = next(iter(s0))
            r.append((t, y))
            w1[y] = True
            s0.remove(y)
            s1.add(y)
            c[y] += 1
            q.append(y)
            while qi < len(q):
                x = q[qi]
                qi += 1
                for v in g1[x]:
                    if not w1[v]:
                        w1[v] = True
                        if c[v] == 0:
                            s0.remove(v)
                            s1.add(v)
                        c[v] += 1
                        q.append(v)
            g1[t].append(y)
            g1[y].append(t)
            if w2[y]:
                g2[t].append(y)
                g2[y].append(t)
                break
            q1.append(y)
            c[y] += 1
            w2[y] = True
            while qj < len(q1):
                x = q1[qj]
                qj += 1
                for v in g2[x]:
                    if not w2[v]:
                        w2[v] = True
                        if c[v] == 0:
                            s0.remove(v)
                            s1.add(v)
                        c[v] += 1
                        q1.append(v)
            g2[t].append(y)
            g2[y].append(t)
        for v in q1:
            c[v] -= 1
            if c[v] == 0:
                s1.remove(v)
                s0.add(v)
        j += 1
    print(len(r))
    for (u, v) in r:
        print(u, v)
solve()

Solution (Python):
import sys
input = sys.stdin.readline

def chc(x, v):
    if v == 1:
        if c[x] == 0:
            s0.remove(x)
            s1.add(x)
    elif v == -1:
        if c[x] == 1:
            s1.remove(x)
            s0.add(x)
    c[x] += v
    if c[x] < 0:
        raise Exception('<0')
    if c[x] > 2:
        raise Exception('>2')

def solve():
    global s0, s1, c
    (n, m1, m2) = map(int, input().split())
    g1 = [[] for i in range(n + 1)]
    g2 = [[] for i in range(n + 1)]
    for i in range(m1):
        (u, v) = map(int, input().split())
        g1[u].append(v)
        g1[v].append(u)
    for i in range(m2):
        (u, v) = map(int, input().split())
        g2[u].append(v)
        g2[v].append(u)
    r = []
    w1 = [False] * (n + 1)
    w2 = [False] * (n + 1)
    c = [0] * (n + 1)
    s0 = set(list(range(1, n + 1)))
    s1 = set()
    i = 1
    q = [i]
    w1[i] = True
    chc(i, 1)
    qi = 0
    while qi < len(q):
        x = q[qi]
        qi += 1
        for v in g1[x]:
            if not w1[v]:
                w1[v] = True
                chc(v, 1)
                q.append(v)
    j = 0
    while j < len(q):
        t = q[j]
        if w2[t]:
            j += 1
            continue
        w2[t] = True
        chc(t, 1)
        q1 = [t]
        qj = 0
        while qj < len(q1):
            x = q1[qj]
            qj += 1
            for v in g2[x]:
                if not w2[v]:
                    w2[v] = True
                    chc(v, 1)
                    q1.append(v)
        while len(s0) != 0:
            y = next(iter(s0))
            r.append((t, y))
            chc(y, 1)
            w1[y] = True
            q.append(y)
            while qi < len(q):
                x = q[qi]
                qi += 1
                for v in g1[x]:
                    if not w1[v]:
                        w1[v] = True
                        chc(v, 1)
                        q.append(v)
            g1[t].append(y)
            g1[y].append(t)
            if w2[y]:
                g2[t].append(y)
                g2[y].append(t)
                break
            q1.append(y)
            chc(y, 1)
            w2[y] = True
            while qj < len(q1):
                x = q1[qj]
                qj += 1
                for v in g2[x]:
                    if not w2[v]:
                        w2[v] = True
                        chc(v, 1)
                        q1.append(v)
            g2[t].append(y)
            g2[y].append(t)
        for v in q1:
            chc(v, -1)
        j += 1
    print(len(r))
    for (u, v) in r:
        print(u, v)
solve()

Solution (Python):
import sys
input = sys.stdin.readline

def chc(x, v):
    if v == 1:
        if c[x] == 0:
            s0.remove(x)
            s1.add(x)
    elif v == -1:
        if c[x] == 1:
            s1.remove(x)
            s0.add(x)
    c[x] += v
    if c[x] < 0:
        raise Exception('<0')
    if c[x] > 2:
        raise Exception('>2')

def solve():
    global s0, s1, c
    (n, m1, m2) = map(int, input().split())
    g1 = [[] for i in range(n + 1)]
    g2 = [[] for i in range(n + 1)]
    for i in range(m1):
        (u, v) = map(int, input().split())
        g1[u].append(v)
        g1[v].append(u)
    for i in range(m2):
        (u, v) = map(int, input().split())
        g2[u].append(v)
        g2[v].append(u)
    r = []
    w = [False] * (n + 1)
    w1 = [False] * (n + 1)
    w2 = [False] * (n + 1)
    c = [0] * (n + 1)
    s0 = set(list(range(1, n + 1)))
    s1 = set()
    i = 1
    q = [i]
    w1[i] = True
    chc(i, 1)
    qi = 0
    while qi < len(q):
        x = q[qi]
        qi += 1
        for v in g1[x]:
            if not w1[v]:
                w1[v] = True
                chc(v, 1)
                q.append(v)
    j = 0
    while j < len(q):
        t = q[j]
        w[t] = True
        if w2[t]:
            j += 1
            continue
        w2[t] = True
        chc(t, 1)
        q1 = [t]
        qj = 0
        while qj < len(q1):
            x = q1[qj]
            qj += 1
            for v in g2[x]:
                if not w2[v]:
                    w2[v] = True
                    chc(v, 1)
                    q1.append(v)
        while len(s0) != 0:
            y = next(iter(s0))
            r.append((t, y))
            chc(y, 1)
            w1[y] = True
            q.append(y)
            while qi < len(q):
                x = q[qi]
                qi += 1
                for v in g1[x]:
                    if not w1[v]:
                        w1[v] = True
                        chc(v, 1)
                        q.append(v)
            g1[t].append(y)
            g1[y].append(t)
            if w2[y]:
                g2[t].append(y)
                g2[y].append(t)
                break
            q1.append(y)
            chc(y, 1)
            w2[y] = True
            while qj < len(q1):
                x = q1[qj]
                qj += 1
                for v in g2[x]:
                    if not w2[v]:
                        w2[v] = True
                        chc(v, 1)
                        q1.append(v)
            g2[t].append(y)
            g2[y].append(t)
        for v in q1:
            chc(v, -1)
        j += 1
    print(len(r))
    for (u, v) in r:
        print(u, v)
solve()