News Distribution (Codeforces 1167C)
Tags: Others, dsu

In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it. 

Input: The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) — the number of users and the number of groups of friends, respectively.Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) — the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group.It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$.

Output: Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it.


Sample Inputs:
7 5
3 2 5 4
0
2 1 2
1 1
2 6 7

Sample Outputs:
4 4 1 4 4 2 2

Solution (Python):
from sys import stdin

def find(parent, x):
    while parent[x] != x:
        parent[x] = parent[parent[x]]
        x = parent[x]
    return x

def solve():
    (N, M) = map(int, stdin.readline().split())
    parent = list(range(N + 1))
    for _ in range(M):
        group = [int(k) for k in stdin.readline().split()][1:]
        if group:
            p = find(parent, group[0])
            for x in group[1:]:
                parent[find(parent, x)] = p
    count = [0] * (N + 1)
    for i in range(1, N + 1):
        count[find(parent, i)] += 1
    print(' '.join(map(str, [count[find(parent, i)] for i in range(1, N + 1)])))
if __name__ == '__main__':
    solve()

Solution (Python):
N = 500 * 1000 + 13

def getP(a, p):
    if a == p[a]:
        return a
    p[a] = getP(p[a], p)
    return p[a]

def unite(a, b, p, rk):
    a = getP(a, p)
    b = getP(b, p)
    if a == b:
        return
    if rk[a] < rk[b]:
        (a, b) = (b, a)
    p[b] = a
    rk[a] += rk[b]
(n, m) = map(int, input().split())
p = [i for i in range(n)]
rk = [1] * n
for _ in range(m):
    (k, *nodes) = map(int, input().split())
    lst = -1
    for x in nodes:
        x -= 1
        if lst != -1:
            unite(x, lst, p, rk)
        lst = x
output = ' '.join((str(rk[getP(i, p)]) for i in range(n)))
print(output)

Solution (Python):
import sys

def find(x, pre):
    if x == pre[x]:
        return x
    else:
        pre[x] = find(pre[x], pre)
        return pre[x]

def merge(x, y, pre, size):
    xx = find(x, pre)
    yy = find(y, pre)
    if xx != yy:
        if size[xx] > size[yy]:
            (xx, yy) = (yy, xx)
        pre[xx] = yy
        size[yy] += size[xx]
(n, m) = map(int, sys.stdin.readline().split())
pre = [i for i in range(n + 1)]
size = [1 for _ in range(n + 1)]
for i in range(m):
    line = list(map(int, sys.stdin.readline().split()))[1:]
    for j in range(1, len(line)):
        merge(line[j - 1], line[j], pre, size)
a = [0] * (n + 1)
for i in range(1, n + 1):
    a[find(i, pre)] += 1
print(' '.join((str(a[find(i, pre)]) for i in range(1, n + 1))))