UVA - 1197 (简单并查集计数)
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others. In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).
Once a member in a group is a suspect, all members in the group are suspects.
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.
Input
The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n ≤ 30000 and 0 ≤ m ≤ 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space. A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
Output
For each case, output the number of suspects in one line.
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
题目意思:
有n个人,m个学习,每个信息表示k个人曾经是一个团体(一个团体的人只要一人有病,团体所有人都可能患病)现在已经知道0号有病
问你有几个人可能会患病
分析:
简单的计数并查集
最后统计一些和0在一个集合里面的人数就是可能患病总人数
code:
#include <iostream>
#include<algorithm>
#include <cstdio>
#include<cstring>
#include<math.h>
#include<memory>
using namespace std;
typedef long long LL;
#define max_v 30000
int pa[max_v];
int rk[max_v];
int n,m;
void make_set(int x)
{
pa[x]=x;
rk[x]=;
}
int find_set(int x)
{
if(x!=pa[x])
pa[x]=find_set(pa[x]);
return pa[x];
}
void union_set(int x,int y)
{
x=find_set(x);
y=find_set(y);
if(x==y)
return ;
if(rk[x]>rk[y])
pa[y]=x;
else
{
pa[x]=y;
if(rk[x]==rk[y])
rk[y]++;
}
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
if(n==&&m==)
break;
for(int i=;i<=n-;i++)
make_set(i);
for(int i=;i<=m;i++)
{
int k;
scanf("%d",&k);
int x,y;
scanf("%d",&x);
for(int i=;i<=k-;i++)
{
scanf("%d",&y);
union_set(x,y);
}
}
int sum=;
for(int i=;i<=n-;i++)
{
if(find_set(i)==pa[])
sum++;
}
printf("%d\n",sum);
}
return ;
}
UVA - 1197 (简单并查集计数)的更多相关文章
- POJ 2524 (简单并查集) Ubiquitous Religions
题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰 简单并查集 //#define LOCAL #include <io ...
- poj1611 简单并查集
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 32781 Accepted: 15902 De ...
- 1213 How Many Tables(简单并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单并查集,统计单独成树的数量. 代码: #include <stdio.h> #i ...
- 【简单并查集】Farm Irrigation
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- ACM_“打老虎”的背后(简单并查集)
“打老虎”的背后 Time Limit: 2000/1000ms (Java/Others) Problem Description: “习大大”自担任国家主席以来大力反腐倡廉,各地打击贪腐力度也逐步 ...
- Codeforces Round #582 (Div. 3) G. Path Queries (并查集计数)
题意:给你带边权的树,有\(m\)次询问,每次询问有多少点对\((u,v)\)之间简单路径上的最大边权不超过\(q_i\). 题解:真的想不到用最小生成树来写啊.... 我们对边权排序,然后再对询问的 ...
- poj1988 简单并查集
B - 叠叠乐 Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:30000KB 64bit ...
- poj 2524 Ubiquitous Religions 一简单并查集
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 22389 Accepted ...
- ACM_城市交通线(简单并查集)
城市交通线 Time Limit: 2000/1000ms (Java/Others) Problem Description: A国有n座城市,编号为1~n,这n个城市之间没有任何交通线路,所以不同 ...
随机推荐
- 最短路问题(dijkstral 算法)(优化待续)
迪杰斯特拉算法是由荷兰计算机科学家狄克斯特拉于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中最短路径问题.迪杰斯特拉算法主要特点是以起始点为中心向 ...
- Generator函数自执行
Generator函数实现 function* gen(x){ var y = yield x + 2; return y } //1.调用g函数会返回一个内部的指针 var g = gen(1); ...
- C#与.NET的区别和C#程序结构
C#语言及其特点 (1)语法简洁,不允许直接操作做内存,去掉指针操作 (2)彻底的面向对象设计,C#具有面向对象所应用的一切特性:封装.继承.多态 (3)与Web紧密结合,C#支持绝大多数的Web标准 ...
- canvas toDataURL() 方法如何生成部分画布内容的图片
HTMLCanvasElement.toDataURL() 方法返回一个包含图片展示的 data URI .可以使用 type参数其类型,默认为 PNG 格式.图片的分辨率为96dpi. 如果画布的高 ...
- 解决react不能往setState中传key作为参数的办法(文章最后实现了传递key做参数的办法)
读者朋友可以直接看最后一个分割线下面的那部分!利用方括号语法来动态的访问对象的属性,实现当参数为属性名的传递; 有时候我们需要每次单独设置众多state中的一个,但是,都是进行相同的操作,这时候如果每 ...
- Shellinabox安装及使用教程
本文转载自: shellinabox:一款使用 AJAX 的基于 Web 的终端模拟器 一.shellinabox简介 通常情况下,我们在访问任何远程服务器时,会使用常见的通信工具如OpenSSH和P ...
- MySQL5.5登陆
通过cmd登陆 mysql -h localhost -P 3306 -u root -p123456 h后面跟的是域名或IP地址:大写的P后面跟的是端口号:u后面跟的是用户名:小写的p后面跟的是密码 ...
- Angular入门教程二
4 功能介绍 4.1数据绑定 AngularJS的双向数据绑定,意味着你可以在Mode(JS)中改变数据,而这些变动立刻就会自动出现在View上,反之亦然.即:一方面可以做到model变化驱动了DOM ...
- Java快速入门-02-基础篇
Java快速入门-02-基础篇 上一篇应该已经让0基础的人对 Java 有了一些了解,接一篇更进一步 使用 Eclipse 快捷键 这个老师一般都经常提,但是自己不容易记住,慢慢熟练 快捷键 快捷键作 ...
- MUI框架-03-自定义MUI控件样式
MUI框架-03-自定义MUI控件样式 开发请查阅:官方文档:http://dev.dcloud.net.cn/mui/ui/ 如何自定义MUI控件样式 mui 以 iOS 7的 UI 为基础,补充了 ...