Euclid's nightmare (Codeforces 1466F)
Tags: Greedy, dsu

You may know that Euclid was a mathematician. Well, as it turns out, Morpheus knew it too. So when he wanted to play a mean trick on Euclid, he sent him an appropriate nightmare. In his bad dream Euclid has a set $$$S$$$ of $$$n$$$ $$$m$$$-dimensional vectors over the $$$\mathbb{Z}_2$$$ field and can perform vector addition on them. In other words he has vectors with $$$m$$$ coordinates, each one equal either $$$0$$$ or $$$1$$$. Vector addition is defined as follows: let $$$u+v = w$$$, then $$$w_i = (u_i + v_i) \bmod 2$$$. Euclid can sum any subset of $$$S$$$ and archive another $$$m$$$-dimensional vector over $$$\mathbb{Z}_2$$$. In particular, he can sum together an empty subset; in such a case, the resulting vector has all coordinates equal $$$0$$$.Let $$$T$$$ be the set of all the vectors that can be written as a sum of some vectors from $$$S$$$. Now Euclid wonders the size of $$$T$$$ and whether he can use only a subset $$$S'$$$ of $$$S$$$ to obtain all the vectors from $$$T$$$. As it is usually the case in such scenarios, he will not wake up until he figures this out. So far, things are looking rather grim for the philosopher. But there is hope, as he noticed that all vectors in $$$S$$$ have at most $$$2$$$ coordinates equal $$$1$$$. Help Euclid and calculate $$$|T|$$$, the number of $$$m$$$-dimensional vectors over $$$\mathbb{Z}_2$$$ that can be written as a sum of some vectors from $$$S$$$. As it can be quite large, calculate it modulo $$$10^9+7$$$. You should also find $$$S'$$$, the smallest such subset of $$$S$$$, that all vectors in $$$T$$$ can be written as a sum of vectors from $$$S'$$$. In case there are multiple such sets with a minimal number of elements, output the lexicographically smallest one with respect to the order in which their elements are given in the input. Consider sets $$$A$$$ and $$$B$$$ such that $$$|A| = |B|$$$. Let $$$a_1, a_2, \dots a_{|A|}$$$ and $$$b_1, b_2, \dots b_{|B|}$$$ be increasing arrays of indices elements of $$$A$$$ and $$$B$$$ correspondingly. $$$A$$$ is lexicographically smaller than $$$B$$$ iff there exists such $$$i$$$ that $$$a_j = b_j$$$ for all $$$j < i$$$ and $$$a_i < b_i$$$.

Input: In the first line of input, there are two integers $$$n$$$, $$$m$$$ ($$$1 \leq n, m \leq 5 \cdot 10^5$$$) denoting the number of vectors in $$$S$$$ and the number of dimensions. Next $$$n$$$ lines contain the description of the vectors in $$$S$$$. In each of them there is an integer $$$k$$$ ($$$1 \leq k \leq 2$$$) and then follow $$$k$$$ distinct integers $$$x_1, \dots x_k$$$ ($$$1 \leq x_i \leq m$$$). This encodes an $$$m$$$-dimensional vector having $$$1$$$s on coordinates $$$x_1, \dots x_k$$$ and $$$0$$$s on the rest of them.Among the $$$n$$$ vectors, no two are the same.

Output: In the first line, output two integers: remainder modulo $$$10^9+7$$$ of $$$|T|$$$ and $$$|S'|$$$. In the second line, output $$$|S'|$$$ numbers, indices of the elements of $$$S'$$$ in ascending order. The elements of $$$S$$$ are numbered from $$$1$$$ in the order they are given in the input.

Note: In the first example we are given three vectors:   $$$10$$$  $$$01$$$  $$$11$$$ It turns out that we can represent all vectors from our $$$2$$$-dimensional space using these vectors:   $$$00$$$ is a sum of the empty subset of above vectors;  $$$01 = 11 + 10$$$, is a sum of the first and third vector;  $$$10 = 10$$$, is just the first vector;  $$$11 = 10 + 01$$$, is a sum of the first and the second vector. Hence, $$$T = \{00, 01, 10, 11\}$$$. We can choose any two of the three vectors from $$$S$$$ and still be able to obtain all the vectors in $$$T$$$. In such a case, we choose the two vectors which appear first in the input. Since we cannot obtain all vectors in $$$T$$$ using only a single vector from $$$S$$$, $$$|S'| = 2$$$ and $$$S' = \{10, 01\}$$$ (indices $$$1$$$ and $$$2$$$), as set $$$\{1, 2 \}$$$ is lexicographically the smallest. We can represent all vectors from $$$T$$$, using only vectors from $$$S'$$$, as shown below:   $$$00$$$ is a sum of the empty subset;  $$$01 = 01$$$ is just the second vector;  $$$10 = 10$$$ is just the first vector;  $$$11 = 10 + 01$$$ is a sum of the first and the second vector. 

Sample Inputs:
3 2
1 1
1 2
2 2 1

2 3
2 1 3
2 1 2

3 5
2 1 2
1 3
1 4

Sample Outputs:
4 2
1 2

4 2
1 2

8 3
1 2 3

Solution (Python):
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):

    def __init__(self, file):
        self.newlines = 0
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = 'x' in file.mode or 'r' not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b'\n') + (not b)
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            (self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode('ascii'))
        self.read = lambda : self.buffer.read().decode('ascii')
        self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline()

def RL():
    return map(int, sys.stdin.readline().split())

def RLL():
    return list(map(int, sys.stdin.readline().split()))

def N():
    return int(input())

def S():
    return input().strip()

def print_list(l):
    print(' '.join(map(str, l)))

def find(region, u):
    path = []
    while region[u] != u:
        path.append(u)
        u = region[u]
    for v in path:
        region[v] = u
    return u

def union(region, u, v):
    (u, v) = (find(region, u), find(region, v))
    region[u] = v
    return u != v
M = 10 ** 9 + 7
(n, m) = RL()
region = list(range(m + 1))
ans = []
for i in range(1, n + 1):
    s = RLL()
    t = s[2] if s[0] == 2 else 0
    if union(region, s[1], t):
        ans.append(i)
res = 1
for _ in range(len(ans)):
    res = (res << 1) % M
print(res, len(ans))
print_list(ans)

Solution (Python):
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def unionfind(group, root):
    son = root
    while root != group[root]:
        root = group[root]
    while son != root:
        temp = group[son]
        group[son] = root
        son = group[temp]
    return root

def main(t):
    M = 10 ** 9 + 7
    (n, m) = map(int, input().split())
    group = [i for i in range(m + 1)]
    valid = []
    for i in range(n):
        arr = list(map(int, input().split()))
        if len(arr) == 2:
            root = unionfind(group, arr[1])
            if root == 0:
                continue
            group[root] = 0
            valid.append(i + 1)
        else:
            root1 = unionfind(group, arr[1])
            root2 = unionfind(group, arr[2])
            if root1 == root2:
                continue
            group[max(root1, root2)] = min(root1, root2)
            valid.append(i + 1)
    print(pow(2, len(valid), M), len(valid))
    print(' '.join(map(str, valid)))
main(1)

Solution (Python):
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):

    def __init__(self, file):
        self.newlines = 0
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = 'x' in file.mode or 'r' not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b'\n') + (not b)
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            (self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode('ascii'))
        self.read = lambda : self.buffer.read().decode('ascii')
        self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline()

def RL():
    return map(int, sys.stdin.readline().split())

def RLL():
    return list(map(int, sys.stdin.readline().split()))

def N():
    return int(input())

def S():
    return input().strip()

def print_list(l):
    print(' '.join(map(str, l)))

def find(region, u):
    path = []
    while u != region[u]:
        path.append(u)
        u = region[u]
    for v in path:
        region[v] = u
    return u

def union(region, u, v):
    (u, v) = (find(region, u), find(region, v))
    region[u] = v
    return u != v
(n, m) = RL()
(M, ans, res, region) = (10 ** 9 + 7, [], 1, list(range(m + 1)))
for i in range(1, n + 1):
    s = RLL()
    t = s[2] if s[0] == 2 else 0
    if union(region, s[1], t):
        ans.append(i)
        res = (res << 1) % M
print(res, len(ans))
print_list(ans)