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; ...
随机推荐
- detours安装和使用
http://blog.csdn.net/evi10r/article/details/6659354 http://blog.csdn.net/donglinshengan/article/deta ...
- Android 遍历界面控件
//遍历界面上的控件 fubin.pan LinearLayout sLinerLayout = (LinearLayout)findViewById(R.id.layout_scr); for (i ...
- [Android教程]EditText怎样限制用户的输入?数字/字母/邮箱
有输入必有验证.为了防止用户随便输入确保提交数据的合法性,程序不得不在文本输入框(EditText)中增加限制或验证. 关于输入类型有数字.字母.邮箱.电话等形式,这些具体得根据业务来.那么Andro ...
- mac os 安装 pkg-config
wget http://pkgconfig.freedesktop.org/releases/pkg-config-0.29.tar.gz . env LDFLAGS="-framework ...
- 翻译:非常详细易懂的法线贴图(Normal Mapping)
翻译:非常详细易懂的法线贴图(Normal Mapping) 本文翻译自: Shaders » Lesson 6: Normal Mapping 作者: Matt DesLauriers 译者: Fr ...
- make_head,,,pop_head,,,push_head,,,sort_head..
STL中,有很多的排序函数模板供我们调用,省去我们自己编写一些排序过程的麻烦.本文是一篇关于STL中堆排序的一个介绍. 本文涉及的几个函数如下:make_heap(), push_heap(), po ...
- C语言中%d,%o,%f,%e,%x的意义
printf(格式控制,输出列表) 格式控制包括格式说明和格式字符. 格式说明由“%”和格式字符组成,如%d%f等.它的作用是将输出的数据转换为指定的格式输出.格式说明总是由“%”字符开始的.不同类型 ...
- 再论pyquery
发现对于QQ群 空间文件的抓取毫无办法. QQ空间的代码可圈可点: 做了一个js的“客户端”,第一次加载时,将文件的列表信息全部抓取出来,然后基于js进行翻页和排序. 因此,想要抓取js渲染的dom, ...
- shell脚本步骤调试
第一种方式===> [root@localhost functions]# sh -x test.sh --check xx+ '[' --check '!=' -check ']'+ case ...
- JavaScript 在页面上显示数字时钟
显示一个钟表 拓展JavaScript计时:http://www.w3school.com.cn/js/js_timing.asp setTimeout() 方法会返回某个值.在下面的语句中,值被储存 ...