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命令别名的使用
语 法:alias[别名]=[指令名称] 1,查看该用户下的别名: alias 2,有的系统里没有ll这个命令,原因就是没有定义ll='ls -l --color=tty'这个别名 如果想永久生效,就 ...
- bootshrap会改变IE浏览器滚动条样式
在某个小网站的开发中 客户一直抱怨在IE11中网页右边滚动条不一样 后来发现在IE11中,有2个页面滚动条会自动隐藏,一开始以为是浏览器默认行为,改了overflow:scroll后也没有用.仔细观察 ...
- 理解javascript中的策略模式
理解javascript中的策略模式 策略模式的定义是:定义一系列的算法,把它们一个个封装起来,并且使它们可以相互替换. 使用策略模式的优点如下: 优点:1. 策略模式利用组合,委托等技术和思想,有效 ...
- Mac OS X【快捷键组合】汇总
以下是可能会出现在 OS X 菜单中的修饰键符号: ⌘—— () ⌃ ——Control ⌥—— (alt) ⇧——Shift ⇪——Caps Lock fn——功能键就是fn 启动快捷键 按下按键 ...
- HNU 12847 Dwarf Tower(最短路+队列优化)
题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12847 解题报告:有n样物品,编号从1到n第i样物品可以通过金 ...
- 第9章 使用ssh服务管理远程主机。
章节简述: 学习使用nmtui命令配置网卡参数.手工将多块网卡做绑定.使用nmcli命令查看网卡信息和使用ss命令查看网络及端口状态. 完整演示sshd服务配置方法并详细讲述每个参数的作用,实战基于密 ...
- 他们在军训,我在搞 OI(四)
(怎么自动变成两天一更了?) ——因为我菜啊 T_T Day 5 今天上午刷得爽啊!5 道 NOIP,前四题直接 1A,然而最后一题还是 WA 了一发才 A... 第一题是个简单的贪心,题意大概是 n ...
- 2015安徽省赛 B.求和
题目描述 对于正整数n,k,我们定义这样一个函数f,它满足如下规律 现在给出n和k,你的任务就是要计算f(n,k)的值. 输入 首先是一个整数T,表示有T组数据 接下来每组数据是n和k(1<=n ...
- Shortcut key for CodeBlocks
一.not only in CodeBlocks 13.12 Undo last action Ctrl + Z //后退 Redo last action Ctrl + Shift + Z //前进 ...
- BZOJ 1452 [JSOI2009] Count
这道题好像有点简单的样子... absi找题目好厉害啊...确实是一道比较裸的2dBIT啊. 水掉吧. 附:2dBIT怎么做: 2dBIT就是BIT套BIT啦. 所以修改loop(x+=lowbit( ...