您的位置:首页 > 其它

The Suspects

2015-08-27 16:14 267 查看
Description
Severeacuterespiratorysyndrome(SARS),anatypicalpneumoniaofunknownaetiology,wasrecognizedasaglobalthreatinmid-March2003.Tominimizetransmissiontoothers,thebeststrategyisto
separatethesuspectsfromothers.

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<=30000
and0<=m<=500.Everystudentisnumberedbyauniqueintegerbetween0andn−1,andinitiallystudent0isrecognizedasasuspectinallthecases.Thislineisfollowedbymmemberlistsofthegroups,onelinepergroup.Eachlinebeginswithaninteger
kbyitselfrepresentingthenumberofmembersinthegroup.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


/*思路:合并每一组里面的人+从头查询,并查集模版题。*/



<spanstyle="color:#000000;">#include<stdio.h>
#include<string.h>
#include<algorithm>
usingnamespacestd;
intper[30000];//数组开得太小会导致超时
inta[30000];
intn,m;
voidinit()
{
for(inti=0;i<n;i++)
per[i]=i;
}
intfind(intx)
{
intr=x;
while(r!=per[r])
r=per[r];
inti,j;
i=x;
while(i!=r)
{
j=per[i];
per[i]=r;
i=j;
}
returnr;
}
voidjoin(intx,inty)
{
intfx=find(x);
intfy=find(y);
if(fx!=fy)
per[fx]=fy;
}
intsame(intx,inty)
{
returnfind(x)==find(y);
}
intmain()
{
intt,i;
while(scanf("%d%d",&n,&m)&&(n||m))
{
init();
memset(a,0,sizeof(a));
while(m--)
{
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<t-1;i++)
join(a[i],a[i+1]);

}
intcount=0;
for(i=0;i<n;i++)
{
if(same(i,0))
count++;
}
printf("%d\n",count);
}
return0;

}
</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: