POJ 1611:The Suspects(并查集)
| Time Limit: 1000MS | Memory Limit: 20000K | |
| Total Submissions: 48327 | Accepted: 23122 |
Description
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
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
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
题意:有n个点和m个点集,每个点集有k个点,每个点集中的个点可以相连,求与0相连的点的个数(包括0)。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define ll long long
#define INF 0x3f3f3f3f
const int maxn=1e5+10;
int a[maxn];
int sum[maxn];
int ans;
int n,m;
int k,x,y;
int find(int x)
{
if(x!=a[x])
{
return find(a[x]);
}
return a[x];
}
void join(int x,int y)
{
int dx=find(x);
int dy=find(y);
//对dx(或dy)所在的点集进行个数统计
if(dx!=dy)
{
a[dy]=dx;
sum[dx]+=sum[dy];
}
}
int main(int argc, char const *argv[])
{
while(std::cin>>n>>m&&n||m)
{
memset(a,0,sizeof(a));
memset(sum,0,sizeof(sum));
ans=1;
for(int i=0;i<n;i++)
{
sum[i]=1;
a[i]=i;
}
while(m--)
{
std::cin>>k;
std::cin>>x;
k--;
while(k--)
{
std::cin>>y;
join(x,y);
}
}
//输出0所在点集的个数
std::cout<<sum[find(0)]<<std::endl;
}
return 0;
}
POJ 1611:The Suspects(并查集)的更多相关文章
- poj 1611 The Suspects(并查集输出集合个数)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- poj 1611 The Suspects 并查集变形题目
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 20596 Accepted: 9998 D ...
- POJ 1611 The Suspects (并查集+数组记录子孙个数 )
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 24134 Accepted: 11787 De ...
- POJ 1611 The Suspects (并查集求数量)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- POJ 1611 The Suspects 并查集 Union Find
本题也是个标准的并查集题解. 操作完并查集之后,就是要找和0节点在同一个集合的元素有多少. 注意这个操作,须要先找到0的父母节点.然后查找有多少个节点的额父母节点和0的父母节点同样. 这个时候须要对每 ...
- poj 1611 The Suspects 并查集
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 30522 Accepted: 14836 De ...
- [ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21586 Accepted: 10456 De ...
- 并查集 (poj 1611 The Suspects)
原题链接:http://poj.org/problem?id=1611 简单记录下并查集的模板 #include <cstdio> #include <iostream> #i ...
- [并查集] POJ 1611 The Suspects
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 35206 Accepted: 17097 De ...
- poj 1611:The Suspects(并查集,经典题)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21472 Accepted: 10393 De ...
随机推荐
- django-from
构建一个表单 这是一个非常简单的表单.实际应用中,一个表单可能包含几十上百个字段,其中大部分需要预填充,而且我们预料到用户将来回编辑-提交几次才能完成操作. 我们可能需要在表单提交之前,在浏览器端作一 ...
- 2.3 The Object Model -- Computed Properties
一.What are computed properties? 1. 简而言之,计算属性让你声明函数为属性.你通过定义一个计算属性作为一个函数来创建一个,当你请求这个属性时,Ember会自动调用这个f ...
- Q_OBJECT宏的作用
The Q_OBJECT macro at the beginning of the class definition is necessary for all classes that define ...
- forEach方法的实现
var arr = [1, 23, 1, 1, 1, 3, 23, 5, 6, 7, 9, 9, 8, 5]; Array.prototype.forEach = Array.prototype.fo ...
- 《大话设计模式》ruby版代码:建造者模式
需求: 画一个小人,有头,有身体,两手两脚即可. 初始代码: # -*- encoding: utf-8 -*- #小人一 puts '这是第一个小人' puts '小人一:头' puts '小人一: ...
- 33Sql数据删除与遍历
数据库的创建.添加.修改.查询.删除都是利用SQL语句和类QSqlQuery的结合. QSqlDatabase::database().可返回当前正在打开的数据库对象. 数据库的删除 //获取删除的名 ...
- WPF usercontrol 自定义依赖属性
1.依赖属性不同意一般属性,一般属性主要定义在对象中,而依赖属性是存在一个特殊的依赖属性表中.2.当我们触发改变值时,需要通过SetValue这种方式进行触发. UserControl1.xaml: ...
- Centos75 firewalld防火墙
Centos75 防火墙iptables被firewalld取代 #启动firewalld systemctl start firewalld #查看firewalld systemctl statu ...
- 20145201李子璇《网络对抗》PC平台逆向破解
20145201<网络对抗>PC平台逆向破解 准备阶段 下载安装execstack. 获取shellcode的c语言代码 设置堆栈可执行 将环境设置为:堆栈可执行.地址随机化关闭(2开启, ...
- Qt无法调试Qvector
现象: 解决: 打开文件 $(VSDIR)\Common7\Packages\Debugger\autoexp.dat (VSDIR是本机Visual Studio的安装目录)把定义QVector和Q ...