human
I found an interesting problem on https://codeforces.com/problemset/problem/1290/C:
There are $n$ lamps on a line, numbered from $1$ to $n$. Each one has an initial state off ($0$) or on ($1$).

You're given $k$ subsets $A_1, \ldots, A_k$ of $\{1, 2, \dots, n\}$, such that the intersection of any three subsets is empty. In other words, for all $1 \le i_1 < i_2 < i_3 \le k$, $A_{i_1} \cap A_{i_2} \cap A_{i_3} = \varnothing$.

In one operation, you can choose one of these $k$ subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.

Let $m_i$ be the minimum number of operations you have to do in order to make the $i$ first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between $i+1$ and $n$), they can be either off or on.

You have to compute $m_i$ for all $1 \le i \le n$.

-----Input-----

The first line contains two integers $n$ and $k$ ($1 \le n, k \le 3 \cdot 10^5$).

The second line contains a binary string of length $n$, representing the initial state of each lamp (the lamp $i$ is off if $s_i = 0$, on if $s_i = 1$).

The description of each one of the $k$ subsets follows, in the following format:

The first line of the description contains a single integer $c$ ($1 \le c \le n$)  — the number of elements in the subset.

The second line of the description contains $c$ distinct integers $x_1, \ldots, x_c$ ($1 \le x_i \le n$)  — the elements of the subset.

It is guaranteed that:   The intersection of any three subsets is empty.
 It's possible to make all lamps be simultaneously on using some operations. 

-----Output-----

You must output $n$ lines. The $i$-th line should contain a single integer $m_i$  — the minimum number of operations required to make the lamps $1$ to $i$ be simultaneously on.

-----Examples-----
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3

Output
1
2
3
3
3
3
3

Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2

Output
1
1
1
1
1
1
4
4

Input
5 3
00011
3
1 2 3
1
4
3
3 4 5

Output
1
1
1
1
1

Input
19...

I tried it in Python, but could not do it. Can you solve it?
assistant
#Here is Python program to compute $m_i$ for all $1 \le i \le n$.

import sys
readline = sys.stdin.readline
class UF():
    def __init__(self, num):
        self.par = [-1]*num
        self.weight = [0]*num
    def find(self, x):
        stack = []
        while self.par[x] >= 0:
            stack.append(x)
            x = self.par[x]
        for xi in stack:
            self.par[xi] = x
        return x
    
    def union(self, x, y):
        rx = self.find(x)
        ry = self.find(y)
        if rx != ry:
            if self.par[rx] > self.par[ry]:
                rx, ry = ry, rx
            self.par[rx] += self.par[ry]
            self.par[ry] = rx
            self.weight[rx] += self.weight[ry]
        return rx

N, K = list(map(int, readline().split()))
S = list(map(int, readline().strip()))

A = [[] for _ in range(N)]

for k in range(K):
    BL = int(readline())
    B = list(map(int, readline().split()))
    for b in B:
        A[b-1].append(k)

cnt = 0
T = UF(2*K)
used = set()
Ans = [None]*N
inf = 10**9+7
for i in range(N):
    if not len(A[i]):
        Ans[i] = cnt
        continue
    kk = 0
    if len(A[i]) == 2:    
        x, y = A[i]
        if S[i]:
            rx = T.find(x)
            ry = T.find(y)
            if rx != ry:
                rx2 = T.find(x+K)
                ry2 = T.find(y+K)
                sp = min(T.weight[rx], T.weight[rx2]) + min(T.weight[ry], T.weight[ry2])
                if x not in used:
                    used.add(x)
                    T.weight[rx] += 1
                if y not in used:
                    used.add(y)
                    T.weight[ry] += 1
                rz = T.union(rx, ry)
                rz2 = T.union(rx2, ry2)
                sf = min(T.weight[rz], T.weight[rz2])
                kk = sf - sp
        else:
            rx = T.find(x)
            ry2 = T.find(y+K)
            sp = 0
            if rx != ry2:
                ry = T.find(y)
                rx2 = T.find(x+K)
                sp = min(T.weight[rx], T.weight[rx2]) + min(T.weight[ry], T.weight[ry2])
                if x not in used:
                   ...