Good Bye 2020 F. Euclid‘s nightmare(图论,并查集)

题目描述

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 Z2 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 wi=(ui+vi)mod2.
Euclid can sum any subset of S and archive another m-dimensional vector over Z2. 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 Z2 that can be written as a sum of some vectors from S. As it can be quite large, calculate it modulo 109+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 a1,a2,…a|A| and b1,b2,…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 aj=bj for all j<i and ai<bi.

Input

In the first line of input, there are two integers n, m (1≤n,m≤5⋅105) 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≤k≤2) and then follow k distinct integers x1,…xk (1≤xi≤m). This encodes an m-dimensional vector having 1s on coordinates x1,…xk and 0s on the rest of them.
Among the n vectors, no two are the same.

Output

In the first line, output two integers: remainder modulo 109+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.

Examples

input
3 2
1 1
1 2
2 2 1
output
4 2
1 2
input
2 3
2 1 3
2 1 2
output
4 2
1 2
input
3 5
2 1 2
1 3
1 4
output
8 3
1 2 3

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.

题目大意

给出n个m维向量,这些向量可以进行模2的加法。这n个向量最多只有两个位置上是1,其余位置上都为0。
求这n个向量进行模2加法一共能够得到多少种向量(这个向量集合为T),以及最少需要n个向量中的哪几个,也可得到集合T。并给出这组向量的编号(多个的话要字典序最小的解)。

题目分析

因为每个向量都最多只有两个位置(假设这两个位置上的数分别为x和y)上有1,我们可以在x和y上连一条边(因为存在只有一个位置上有1的向量,因此我们可以设一个虚拟点,在该位置与虚拟点之间连一条边)。

这样连完之后就能形成一个图。如果图中有a条边形成了一个环,那么说明这个环中只有a-1条有效边(即a-1个有效向量)
那么最终有效向量的数量即为:m-图中的环数。
因此可以得到的向量种数T即为:2有效向量的个数

问题就转化为了求图中有多少环,这个问题我们可以用并查集来解决。

代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <queue>
#include <vector>
#include <set>
#include <bitset>
#include <algorithm>
#define LL long long
#define PII pair<int,int>
#define x first
#define y second
using namespace std;
const int N=5e5+5,mod=1e9+7;
int p[N];
vector<int> ans;
int find(int x)					//并查集模板
{
    
    
	if(p[x]!=x) p[x]=find(p[x]);
	return p[x];
}
int main()
{
    
    
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m+1;i++) p[i]=i;
	for(int i=1;i<=n;i++)
	{
    
    
		int k;
		scanf("%d",&k);
		int x,y=m+1;				//虚拟点设为m+1
		scanf("%d",&x);
		if(k==2) scanf("%d",&y);
		x=find(x),y=find(y);		//找到x和y所在的集合
		if(x!=y)
		{
    
    
			ans.push_back(i);		//如果x和y不在同一集合,那么说明它为有效向量,用ans记录
			p[x]=y;					//并将其加入同一集合中去
		}
	}
	int cnt=1;
	for(int i=0;i<ans.size();i++) cnt=(cnt<<1)%mod;		//T中的个数即为2^有效向量的数量
	printf("%d %d\n",cnt,ans.size());
	for(int i=0;i<ans.size();i++)				//输出这些有效向量的编号
		printf("%d ",ans[i]);
	return 0; 
}

猜你喜欢

转载自blog.csdn.net/li_wen_zhuo/article/details/113958085