SPOJ:Lost and survived(multiset+并查集)
On September 22, 2004, Oceanic Flight 815 crashed on a mysterious island somewhere in the pacific.
There actually were survivors in the crash , N survivors . The mysterious island kept on moving in space - time, so no rescue reached them.
Initially every survivor was on his own .But soon they realized there are these “The Others” (Scientists of damn Dharma Initiative) on this Island too.
So to protect themselves from mad Scientists they started uniting into groups after Dr. Shephard said “ Live together Die alone ”.
You have to handle Q queries; which consist of two survivors becoming friends and thereby uniting there respective groups into a larger group.
After each query, output the difference between the group of largest size and group of smallest size at that time.
If there is only one group, output 0. At first, everyone is in their own group.
Also note, if the two survivors in the query are already in the same group, print the current answer, and skip merging groups.
Also do comment if you have watched Lost :-p
Input
The first line consists of two space separated integers, N and Q
The next Q line consists of two integers, A and B, meaning that
survivor A and survivor B became friends uniting there groups.
Output
Output Q lines, the answer after each query.
1<=N<=100000
1<=Q<=100000
Example
Input:
5 3
1 2
2 3
5 4
Output:
1
2
1
题意:有N个人,一开始都自己一个集合。有Q次操作,每次给定u,v,要求合并u和v到一个集合(已经在就忽略),然后输出目前的最大集合元素个数减去最小集合元素个数。
思路:集合用并查集表示,元素个数用multiset保存。
注意multiset的删除:s.erase(find(x)),只删除一个;而 s.erase(x), 会删除所有值为x的元素
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
multiset<int>s;
int fa[maxn],num[maxn],Max=,Min=;
int find(int u){
if(u==fa[u]) return u;
fa[u]=find(fa[u]); return fa[u];
}
void merge(int u,int v){
int fau=find(u),fav=find(v);
if(num[fau]>num[fav]) swap(fau,fav);
fa[fau]=fav;
s.erase(s.find(num[fau])); s.erase(s.find(num[fav]));
num[fav]+=num[fau]; s.insert(num[fav]);
if(num[fav]>Max) Max=num[fav];
Min=*s.begin();
}
int main()
{
int N,Q,u,v,i,j,ans;
scanf("%d%d",&N,&Q);
for(i=;i<=N;i++){
fa[i]=i; num[i]=;
s.insert();
}
while(Q--){
scanf("%d%d",&u,&v);
if(find(u)!=find(v)) merge(u,v);
printf("%d\n",Max-Min);
}
return ;
}
SPOJ:Lost and survived(multiset+并查集)的更多相关文章
- BZOJ 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居:队列 + multiset + 并查集【曼哈顿距离变形】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1604 题意: 平面直角坐标系中,有n个点(n <= 100000,坐标范围10^9) ...
- SPOJ:Lexicographically Smallest(并查集&排序)
Taplu and Abhishar loved playing scrabble. One day they thought of inventing a new game using alphab ...
- SPOJ:Stack Overflow(并查集)
Stack is a basic data structure. Where 3 operation can be done- Push: You can push object to the sta ...
- bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居【切比雪夫距离+并查集+multiset】
参考:http://hzwer.com/4361.html 坐标开long long,inf开大点 先曼哈顿转切比雪夫(x+y,x-y),距离就变成了max(x',y'): 先按x排序,维护两个指针, ...
- CF # 296 C Glass Carving (并查集 或者 multiset)
C. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- 并查集+multiset+双指针——cf982D
感觉自己的解法很复杂,写了一大堆代码 但核心是从小到大枚举每个元素的值,然后把<=当前元素的值进行合并,由于这个过程是单调的,所以可以直接将新的元素合并到旧的并查集里去 维护并查集的同时维护每 ...
- SPOJ IAPCR2F 【并查集】
思路: 利用并查集/DFS都可以处理连通问题. PS:注意Find()查找值和pre[]值的区别. #include<bits/stdc++.h> using namespace std; ...
- SPOJ GSS4 Can you answer these queries IV ——树状数组 并查集
[题目分析] 区间开方+区间求和. 由于区间开方次数较少,直接并查集维护下一个不是1的数的位置,然后暴力修改,树状数组求和即可. 这不是BZOJ上上帝造题7分钟嘛 [代码] #include < ...
- SPOJ LEXSTR 并查集
题目描述: Taplu and Abhishar loved playing scrabble. One day they thought of inventing a new game using ...
随机推荐
- plsql + 客户端 连接oracle数据库
一. 目录结构D:\oracle\instantclient_11_2D:\oracle\instantclient_11_2\tnsnames.ora 二. 环境变量 NLS_LANG = SIMP ...
- LINUX下面NetworkManager和network冲突的问题
https://blog.csdn.net/ID_EAGLE/article/details/74085409
- ZOJ - 4020 Traffic Light (BFS)
[传送门]http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4020 [题目大意]从起点(sx, sy)出发,要到达(ex , ...
- avi视频文件提取与合并
最近在做一个avi视频文件的提取与合并,花了几天熟悉avi文件格式.制作了一个提取与合并的动态库,不过仅限于提取视频,视频的合并还没添加一些额外判断,可能导致不同分辨率的视频文件合成后不能播放.欢迎大 ...
- 设计模式之装饰(Decorator)模式
设计模式之装饰(Decorator)模式 (一)什么是装饰(Decorator)模式 装饰模式,又称为包装模式,它以对客户端透明的方式扩张对象的功能,是继承关系的替代方案之一. 装饰模式可以在不使用创 ...
- Spring的Bean定义
以下内容引用自http://wiki.jikexueyuan.com/project/spring/bean-definition.html: Bean定义 被称作bean的对象是构成应用程序的支柱也 ...
- MySQL---笔记之视图的使用详解
什么是视图 视图是从一个或多个表中导出来的表,是一种虚拟存在的表. 视图就像一个窗口,通过这个窗口可以看到系统专门提供的数据. 这样,用户可以不用看到整个数据库中的数据,而之关心对自己有用的数据. ...
- BUPT复试专题—寻找i*j=m的个数(2016)
题目描述 3*3的矩阵内容. 1 2 3 2 4 6 3 6 9 即a[i][j](1<=i<=n,1<=j<=n)=i*j. 问一个这样n*n的矩阵里面,里面m出现的次数. ...
- 用df命令显示磁盘使用量和占用率。
使用“df -k”命令,以k为单位显示磁盘使用量和占用率. root@gsg43:/tmp# df -kFilesystem 1K-blocks Used Available Use% ...
- mock.js 的用法 -- 脱离后端独立开发,实现增删改查功能
在我们的生产实际中,后端的接口往往是较晚才会出来,并且还要写接口文档,于是我们的前端的许多开发都要等到接口给我们才能进行,这样对于我们前端来说显得十分的被动,于是有没有可以制造假数据来模拟后端接口呢, ...