Prefix Enlightenment
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$$$.
In the first example:   For $$$i = 1$$$, we can just apply one operation on $$$A_1$$$, the final states will be $$$1010110$$$;  For $$$i = 2$$$, we can apply operations on $$$A_1$$$ and $$$A_3$$$, the final states will be $$$1100110$$$;  For $$$i \ge 3$$$, we can apply operations on $$$A_1$$$, $$$A_2$$$ and $$$A_3$$$, the final states will be $$$1111111$$$. In the second example:   For $$$i \le 6$$$, we can just apply one operation on $$$A_2$$$, the final states will be $$$11111101$$$;  For $$$i \ge 7$$$, we can apply operations on $$$A_1, A_3, A_4, A_6$$$, the final states will be $$$11111111$$$. 
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. 
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.
import sys

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

def merge(x, y):
    x = find(x)
    y = find(y)
    if x != y:
        pre[x] = y
        siz[y] += siz[x]

def cal(x):
    return min(siz[find(x)], siz[find(x + k)])

def Solve(i):
    global ans
    cant = col[i][0]
    if cant == 2:
        x = col[i][1]
        y = col[i][2]
        if S[i] == 1:
            if find(x) == find(y):
                return
            ans -= cal(x) + cal(y)
            merge(x, y)
            merge(x + k, y + k)
            ans += cal(x)
        else:
            if find(x) == find(y + k):
                return
            ans -= cal(x) + cal(y)
            merge(x, y + k)
            merge(x + k, y)
            ans += cal(x)
    elif cant == 1:
        x = col[i][1]
        if S[i] == 1:
            if find(x) == find(0):
                return
            ans -= cal(x)
            merge(x, 0)
            ans += cal(x)
        else:
            if find(x + k) == find(0):
                return
            ans -= cal(x)
            merge(x + k, 0)
            ans += cal(x)
(n, k) = map(int, input().split())
S = [1] + list(map(int, list(sys.stdin.readline().strip())))
col = [[0 for _ in range(3)] for _ in range(n + 2)]
pre = [i for i in range(k * 2 + 1)]
siz = [0 for _ in range(k * 2 + 1)]
ans = 0
for i in range(1, k + 1):
    c = sys.stdin.readline()
    c = int(c)
    conjunto = [1] + list(map(int, list(sys.stdin.readline().split())))
    for j in range(1, len(conjunto)):
        x = conjunto[j]
        col[x][0] = col[x][0] + 1
        col[x][col[x][0]] = i
for i in range(1, k + 1):
    siz[i] = 1
siz[0] = 3 * 1000000.0
for i in range(1, n + 1):
    Solve(i)
    print(ans)
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def get_root(s):
    v = []
    while not s == root[s]:
        v.append(s)
        s = root[s]
    for i in v:
        root[i] = s
    return s

def unite(s, t):
    (rs, rt) = (get_root(s), get_root(t))
    if not rs ^ rt:
        return
    if rank[s] == rank[t]:
        rank[rs] += 1
    if rank[s] >= rank[t]:
        root[rt] = rs
        size[rs] += size[rt]
    else:
        root[rs] = rt
        size[rt] += size[rs]
    cnt[get_root(s)] = min(cnt[rs] + cnt[rt], inf)
    return

def same(s, t):
    return True if get_root(s) == get_root(t) else False

def get_size(s):
    return size[get_root(s)]
(n, k) = map(int, input().split())
s = list(input().rstrip())
c1 = [0] * (n + 1)
c2 = [0] * (n + 1)
for i in range(1, k + 1):
    c = int(input())
    x = list(map(int, input().split()))
    for j in x:
        if not c1[j]:
            c1[j] = i
        else:
            c2[j] = i
root = [i for i in range(2 * k + 1)]
rank = [1 for _ in range(2 * k + 1)]
size = [1 for _ in range(2 * k + 1)]
cnt = [0] * (2 * k + 1)
for i in range(1, k + 1):
    cnt[i] = 1
ans = []
ans0 = 0
inf = pow(10, 9) + 1
for (u, v, w) in zip(c1[1:], c2[1:], s):
    if u:
        ans0 -= min(cnt[get_root(u)], cnt[get_root(u + k)])
    if v and (not same(u, v)) and (not same(u, v + k)):
        ans0 -= min(cnt[get_root(v)], cnt[get_root(v + k)])
    if w & 1 and u:
        if not v:
            cnt[get_root(u)] = inf
        else:
            unite(u, v)
            unite(u + k, v + k)
    elif not w & 1 and u:
        if not v:
            cnt[get_root(u + k)] = inf
        else:
            unite(u, v + k)
            unite(u + k, v)
    if u:
        ans0 += min(cnt[get_root(u)], cnt[get_root(u + k)])
    ans.append(ans0)
for i in range(n - 2, -1, -1):
    ans[i] = min(ans[i], ans[i + 1])
sys.stdout.write('\n'.join(map(str, ans)))
from sys import stdin

class disjoinSet(object):

    def __init__(self, n):
        self.father = [x for x in range(0, n + 1)]
        self.rank = [0 for x in range(0, n + 1)]

    def setOf(self, x):
        if self.father[x] != x:
            self.father[x] = self.setOf(self.father[x])
        return self.father[x]

    def Merge(self, x, y):
        xR = self.setOf(x)
        yR = self.setOf(y)
        if xR == yR:
            return
        if self.rank[xR] < self.rank[yR]:
            self.father[xR] = yR
            size[yR] += size[xR]
        elif self.rank[xR] > self.rank[yR]:
            self.father[yR] = xR
            size[xR] += size[yR]
        else:
            self.father[yR] = xR
            size[xR] += size[yR]
            self.rank[xR] += 1

def LessSize(x):
    return min(size[dsu.setOf(x)], size[dsu.setOf(x + k)])

def Solve(i):
    global ans
    cant = col[i][0]
    if cant == 2:
        x = col[i][1]
        y = col[i][2]
        if S[i] == 1:
            if dsu.setOf(x) == dsu.setOf(y):
                return
            ans -= LessSize(x) + LessSize(y)
            dsu.Merge(x, y)
            dsu.Merge(x + k, y + k)
            ans += LessSize(y)
        else:
            if dsu.setOf(x) == dsu.setOf(y + k):
                return
            ans -= LessSize(x) + LessSize(y)
            dsu.Merge(x, y + k)
            dsu.Merge(x + k, y)
            ans += LessSize(y)
    elif cant == 1:
        x = col[i][1]
        if S[i] == 1:
            if dsu.setOf(x) == dsu.setOf(0):
                return
            ans -= LessSize(x)
            dsu.Merge(x, 0)
            ans += LessSize(x)
        else:
            if dsu.setOf(x + k) == dsu.setOf(0):
                return
            ans -= LessSize(x)
            dsu.Merge(x + k, 0)
            ans += LessSize(x)
(n, k) = map(int, input().split())
S = [1] + list(map(int, list(stdin.readline().strip())))
dsu = disjoinSet(k * 2 + 1)
col = [[0 for _ in range(3)] for _ in range(n + 2)]
size = [0 for _ in range(k * 2 + 1)]
ans = 0
for i in range(1, k + 1):
    c = stdin.readline()
    c = int(c)
    conjunto = [1] + list(map(int, list(stdin.readline().split())))
    for j in range(1, len(conjunto)):
        x = conjunto[j]
        col[x][0] = col[x][0] + 1
        col[x][col[x][0]] = i
for i in range(1, k + 1):
    size[i] = 1
size[0] = 3 * 1000000.0
for i in range(1, n + 1):
    Solve(i)
    print(ans)