The Suspects(并查集)
2014-07-29 19:02
211 查看
Description
Severeacuterespiratorysyndrome(SARS),anatypicalpneumoniaofunknownaetiology,wasrecognizedasaglobalthreatinmid-March2003.Tominimizetransmissiontoothers,thebeststrategyistoseparatethesuspectsfromothers.
IntheNot-Spreading-Your-SicknessUniversity(NSYSU),therearemanystudentgroups.Studentsinthesamegroupintercommunicatewitheachotherfrequently,andastudentmayjoinseveralgroups.TopreventthepossibletransmissionsofSARS,theNSYSUcollects
thememberlistsofallstudentgroups,andmakesthefollowingruleintheirstandardoperationprocedure(SOP).
Onceamemberinagroupisasuspect,allmembersinthegrouparesuspects.
However,theyfindthatitisnoteasytoidentifyallthesuspectswhenastudentisrecognizedasasuspect.Yourjobistowriteaprogramwhichfindsallthesuspects.
Input
Theinputfilecontainsseveralcases.Eachtestcasebeginswithtwointegersnandminaline,wherenisthenumberofstudents,andmisthenumberofgroups.Youmayassumethat0<n<=30000and0<=m<=500.Everystudent
isnumberedbyauniqueintegerbetween0andn−1,andinitiallystudent0isrecognizedasasuspectinallthecases.Thislineisfollowedbymmemberlistsofthegroups,onelinepergroup.Eachlinebeginswithanintegerkbyitselfrepresentingthe
numberofmembersinthegroup.Followingthenumberofmembers,therearekintegersrepresentingthestudentsinthisgroup.Alltheintegersinalineareseparatedbyatleastonespace.
Acasewithn=0andm=0indicatestheendoftheinput,andneednotbeprocessed.
Output
Foreachcase,outputthenumberofsuspectsinoneline.
SampleInput
SampleOutput
Severeacuterespiratorysyndrome(SARS),anatypicalpneumoniaofunknownaetiology,wasrecognizedasaglobalthreatinmid-March2003.Tominimizetransmissiontoothers,thebeststrategyistoseparatethesuspectsfromothers.
IntheNot-Spreading-Your-SicknessUniversity(NSYSU),therearemanystudentgroups.Studentsinthesamegroupintercommunicatewitheachotherfrequently,andastudentmayjoinseveralgroups.TopreventthepossibletransmissionsofSARS,theNSYSUcollects
thememberlistsofallstudentgroups,andmakesthefollowingruleintheirstandardoperationprocedure(SOP).
Onceamemberinagroupisasuspect,allmembersinthegrouparesuspects.
However,theyfindthatitisnoteasytoidentifyallthesuspectswhenastudentisrecognizedasasuspect.Yourjobistowriteaprogramwhichfindsallthesuspects.
Input
Theinputfilecontainsseveralcases.Eachtestcasebeginswithtwointegersnandminaline,wherenisthenumberofstudents,andmisthenumberofgroups.Youmayassumethat0<n<=30000and0<=m<=500.Everystudent
isnumberedbyauniqueintegerbetween0andn−1,andinitiallystudent0isrecognizedasasuspectinallthecases.Thislineisfollowedbymmemberlistsofthegroups,onelinepergroup.Eachlinebeginswithanintegerkbyitselfrepresentingthe
numberofmembersinthegroup.Followingthenumberofmembers,therearekintegersrepresentingthestudentsinthisgroup.Alltheintegersinalineareseparatedbyatleastonespace.
Acasewithn=0andm=0indicatestheendoftheinput,andneednotbeprocessed.
Output
Foreachcase,outputthenumberofsuspectsinoneline.
SampleInput
1004 212 51013111214 201 2992 2002 15 512345 10 00
SampleOutput
4 1 1
#include<iostream>
usingnamespacestd;
intp[30100];
intfind(intx1)
{if(x1==p[x1])returnx1;
elsereturnp[x1]=find(p[x1]);
}
intmain()
{
intn,m,i,x,y,j;
while(cin>>m>>n,m!=0)
{for(i=0;i<=m;i++)
p[i]=i;
intn1;
for(i=0;i<n;i++)
{cin>>n1;
cin>>x;x=find(x);n1--;
while(n1--)
{
cin>>y;
y=find(y);
p[y]=x;
}
}
x=find(0);y=1;
for(j=1;j<=m;j++)
if(find(j)==x)
y++;
cout<<y<<endl;
}
return0;
}
相关文章推荐
- POJ-1611 The Suspects( 并查集 )
- POJ 1611 The Suspects(并查集)
- POJ 1611 The Suspects——并查集
- POJ1611 The Suspects (并查集)
- 并查集 POJ 1611 The Suspects
- POJ 1611 The Suspects 并查集
- poj 1611 The Suspects(并查集)
- poj 1611 The Suspects(并查集)
- POJ - 1611 The Suspects(并查集)
- 并查集 - 1611 The Suspects
- POJ 1611 The Suspects(并查集)
- POJ 1611 The Suspects【简单并查集】
- The Suspects (并查集)
- POJ 1611 The Suspects(并查集)
- POJ 1611 The Suspects (并查集)
- Pku oj 1611 The Suspects(并查集)
- POJ1611 The Suspects(并查集)
- [POJ](1611)The Suspects ---- 并查集
- POJ-1611-The Suspects(并查集)
- poj1611 The Suspects (并查集)