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 ...
随机推荐
- gulp 环境搭建
wind+r弹出cmd命令窗口 要先安装node及npm,检查是否安装成功,node -v,npm -v可查看nod及npm的版本号. 安装gulp之前我们需要安装nodejs的环境,检测能够正常使用 ...
- for 循环进化史
ECMAScript 6已经逐渐普及,经过二十多年的改进,很多功能也有了更成熟的语句,比如 for 循环 这篇博客将介绍一下从最初的 for 循环,到 ES6 的 for-of 等四种遍历方法 先定义 ...
- android开发教程之使用线程实现视图平滑滚动示例 改
package com.melonsapp.messenger.ui.popupuser; import android.os.Handler; import android.view.View; i ...
- python的websocket实现Tornado
1.使用flask的扩展: pip install flask-socketio 2.Tornado提供较好的ws(websocket)支持 参考:1.http://www.jianshu.com/p ...
- CocoaPods为project的全部target添加依赖支持
在使用CocoaPods时.pod install默认仅仅能为xcodeproject的第一个target加入依赖库支持.假设要为全部的target添加可依照例如以下步骤进行 两种情 1. 编辑Pod ...
- jquery提示消息,简单通用
jquery提示消息.简单通用 function showTips(txt,time,status) { var htmlCon = ''; if(txt != ''){ if(status != 0 ...
- Linux之时钟中断
from:深入分析Linux内核源码(http://oss.org.cn/kernel-book/) 时钟中断的产生 Linux的OS时钟的物理产生原因是可编程定时/计数器产生的输出脉冲,这个脉冲送入 ...
- centos7 网络不能重启问题 解决办法
cnetos7 网络不可重启 突然解决办法 参考他人处理 之前部署hadoop环境,在自己机器上安装了一台centos虚拟机,然后图省事,就克隆出三台,一台为master,另两台来作为 slave. ...
- sqlite学习笔记10:C语言中使用sqlite之查询和更新数据
前面说到的 sqlite_exec() 中的第三个參数, SQLite 将为 sql 參数内运行的每一个 SELECT 语句中处理的每一个记录调用这个回调函数. 本节加入了两个函数.selectFro ...
- 《Java虚拟机原理图解》4.JVM机器指令集
0. 前言 Java虚拟机和真实的计算机一样,执行的都是二进制的机器码:而我们将.java 源码编译成.class 文件,class文件便是Java虚拟机可以认识的二进制机器码,Java可以识别cla ...