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 
the groups involving survivor A and survivor B unite into a larger group.

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+并查集)的更多相关文章

  1. BZOJ 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居:队列 + multiset + 并查集【曼哈顿距离变形】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1604 题意: 平面直角坐标系中,有n个点(n <= 100000,坐标范围10^9) ...

  2. SPOJ:Lexicographically Smallest(并查集&排序)

    Taplu and Abhishar loved playing scrabble. One day they thought of inventing a new game using alphab ...

  3. SPOJ:Stack Overflow(并查集)

    Stack is a basic data structure. Where 3 operation can be done- Push: You can push object to the sta ...

  4. bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居【切比雪夫距离+并查集+multiset】

    参考:http://hzwer.com/4361.html 坐标开long long,inf开大点 先曼哈顿转切比雪夫(x+y,x-y),距离就变成了max(x',y'): 先按x排序,维护两个指针, ...

  5. CF # 296 C Glass Carving (并查集 或者 multiset)

    C. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  6. 并查集+multiset+双指针——cf982D

     感觉自己的解法很复杂,写了一大堆代码 但核心是从小到大枚举每个元素的值,然后把<=当前元素的值进行合并,由于这个过程是单调的,所以可以直接将新的元素合并到旧的并查集里去 维护并查集的同时维护每 ...

  7. SPOJ IAPCR2F 【并查集】

    思路: 利用并查集/DFS都可以处理连通问题. PS:注意Find()查找值和pre[]值的区别. #include<bits/stdc++.h> using namespace std; ...

  8. SPOJ GSS4 Can you answer these queries IV ——树状数组 并查集

    [题目分析] 区间开方+区间求和. 由于区间开方次数较少,直接并查集维护下一个不是1的数的位置,然后暴力修改,树状数组求和即可. 这不是BZOJ上上帝造题7分钟嘛 [代码] #include < ...

  9. SPOJ LEXSTR 并查集

    题目描述: Taplu and Abhishar loved playing scrabble. One day they thought of inventing a new game using ...

随机推荐

  1. GitHub中watch、star、fork的作用

    star 的作用是收藏,目的是方便以后查找. watch 的作用是关注,目的是等作者更新的时候,你可以收到通知. fork 的作用是参与,目的是你增加新的内容,然后 Pull Request,把你的修 ...

  2. P1536 村村通 洛谷

    https://www.luogu.org/problem/show?pid=1536 题目描述 某市调查城镇交通状况,得到现有城镇道路统计表.表中列出了每条道路直接连通的城镇.市政府“村村通工程”的 ...

  3. 利用javascript实现在圆周上匀速划动的动画效果

    先看下效果:          

  4. 操作系统学习(三)-- CPU调度

    操作系统之进程与线程 L14 CPU调度策略 如何设计调度算法? 调度关键在:折中和综合 IO约束型的任务一般是前台任务,和用户交互:CPU约束型关注周转时间 进程切换过程需要系统内耗,切换时间长则系 ...

  5. 【转】构造HTTP请求Header实现“伪造来源IP”

    构造 HTTP请求 Header 实现“伪造来源 IP ” 在阅读本文前,大家要有一个概念,在实现正常的TCP/IP 双方通信情况下,是无法伪造来源 IP 的,也就是说,在 TCP/IP 协议中,可以 ...

  6. VS2013 update4+Cocos2d-x 3.7 Win8下安装方法及配置

    1.安装VS 2013 update4 7个G.自己就去网上找吧,一大堆,密钥问度娘. 2.安装及配置python 2.x 这里注意,一定要下载python 3.0下面的版本号. 配置:进行环境变量配 ...

  7. monggodb 复制集 集群 搭建

    https://docs.mongodb.com/manual/tutorial/enable-authentication/ Overview Enabling access control on ...

  8. 编译spark源码 Maven 、SBT 2种方式编译

    由于实际环境较为复杂,从Spark官方下载二进制安装包可能不具有相关功能或不支持指定的软件版本,这就需要我们根据实际情况编译Spark源代码,生成所需要的部署包. Spark可以通过Maven和SBT ...

  9. SLG, 菱形格子的算法.(递归版

    class GeoPoint{ public: int x; int y; public: bool operator == (const GeoPoint& p){ return p.x = ...

  10. C++设计模式之State模式

    这里有两个例子: 1.https://www.cnblogs.com/wanggary/archive/2011/04/21/2024117.html 2.https://www.cnblogs.co ...