并查集 (poj 1611 The Suspects)
原题链接:http://poj.org/problem?id=1611
简单记录下并查集的模板
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
#define ll long long
#define pi 3.1415927
using namespace std;
int fat[];
int finds(int n)
{ //查找
if(fat[n]==n)
return n;
return fat[n]=finds(fat[n]); //路径压缩
}
void join(int a, int b)
{ //合并
int j=finds(a), k=finds(b);
if(j<k)
fat[k]=j;
else
fat[j]=k;
}
int main ()
{
int n,m,i,t,j,k,n2,a,b;
while(scanf("%d %d",&n,&m)&&n+m)
{
for(i=;i<n;++i)
fat[i]=i;
while(m--)
{
scanf("%d %d",&n2,&a);
for(i=;i<n2;++i){
scanf("%d",&b);
join(a,b);
}
}
int sum=;
for(i=;i<n;++i)
if(finds(i)==)
sum++;
printf("%d\n",sum);
}
return ;
}
并查集 (poj 1611 The Suspects)的更多相关文章
- [并查集] 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 ...
- POJ 1611 The Suspects (并查集)
The Suspects 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/B Description 严重急性呼吸系统综合症( S ...
- poj 1611 The Suspects(并查集)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21598 Accepted: 10461 De ...
- 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经典的并查集题目
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...
随机推荐
- [转载]Spring AOP 深入剖析
转载自 http://www.cnblogs.com/digdeep/p/4528353.html 多谢@digdeep AOP是Spring提供的关键特性之一.AOP即面向切面编程,是OOP编程的有 ...
- spring MVC 转发与重定向(传参)
return "forward:index.jsp"; //转发 return "forward:user.do?method=reg5"; //转发 ret ...
- 判断APP是否已安装
NSString *str = [NSString stringWithFormat:@"%@://%@",[dic objectForKey:@"ios_url_sch ...
- final修饰和StringBuffer的几个案例(拼接,反转,对称操作)
final关键字修饰时如果是基本数据类型的变量,则其数值一旦在初始化之后便不能更改:如果是引用类型的变量,则在对其初始化之后便不能再让其指向另一个对象,但引用变量不能变,引用变量所指向的对象中的内容还 ...
- 威布尔weibull distribution
data = wblrnd(0.5,0.8,100,1); 生成威布尔随机函数,尺寸参数为0.5,形状参数为0.8,生成数列100行,一列: parmhat = wblfit(data) 对data的 ...
- sql (8) AVG
SQL avg 语法SELECT AVG(column_name) FROM table_name新建表:StudentS S_id Grade Name phone1 98 小明 1234562 9 ...
- mysql出现You can’t specify target table for update in FROM clause
在mysql执行下面语句时报错: You can’t specify target table for update in FROM clause UPDATE edu_grade_hgm_1 ' W ...
- ROS 自定义消息类型方法
流程 1.在package中新建文件夹名为msg 2.在msg文件夹中创建消息(此处以my_msg.msg)为例,注意的是要以msg为后缀名 内容举例如下: int32 data1 float64 d ...
- 多进程报错 EOFError: EOF when reading a line
EOF的意思为:end of file 这个错误会在多进程中出现,是因为子进程中不能出现input,只能在父进程中使用. 结果:
- niginx相关命令及代理配置
安装 in mac https://www.cnblogs.com/meng1314-shuai/p/8335140.html Nginx相关命令 mac下启动: 通过brew 安装install 后 ...