题目在这里

关于SARS病毒传染的问题。在同一个组的学生是接触很近的,后面也会有新的同学的加入。其中有一位同学感染SARS,那么该组的所有同学得了SARS。要计算出有多少位学生感染SARS了。编号为0的同学是得了SARS的。
直接用并查集解决水掉
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<vector>
  6. #include<stack>
  7. #include<map>
  8. #include<set>
  9. #include<list>
  10. #include<queue>
  11. #include<string>
  12. #include<algorithm>
  13. #include<iomanip>
  14. using namespace std;
  15. #define MAX 100
  16.  
  17. struct node
  18. {
  19. int no;//编号
  20. int rank;
  21. int parent;
  22. int total;
  23. };
  24.  
  25. class DisJoinSet
  26. {
  27. protected:
  28. int n;
  29. node * tree;
  30. public:
  31. DisJoinSet(int n );
  32. ~DisJoinSet();
  33. void Init();
  34. void Union(int x,int y);
  35. int Find(int x);
  36. int GetAswer(int x);
  37. };
  38.  
  39. DisJoinSet ::DisJoinSet(int n)//初始化操作
  40. {
  41. this->n = n;
  42. tree = new node[n];
  43. for(int i = ; i < n; i++)
  44. {
  45. tree[i].no = i;
  46. tree[i].parent = i;
  47. tree[i].total = ;
  48. tree[i].rank = ;
  49. }
  50. }
  51. DisJoinSet::~DisJoinSet()
  52. {
  53. delete[] tree;
  54. }
  55.  
  56. void DisJoinSet :: Init()
  57. {
  58. }
  59. int DisJoinSet::Find(int x)
  60. {
  61. int temp = tree[x].parent;//temp 为x的父亲结点
  62. if( x != tree[x].parent)
  63. {
  64. tree[x].parent = Find(tree[x].parent);//路径压缩
  65. return tree[x].parent;
  66. }
  67. else
  68. {
  69. return x;
  70. }
  71. }
  72.  
  73. void DisJoinSet ::Union(int x,int y)
  74. {
  75. int rootx = Find(x);
  76. int rooty = Find(y);
  77. if(rootx == rooty)
  78. {
  79. return ;
  80. }
  81. else//并查集基本操作
  82. {
  83. if(tree[rootx].rank < tree[rooty].rank)
  84. {
  85. tree[rootx].parent = rooty;
  86. tree[rooty].total += tree[rootx].total;
  87. }
  88. else
  89. {
  90. tree[rooty].parent = rootx;
  91. tree[rootx].total += tree[rooty].total;
  92. if(tree[rootx].rank == tree[rooty].rank)
  93. {
  94. tree[rootx].rank++;
  95. }
  96. }
  97. }
  98. }
  99.  
  100. int DisJoinSet::GetAswer(int x)//返回xtotal
  101. {
  102. int t = Find(x);
  103. return tree[t].total;
  104. }
  105.  
  106. int main()
  107. {
  108. int n;
  109. int m;
  110. while(cin>>n>>m && n!= && m>= )
  111. {
  112. DisJoinSet dis(n);
  113. int per1;
  114. int per2;
  115. for(int i = ; i< m;i++)
  116. {
  117. int num = ;
  118. cin>>num;
  119. cin>>per1;
  120. for(int i = ;i < num; i++)
  121. {
  122. cin>>per2;
  123. dis.Union(per1,per2);
  124. }
  125. }
  126. cout<<dis.GetAswer()<<endl;
  127. }
  128. return ;
  129. }
 

POJ1611(The Suspects)--简单并查集的更多相关文章

  1. poj1611 The Suspects(并查集)

    题目链接 http://poj.org/problem?id=1611 题意 有n个学生,编号0~n-1,m个社团,每个社团有k个学生,如果社团里有1个学生是SARS的疑似患者,则该社团所有人都要被隔 ...

  2. POJ1611 The Suspects (并查集)

    本文出自:http://blog.csdn.net/svitter 题意:0号学生染病,有n个学生,m个小组.和0号学生同组的学生染病,病能够传染. 输入格式:n,m 数量  学生编号1,2,3,4 ...

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

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

  4. poj1611 The suspects【并查集】

    严重急性呼吸系统综合症( SARS), 一种原因不明的非典型性肺炎,从2003年3月中旬开始被认为是全球威胁.为了减少传播给别人的机会, 最好的策略是隔离可能的患者. 在Not-Spreading-Y ...

  5. poj1611 简单并查集

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 32781   Accepted: 15902 De ...

  6. POJ 1611 The Suspects(并查集,简单)

    为什么ACM的题意都这么难懂,就不能说的直白点吗?还能不能好好的一起刷题了? 题意:你需要建一个n的并查集,有m个集合,最后要输出包含0的那个集合的元素的个数. 这是简单并查集应用,所以直接看代码吧! ...

  7. POJ 2524 (简单并查集) Ubiquitous Religions

    题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰 简单并查集 //#define LOCAL #include <io ...

  8. 1213 How Many Tables(简单并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单并查集,统计单独成树的数量. 代码: #include <stdio.h> #i ...

  9. 【简单并查集】Farm Irrigation

    Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tot ...

随机推荐

  1. Flutter 中AlertDialog确认提示弹窗

    import 'package:flutter/material.dart'; import 'dart:async'; enum Action { Ok, Cancel } class AlertD ...

  2. nginx通过robots.txt禁止所有蜘蛛访问(禁止搜索引擎收录)

    在server {} 块中添加下面的配置 location =/robots.txt { default_type text/html; add_header Content-Type "t ...

  3. ES6深入浅出-4 迭代器与生成器-2.Symbol 和迭代器

    symbol https://zhuanlan.zhihu.com/p/22652486 Es5中的数据类型,所有的复杂类型都是对象类型. ES6里面增加了symbol类型,用处不多. https:/ ...

  4. ISCSI多路径配置(二)

    搭建iscsi存储系统(一) (1).配置ISCSI多路径实现磁盘挂载高可用 如果存储服务器到交换机只有一条线路的时候,那么一条线路出现故障,整个就没法使用了,所以多线路可以解决这个问题,避免单点故障 ...

  5. ip地址分类和网段详解

    IP地址分类/IP地址10开头和172开头和192开头的区别/判断是否同一网段 简单来说在公司或企业内部看到的就基本都是内网IP,ABC三类IP地址里的常见IP段. 每个IP地址都包含两部分,即网络号 ...

  6. pipline中替换tag变量

    实验架构: 192.168.0.96 gitlab 192.168.0.97 jenkins 192.168.0.98 harbor.docker集群 说明:下面代码编译镜像那一步的代码必须靠左,目的 ...

  7. 迅速生成项目-react-scripts

    推荐指数:

  8. Beta冲刺(3/4)

    队名:秃头小队 组长博客 作业博客 组长徐俊杰 过去两天完成的任务:学习了很多东西 Github签入记录 接下来的计划:继续学习 还剩下哪些任务:后端部分 燃尽图 遇到的困难:自己太菜了 收获和疑问: ...

  9. mysql 报错ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)

    ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ ( ...

  10. linux 文件描述符表 打开文件表 inode vnode

      在Linux中,进程是通过文件描述符(file descriptors,简称fd)而不是文件名来访问文件的,文件描述符实际上是一个整数.Linux中规定每个进程能最多能同时使用NR_OPEN个文件 ...