并查集 (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 ...
随机推荐
- Java面试(2)
包含的模块 本文分为十九个模块,分别是: Java 基础.容器.多线程.反射.对象拷贝.Java Web .异常.网络.设计模式.Spring/Spring MVC.Spring Boot/Sprin ...
- Hadoop Tez框架
- JS对象 向上取整ceil() ceil() 方法可对一个数进行向上取整。 语法: Math.ceil(x) 注意:它返回的是大于或等于x,并且与x最接近的整数。
向上取整ceil() ceil() 方法可对一个数进行向上取整. 语法: Math.ceil(x) 参数说明: 注意:它返回的是大于或等于x,并且与x最接近的整数. 我们将把 ceil() 方法运用到 ...
- 【笔记篇】斜率优化dp(五) USACO08MAR土地购(征)买(用)Land Acquisition
好好的题目连个名字都不统一.. 看到这种最大最小的就先排个序嘛= =以x为第一关键字, y为第二关键字排序. 然后有一些\(x_i<=x_{i+1},且y_i<=y_{i+1}\)的土地就 ...
- Go, JS和Websocket
JS中建立Websocket连接 var ws = new WebSocket("ws://hostname/path", ["protocol1", &quo ...
- MySQL - 锁等待超时与information_schema的三个表
引用地址:https://blog.csdn.net/J080624/article/details/80596958 回顾一下生产中的一次MySQL异常,Cause: java.sql.SQLExc ...
- js代码触发事件
/*** * 需要触发谁的点击事件 * @param how_id 节点的id 如:<input id='test'/> 则how_id=test * @param how_this 这个 ...
- csp-s模拟测试83(集训过半)
csp-s模拟测试83(集训过半) 一场信心赛.起初$OJ$爆了我就看见全场$A$了$T1$并且夹杂着$A$掉$T2$我就很难受. 这场比赛没有深入思考,很失败,一个多小时后就结束了我罪恶的一生. 0 ...
- 使用JDK自带功能,实现一个简单的Web Service接口发布
万事开头难,本篇文章的目的就是使用JDK自带的功能,实现一个最简单的Web Service接口的发布. 下图是项目的组成,主要有三个部分,一个接口(WS),一个接口的实现类(WSImp),还有一个接口 ...
- 《DSP using MATLAB》Problem 8.39
代码: %% ------------------------------------------------------------------------ %% Output Info about ...