POJ 1611 The Suspects(简单并查集)
#include<iostream>
#include<cstdio>
using namespace std;
int fa[30010]; void makeSet(int n) //初始化,n个元素,处于单独集合
{
for(int i=0;i<n;i++)
fa[i]=i;
} int findSet(int x) //找到该点的粑粑
{
return fa[x]=fa[x]==x?x:findSet(fa[x]);
} void unionSet(int x,int y) //粑粑相同的合并成过一个集合
{
int a=findSet(x),b=findSet(y);
if(a!=b)
fa[b]=a;
} int main()
{
int n,m,k,g[30010];
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0) break;
makeSet(n);
for(int i=0;i<m;i++)
{
scanf("%d",&k);
for(int j=0;j<k;j++)
scanf("%d",&g[j]);
for(int j=1;j<k;j++)
unionSet(g[j-1],g[j]);
}
int sum=0;
for(int i=0;i<n;i++)
{
if(findSet(fa[0])==findSet(fa[i]))
sum++;
}
printf("%d\n",sum);
}
return 0;
}
POJ 1611 The Suspects(简单并查集)的更多相关文章
- poj 1611 The Suspects(简单并查集)
题目:http://poj.org/problem?id=1611 0号是病原,求多少人有可能感染 #include<stdio.h> #include<string.h> # ...
- POJ 1611 The Suspects(并查集,简单)
为什么ACM的题意都这么难懂,就不能说的直白点吗?还能不能好好的一起刷题了? 题意:你需要建一个n的并查集,有m个集合,最后要输出包含0的那个集合的元素的个数. 这是简单并查集应用,所以直接看代码吧! ...
- poj 1611:The Suspects(并查集,经典题)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21472 Accepted: 10393 De ...
- POJ 1611 The Suspects (并查集)
The Suspects 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/B Description 严重急性呼吸系统综合症( S ...
- poj 1611 The Suspects(并查集)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21598 Accepted: 10461 De ...
- POJ - 1611 The Suspects 【并查集】
题目链接 http://poj.org/problem?id=1611 题意 给出 n, m 有n个人 编号为 0 - n - 1 有m组人 他们之间是有关系的 编号为 0 的人是 有嫌疑的 然后和 ...
- POJ 1611 The Suspects【并查集】
解题思路:一共给出 n个人,m组,接下来是m组数据,每一组开头是该组共有的人 num,则接下来输入的num个数,这些数是一组的 又因为最开始只有编号为0的人携带有病毒,且只有同一组的人会相互传染,问最 ...
- poj 2524 Ubiquitous Religions(简单并查集)
对与知道并查集的人来说这题太水了,裸的并查集,如果你要给别人讲述并查集可以使用这个题当做例题,代码中我使用了路径压缩,还是有一定优化作用的. #include <stdio.h> #inc ...
- POJ1611(The Suspects)--简单并查集
题目在这里 关于SARS病毒传染的问题.在同一个组的学生是接触很近的,后面也会有新的同学的加入.其中有一位同学感染SARS,那么该组的所有同学得了SARS.要计算出有多少位学生感染SARS了.编号为0 ...
- POJ 2524 (简单并查集) Ubiquitous Religions
题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰 简单并查集 //#define LOCAL #include <io ...
随机推荐
- sqlserver和Windows资源管理器争用内存
sqlserver和Windows资源管理器在设置成相同的优先级的情况下(普通),Windows资源管理器优先于sqlserver对内存的征用.开始是
- Linux常用命令汇总及使用方法(一)
最近开始学习Linux,但是因为原来没有接触过,所有现在先将Linux中常用的命令记录下来,方便自己后期使用. 1.登陆 通过secureCRT/xshell/ssh等软件远程登陆Linux (1)# ...
- Angular之Providers (Value, Factory, Service and Constant )
官方文档Providers Each web application you build is composed of objects that collaborate to get stuff do ...
- chrom扩展学习
详细教程-- http://www.ituring.com.cn/minibook/950
- scala正则表达式
正则表达式 Scala 通过 scala.util.matching 包种的 Regex 类来支持正则表达式 scala.util.matching.Regex.构造一个Regex对象, ...
- 博客word测试
博客word测试 博客word测试 from __future__ import division, print_functionDOCLINES = (__doc__ or '').split(&q ...
- android 5.0 -- Ripple 效果
Ripple 水波纹效果,也就是涟漪效果. 波纹效果有两种: 1,波纹有边界:波纹涟漪效果只是显示在控件内部 android:background="?android:attr/select ...
- Struts2第四天
Struts2第四天 昨天: 自定义的拦截器:继续methodFilterInterceptor,可以指定哪些方法需要拦截或者不拦截. Intercepters(配置拦截器),intercepter( ...
- Linux下查看access访问日志IP统计命令
查看 access.Log 文件ip统计 cat access.log |awk '{print $1}'|uniq -c |sort -k1,1nr 去掉r则从高到低 cat access.log ...
- hdu1026
#include <stdio.h> #include <string.h> #include <queue> using namespace std; struc ...