解题思路:一共给出 n个人,m组,接下来是m组数据,每一组开头是该组共有的人 num,则接下来输入的num个数,这些数是一组的

又因为最开始只有编号为0的人携带有病毒,且只有同一组的人会相互传染,问最后一共有多少人携带有病毒,则问题简化为求含有0那一组一共有多少个人。

反思:最开始把对应每输入一组的数据用数组来放,发现 int a[30001]编译不过;

然后就用一个while循环来输入,还有最开始得不到正确结果是因为把最后的判断条件写成了

if(find(i)==0)

sum++;

当时以为含有0的组的根节点就是0,其实0的根节点也不一定就是0,还应该再找一下0的根节点,应该改成

if(find(i)==find(0))

sum++;

Description

Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others.
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).
Once a member in a group is a suspect, all members in the group are suspects.
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.

Input

The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.

Output

For each case, output the number of suspects in one 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
#include<stdio.h>
int pre[30005];
int find(int root)
{
if(root!=pre[root])
{
pre[root]=find(pre[root]);
}
return pre[root];
}
void unionroot(int x,int y)
{
int root1,root2;
root1=find(x);
root2=find(y);
if(root1!=root2)
{
pre[root1]=root2;
}
} int main()
{
int n,m,num,k,t,i,p;
int x,y;
int sum;
while(scanf("%d %d",&n,&m)!=EOF&&(n||m))
{
sum=0;
for(i=0;i<n;i++)
pre[i]=i;
while(m--)
{
scanf("%d",&num);
scanf("%d",&x);
k=x;
if(num==1)
{
pre[x]=find(x);
}
else
{
p=num-1;
while(p--)
{
scanf("%d",&y);
t=y;
unionroot(k,y);
k=t;
}
}
} for(i=0;i<n;i++)
{
if(find(i)==find(0))
sum++; }
printf("%d\n",sum);
}
}

最新文章

  1. [转]extjs组件添加事件监听的三种方式
  2. Map的性能
  3. 字节流与数据类型的相互转换---使用struct模块
  4. C# Excel导入导出
  5. 演示save point及自治事务的用处
  6. MyBatis Generator自动生成的配置及使用
  7. MySQL支持的列类型
  8. struts2异常处理及类型转换
  9. HttpClient连接池抛出大量ConnectionPoolTimeoutException: Timeout waiting for connection异常排查
  10. Python 练习 —— 2048
  11. Android中使用JNI获得APK签名的哈希值
  12. 使用pager进行分页
  13. 设置查询对话框的F7
  14. DOM中document对象的常用属性方法
  15. word文字覆盖问题
  16. git commit -m 提交的内容换行
  17. mycat实现简单的mysql集群负载均衡
  18. Java 社区平台 - Sym 1.7.0 发布
  19. libcurl断点下载遇到的问题
  20. 由最小生成树(MST)到并查集(UF)

热门文章

  1. bootstrap modal 一闪
  2. Language Integrated Query
  3. javaScript 计算两个日期的天数相差
  4. 路飞学城Python-Day75
  5. js脚本捕获页面 GET 方式请求的参数?其实直接使用 window.location.search 获得
  6. [网络流24题] 最长k可重线段集问题 (费用流)
  7. [poj 3666] Making the Grade (离散化 线性dp)
  8. P1892 团伙
  9. seliux(类似防火墙,限制root用户)
  10. J2EE提高之知识清单