Title Description
Novel coronavirus pneumonia (Corona Virus Disease 2019, COVID-19), referred to as "the new crown pneumonia" refers to the 2019 novel coronavirus infection caused by pneumonia.
If an infected person into a group, then this group need to be isolated!
A small student was diagnosed with the new crown infection, and does not wear a mask! ! ! ! ! !
Risk! ! !
Time is running out! ! ! !
Need to find as quickly as possible to all students and small A direct or indirect contact through the students, they will be isolated to prevent the spread of a wider range.
As we all know, students' communication may be in small groups, a student may also participate in multiple small groups.
Please write a program to solve! wear mask! !
Enter a description
A plurality of sets of data, for each set of test data:
The first two acts of integers n and m (n = m = 0 indicates the end of input, no treatment), n is the number of students, m is the number of groups of students. 0 <n <= 3e4, 0 <= m <= 5e2
Student numbered 0 ~ n-1
A small number is 0
Then, m lines, each person has a number of small groups i.e. integer num. Followed by a num integer representing students in small groups.
Output Specifications
The number of output to be isolated, the answer to the output of each data per line
Sample Input
100 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
200 2
1 5
5 1 2 3 4 5
1 0
0 0
Sample Output
4
1
1
Thinking
Used here and check the set of ideas, whether direct or indirect contact with the student, the students merger process, students first set of the same group is the same, if there are two groups of students intersection, then the two sets combined, at the same time each record the number of students in the collection, the final output of the set where the number of students 0 students can be.
For a plurality of sets of input data, a data processing required before each of disjoint-set initialization process.
Code
#include <iostream>
#include<string.h>
using namespace std;
const int size=3e4+10;
int v[size];
int number[size];
void ini(int n)
{
for(int i=0;i<n;i++)
v[i]=i,number[i]=1;
}
int find(int n)
{
return v[n]==n?n:v[n]=find(v[n]);
}
bool unite(int a,int b)
{
int fa=find(a),fb=find(b);
if(fa==fb)
{
return false;
}
else
{
if(number[fa]<number[fb])swap(fa,fb);
number[fa]+=number[fb];
v[fb]=fa;
return true;
}
}
int main(int argc, char** argv) {
int m,n;
while(~scanf("%d%d",&n,&m)&&!(m==0&&n==0))
{
ini(n);
int sum=0;
for(int i=0;i<m;i++)
{
int num,temp=-1;
scanf("%d",&num);
for(int j=0;j<num;j++)
{
int a;scanf("%d",&a);
if(temp!=-1)unite(temp,a);
temp=a;
}
}
printf("%d\n",number[find(0)]);
}
return 0;
}