Cow and Snacks (Codeforces 1209D)
Tags: Others, dsu

The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!There are $$$n$$$ snacks flavors, numbered with integers $$$1, 2, \ldots, n$$$. Bessie has $$$n$$$ snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows:  First, Bessie will line up the guests in some way.  Then in this order, guests will approach the snacks one by one.  Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad.  Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.

Input: The first line contains integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le 10^5$$$), the number of snacks and the number of guests. The $$$i$$$-th of the following $$$k$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i, y_i \le n$$$, $$$x_i \ne y_i$$$), favorite snack flavors of the $$$i$$$-th guest.

Output: Output one integer, the smallest possible number of sad guests.

Note: In the first example, Bessie can order the guests like this: $$$3, 1, 2, 4$$$. Guest $$$3$$$ goes first and eats snacks $$$1$$$ and $$$4$$$. Then the guest $$$1$$$ goes and eats the snack $$$2$$$ only, because the snack $$$1$$$ has already been eaten. Similarly, the guest $$$2$$$ goes up and eats the snack $$$3$$$ only. All the snacks are gone, so the guest $$$4$$$ will be sad. In the second example, one optimal ordering is $$$2, 1, 3, 5, 4$$$. All the guests will be satisfied.

Sample Inputs:
5 4
1 2
4 3
1 4
3 4

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

Sample Outputs:
1

0

Solution (Python):
def find(a, parent):
    while parent[a] != a:
        a = parent[a]
    return a

def union(a, b, parent, size):
    a_par = find(a, parent)
    b_par = find(b, parent)
    if a_par == b_par:
        return 1
    if size[a_par] < size[b_par]:
        parent[a_par] = b_par
        size[b_par] += size[a_par]
    else:
        parent[b_par] = a_par
        size[a_par] += size[b_par]
    return 0

def main():
    (N, Q) = map(int, input().split())
    parent = [i for i in range(N + 1)]
    size = [1 for _ in range(N + 1)]
    sad = 0
    for i in range(Q):
        (u, v) = map(int, input().split())
        sad += union(u, v, parent, size)
    print(sad)
if __name__ == '__main__':
    main()

Solution (Python):
def flood_fill(adj_lists, colors, i, color):
    q = set()
    q.add(i)
    while len(q) > 0:
        j = q.pop()
        if colors[j] is None:
            colors[j] = color
            for k in adj_lists[j]:
                q.add(k)

def main():
    (n, k) = map(int, input().split())
    adj_lists = [[] for _ in range(n)]
    for _ in range(k):
        (a, b) = map(int, input().split())
        a -= 1
        b -= 1
        adj_lists[a].append(b)
        adj_lists[b].append(a)
    colors = [None for _ in range(n)]
    num_colors = 0
    for i in range(n):
        if colors[i] is None:
            flood_fill(adj_lists, colors, i, num_colors)
            num_colors += 1
    print(k - n + num_colors)
if __name__ == '__main__':
    main()

Solution (Python):
import sys

def find(u, a):
    if a[u] != -1:
        a[u] = find(a[u], a)
        return a[u]
    else:
        return u

def union(a, u, v, ct):
    x = find(u, a)
    y = find(v, a)
    if x == y:
        ct += 1
    else:
        a[x] = y
        ct = ct
    return ct
(n, k) = map(int, input().split())
a = [-1 for i in range(n + 1)]
count = 0
for i in range(k):
    (u, v) = map(int, input().split())
    count = union(a, u, v, count)
print(count)