过程是模板 merge完后扫一下几个跟0同祖先节点就是答案了

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
const int MAX=;
int father[MAX];
int findfather(int x)
{
while(x!=father[x])
x=father[x];
return x;
}
void merge(int x,int y)
{
x=findfather(x);
y=findfather(y);
if(x!=y) father[x]=y;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m),n||m)
{
int ans=;
for(int i=;i<n;i++)
father[i]=i;
while(m--)
{
int k,fir,tmp;
scanf("%d%d",&k,&fir);
k--;
while(k--)
{
scanf("%d",&tmp);
merge(fir,tmp);
}
}
int ex=findfather();
for(int i=;i<n;i++)
if(findfather(i)==ex) ans++;
printf("%d\n",ans);
}
return ;
}

(似乎这个时候代码还很丑)

kuangbin_UnionFind B (POJ 1611)的更多相关文章

  1. poj 1611(并查集)

    http://poj.org/problem?id=1611 题意:有个学生感染病毒了,只要是和这个学生接触过的人都会感染,而和这些被感染者接触的人,也会被感染,现在给定你一些协会的人数,以及所在学生 ...

  2. poj 1611 The Suspects 解题报告

    题目链接:http://poj.org/problem?id=1611 题意:给定n个人和m个群,接下来是m行,每行给出该群内的人数以及这些人所对应的编号.需要统计出跟编号0的人有直接或间接关系的人数 ...

  3. poj 1611 The Suspects(简单并查集)

    题目:http://poj.org/problem?id=1611 0号是病原,求多少人有可能感染 #include<stdio.h> #include<string.h> # ...

  4. 【原创】poj ----- 1611 The Suspects 解题报告

    题目地址: http://poj.org/problem?id=1611 题目内容: The Suspects Time Limit: 1000MS   Memory Limit: 20000K To ...

  5. (并查集)The Suspects --POJ --1611

    链接: http://poj.org/problem?id=1611 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...

  6. POJ - 1611 The Suspects 【并查集】

    题目链接 http://poj.org/problem?id=1611 题意 给出 n, m 有n个人 编号为 0 - n - 1 有m组人 他们之间是有关系的 编号为 0 的人是 有嫌疑的 然后和 ...

  7. 【裸的并查集】POJ 1611 The Suspects

    http://poj.org/problem?id=1611 [Accepted] #include<iostream> #include<cstdio> #include&l ...

  8. 并查集 (poj 1611 The Suspects)

    原题链接:http://poj.org/problem?id=1611 简单记录下并查集的模板 #include <cstdio> #include <iostream> #i ...

  9. [并查集] POJ 1611 The Suspects

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 35206   Accepted: 17097 De ...

随机推荐

  1. 《c语言全局变量的用法》

    //全局变量的用法. /*有一个一维数组,内放n个学生的成绩,(n由用户自己指定,通过调用函数实现定义一个数组.)写一个函数,当主函数调用此函数后,能求出平均分,最高分,最低分.*/ #include ...

  2. (转)经典收藏 50个jQuery Mobile开发技巧集萃

    (原)http://www.cnblogs.com/chu888chu888/archive/2011/11/10/2244181.html 经典收藏 50个jQuery Mobile开发技巧集萃   ...

  3. JSP重定向传递参数

    我一个JSP程序,要实现前台提交数据给后台处理后,后台jsp自动跳转到另一个jsp页面,这种方式也叫重定向,重定向的方法有多种,暂时我试过的并且能成功的有两个: 一种是用 response.sendR ...

  4. 加载JS

  5. codevs 5429 完全背包

    单调队列优化. 好像有点烦...调了许久. #include<iostream> #include<cstdio> #include<cstring> #inclu ...

  6. Ubuntu 启动黑屏解决

    要sudo apt-get install xserver...................balabala...   then.... sudo gedit /boot/grub/grub.cf ...

  7. MonogoDB的GirdFS

    GirdFS是一种在MongoDB中存储大二进制文件的机制. mongofiles内置在MongoDB发布版中,可以用来在GridFS中上传.下载.列示.查找或删除文件. $ echo "H ...

  8. swift系统学习控件篇:UIProgressView+NSTimer+UIstepper+UIAlertController

    工作之余,学习下swift大法.把自己的学习过程分享一下.当中的布局很乱,就表在意这些细节了.直接上代码: UIProgressView+NSTimer+UIstepper UIStepper UIP ...

  9. CODEVS1533 互斥的数(哈希表)

    给定一个集合,要求一个最大子集,满足两两之间不互斥.对两个数x,y互斥的定义是,y=p*x. 先对集合中的数从小到大排序后线性扫,若一个数x可以取则取,取完之后p*x这个数不可取.由于数字较大,使用哈 ...

  10. HDU 1080

    http://acm.hdu.edu.cn/showproblem.php?pid=1080 二维最长公共子序列 #include <iostream> #include <cstd ...