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
SampleOutput
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>
相关文章推荐
- 欢迎使用CSDN-markdown编辑器
- [leetcode]Merge Sorted Array C语言
- $.each遍历json对象
- 基于Metronic的Bootstrap开发框架经验总结(4)--Bootstrap图标的提取和利用
- FTP
- 反射使用的方法工具示例代码
- [转]CSS3 Filter的十种特效
- iOS Life Cycle of a URL Session URLSession生命周期 开发者文档翻译(一)
- linux下c语言使用libcurl和实现文件上传
- 21点游戏c++版(一)
- 循环-01. 求整数段和
- 使用SVG中的Symbol元素制作Icon(链接地址)
- tomcat文件夹与文件解析
- 【Groovy】入门布道ppt
- zabbix利用orabbix监控oracle
- JVM内存管理:深入Java内存区域与OOM
- git简单使用
- centos6.5下安装mysql 5.6
- JAVA array,map 转 json 字符串
- Linux驱动中保存读写文件方法