The Suspects 简单的并查集
Description
Input
Output
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<cstdio>
#include<iostream> using namespace std;
int me[];
int find(int a)
{
while(me[a]!=a)
a=me[a];
return a;
}
void build(int a,int b)
{
int fa=find(a);
int fb=find(b);
if(fa!=fb)
{
if(me[fa]>=me[fb])
{
me[fa]=fb;
}
else
{
me[fb]=fa;
}
}
else return;
}
int main()
{ int n,m;
while((scanf("%d%d",&n,&m))!=EOF&&(n+m!=))
{
for(int i=;i<;i++)
me[i]=i;
for(int i=;i<m;i++)
{
int t;
cin>>t;
int p1,p2;
cin>>p1;
for(int j=;j<t;j++)
{
cin>>p2;
build(p1,p2);
//cout<<p1<<"sss"<<p2<<endl;
}
}
int ans=;
for(int i=;i<n;i++)
if(find(i)==) ans++;
cout<<ans<<endl;
}
return ;
}
连搜索都不用优化的简单并查集。
The Suspects 简单的并查集的更多相关文章
- The Suspects(简单的并查集)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- hdu 1182 A Bug's Life(简单种类并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 题意:就是给你m条关系a与b有性关系,问这些关系中是否有同性恋 这是一道简单的种类并查集,而且也 ...
- poj 1611 :The Suspects经典的并查集题目
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...
- 【POJ】The Suspects(裸并查集)
并查集的模板题,为了避免麻烦,合并的时候根节点大的合并到小的结点. #include<cstdio> #include<algorithm> using namespace s ...
- UVA - 1160(简单建模+并查集)
A secret service developed a new kind of explosive that attain its volatile property only when a spe ...
- hdu 1213 (How Many Tables)(简单的并查集,纯模板)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- POJ——1611The Suspects(启发式并查集+邻接表)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 31100 Accepted: 15110 Descri ...
- [原]poj-1611-The Suspects(水并查集)
题目链接:http://poj.org/problem?id=1611 题意:输入n个人,m个组.初始化0为疑似病例.输入m个小组,每组中只要有一个疑似病例,整组人都是疑似病例.相同的成员可以在不同的 ...
- 1213 How Many Tables 简单的并查集问题
my code: #include <cstdio>#include <cstring>#include<iostream>using namespace std; ...
随机推荐
- linux cp命令参数及用法详解
cp (复制档案或目录)[root@linux ~]# cp [-adfilprsu] 来源档(source) 目的檔(destination)[root@linux ~]# cp [options] ...
- jQuery1.11源码分析(9)-----初始化jQuery对象的函数和关联节点获取函数
这篇也没什么好说的,初始化jQuery对象的函数要处理多种情况,已经被寒冬吐槽烂了.关联节点获取函数主要基于两个工具函数dir和sibling,前者基于指定的方向遍历,后者则遍历兄弟节点(真的不能合并 ...
- FineUI第一天
示例:http://fineui.com/demo/ 文档:http://fineui.com/doc/ 下载:http://fineui.codeplex.com/ 1.fineUI基于extjs, ...
- [Effective JavaScript 笔记]第60条:支持方法链
无状态的API的部分能力是将复杂操作分解为更小的操作的灵活性.一个很好的例子是字符串的replace方法.由于结果本身也是字符串,可以对前一个replace操作重复执行替换.这种模式的一个常见用例是在 ...
- Apache服务器常规操作
导读 Apache是世界上排名第一的Web服务器,50%以上的Web服务器都在使用Apache,它几乎可以在所有计算机平台上运行.下面就由我给大家说说Apache服务器的一些常规操作. Apache服 ...
- TCP/IP协议原理【转载】
前述 各种L2数据网具有不同的通信协议与帧结构,其网络节点设备可以是各种类型的数据交换机(X.25.FR.Ethernet和ATM等分组交换机):而L3数据网(IP网或internet) ...
- python string与list互转
因为python的read和write方法的操作对象都是string.而操作二进制的时候会把string转换成list进行解析,解析后重新写入文件的时候,还得转换成string. >>&g ...
- JS的trim()方法
去除字符串左右两端的空格,在vbscript里面可以轻松地使用 trim.ltrim 或 rtrim,但在js中却没有这3个内置方法,需要手工编写.下面的实现方法是用到了正则表达式,效率不错,并把这三 ...
- SQL小技巧小知识
1.[ ]的使用 当我们所要查的表是系统关键字或者表名中含有空格时,需要用[]括起来,例如新建了两个表,分别为user,user info,那么select * from user和select * ...
- mysql cluster 运行的必备条件
1.由于同步复制一共需要4次消息传递,故mysql cluster的数据更新速度比单机mysql要慢.所以mysql cluster要求运行在千兆以上的局域网内,节点可以采用双网卡,节点组之间采用直 ...