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

  1. #include <iostream>
  2. #include <string>
  3. #include <cstdio>
  4. #include <cmath>
  5. #include <cstring>
  6. #include <queue>
  7. #include <map>
  8. #include <set>
  9. #include <algorithm>
  10. using namespace std;
  11. const int MAX=;
  12. int father[MAX];
  13. int findfather(int x)
  14. {
  15. while(x!=father[x])
  16. x=father[x];
  17. return x;
  18. }
  19. void merge(int x,int y)
  20. {
  21. x=findfather(x);
  22. y=findfather(y);
  23. if(x!=y) father[x]=y;
  24. }
  25. int main()
  26. {
  27. int n,m;
  28. while(scanf("%d%d",&n,&m),n||m)
  29. {
  30. int ans=;
  31. for(int i=;i<n;i++)
  32. father[i]=i;
  33. while(m--)
  34. {
  35. int k,fir,tmp;
  36. scanf("%d%d",&k,&fir);
  37. k--;
  38. while(k--)
  39. {
  40. scanf("%d",&tmp);
  41. merge(fir,tmp);
  42. }
  43. }
  44. int ex=findfather();
  45. for(int i=;i<n;i++)
  46. if(findfather(i)==ex) ans++;
  47. printf("%d\n",ans);
  48. }
  49. return ;
  50. }

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

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. Twitter CEO:有望进军中国 不会改变原则

    新浪科技讯 8月12日下午消息,据台湾“中央社”报道,Twitter CEO科斯特洛(Dick Costolo)日前接受<日经新闻>专访时指出,Twitter有望进军中国大陆,科斯特洛表示 ...

  2. python解无忧公主的数学时间097.py

    python解无忧公主的数学时间097.py """ python解无忧公主的数学时间097.py codegay 2016年3月30日 00:17:26 http:// ...

  3. VS2013开发Android App 环境搭建

    下载并安装vs2013,(安装时发现多了with blend,百度后有人说是设计师用版本,这是不对的,害我花费不少时间查找程序员用版本).我安装的是Microsoft Visual Studio Ul ...

  4. 理解Mach Port

    参考文档: 1. http://robert.sesek.com/thoughts/2012/1/debugging_mach_ports.html 2. Mach 3 Kernel Interfac ...

  5. PED结构获取进程路径和命令行地址

    1.FS寄存器 2.进入FS寄存器地址,7FFDD000 3.偏移30为PED结构 4.偏移地址10 3C,44偏移:路径地址,命令行地址 // 通过PEB结构去查找所有进程模块 void *PEB ...

  6. 另类加载dll---快捷方式启动参数

    http://blogs.360.cn/360safe/2014/08/29/cnc_trojan_and_fake_proto/ 原文: BMP1和BMP2是两个lnk文件(快捷方式),我们知道bm ...

  7. iOS APP上线流程

    前言:作为一名IOS开发者,把开发出来的App上传到App Store是必须的.下面就来详细介绍下具体流程. 1.打开苹果开发者中心:https://developer.apple.com 打开后点击 ...

  8. LITTLE SHOP OF FLOWERS_DP

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20802   Accepted: 9613 Description You ...

  9. Logger日志打印普通方法

    using System; using System.IO; using System.Text; namespace Core { public class LogHelper { private ...

  10. Cryptography加密和解密

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Se ...