The Cow Doctor (bitset)

Texas is the state having the largest number of cows in the US: according to the 2005 report of the NationalAgricultural Statistics Service, the bovine population of Texas is 13.8 million. This is higher than thepopulation of the two runner-up states combined: there are only 6.65 million cows in Kansas and 6.35millions cows in Nebraska. 

There are several diseases that can threaten a herd of cows, the most feared being ``Mad Cow Disease" or Bovine Spongiform Encephalopathy (BSE); therefore, it is very important to be able to diagnose certain illnesses. Fortunately, there are many tests available that can be used to detect these diseases. 

A test is performed as follows. First a blood sample is taken from the cow, then the sample is mixed with a test material. Each test material detects a certain number of diseases. If the test material is mixed with a blood sample having any of these diseases, then a reaction takes place that is easy to observe. However, if a test material can detect several diseases, then we have no way to decide which of these diseases is present in the blood sample as all of them produce the same reaction. There are materials that detect many diseases (such tests can be used to rule out several diseases at once) and there are tests thatdetect only a few diseases (they can be used to make an accurate diagnosis of the problem). 

The test materials can be mixed to create new tests. If we have a test material that detects diseases A and B; and there is another test material that detects diseases B and C, then they can be mixed toobtain a test that detects diseases A, B, and C. This means that if we have these two test materials, then there is no need for a test material that tests diseases A, B, and C-such a material can be obtained bymixing these two. 

Producing, distributing, and storing many different types of test materials is very expensive, and inmost cases, unnecessary. Your task is to eliminate as many unnecessary test materials as possible. Ithas to be done in such a way that if a test material is eliminated, then it should be possible to mix an equivalent test from the remaining materials. (``Equivalent" means that the mix tests exactly the samediseases as the eliminated material, not more, not less).

Input

The input contains several blocks of test cases. Each case begins with a line containing two integers: the number 1 ≤ n ≤ 300 of diseases, and the number 1 ≤ m ≤ 200 of test materials. The next m lines correspond to the m test materials. Each line begins with an integer, the number 1 ≤ k ≤ 300 of diseases that the material can detect. This is followed by k integers describing the k diseases. These integers are between 1 and n . 

The input is terminated by a block with n = m = 0 .

Output

For each test case, you have to output a line containing a single integer: the maximum number of test materials that can be eliminated.

Sample Input

10 5
2 1 2
2 2 3
3 1 2 3
4 1 2 3 4
1 4
3 7
1 1
1 2 
1 3
2 1 2
2 1 3
2 3 2
3 1 2 3
0 0

Sample Output

2
4

Hint

Huge input file, 'scanf' recommended to avoid TLE. 

        本题大意是删掉无用的集合,无用的集合即为其他一些集合的并集。使用bitset,bitset是类似于vector的一种类模板,但是bitset需要规定长度,bitset中每一位的值都只能是0或者1。将每个集合都保存到一个bitset中,同时与之前所有的集合做比较,若两个集合存在包含关系,则记录到另一个bitset中,最后将两个bitset做比较,若相等则表示可以由一些别的集合并起来得到,可以删除。代码如下:

#include<iostream>
#include<cstdio>
#include<bitset> 
using namespace std;
const int maxn=305;
bitset<maxn>a[maxn];
bitset<maxn>b[maxn];
int main()
{
	int m,n,cnt,tmp;
	while(cin>>n>>m && (n||m))
	{
		for(int i=0;i<m;i++)//初始化
			a[i]=b[i]=0;
		for(int i=0;i<m;i++)
		{
			cin>>cnt;
			while(cnt--)
			{
				cin>>tmp;
				a[i].set(tmp);//保存当前集合的元素,给a[i]的第tmp位置1
			}
			for(int j=0;j<i;j++)
			{
				if((a[i]&a[j])==a[i])//如果a[i]是a[j]的子集,则将a[i]中值为1的位记录到b[j]中,若最后b[j]与a[j]相等,则表示j集合可以由一些集合并起来得到。
					b[j]|=a[i];
				else if((a[i]&a[j])==a[j])//操作同上
					b[i]|=a[j];
			}
		}
		int res=0;
		for(int i=0;i<m;i++)
			if(a[i]==b[i])//若a和b相等则表示当前集合可以由别的集合并起来得到,可以删掉
				res++;
		cout<<res<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Q_M_X_D_D_/article/details/83626255