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. ORA-01940: cannot drop a user that is currently connected 问题解析

    https://www.linuxidc.com/Linux/2012-12/76448.htm

  2. Python练习题–持续更新

    1.你是一个高级测试工程师,现在要做性能测试,需要你写一个函数,批量生成一些注册使用的账号. 产生的账号是以@163.com结尾,长度由用户输入,产生多少条也由用户输入,用户名不能重复,用户名必须由大 ...

  3. (1)git

    1.创建一个版本库 #创建一个文件夹 E:\>mkdir pythonGit #进入文件夹 E:\>cd pythonGit #把此目录创建成git版本库 E:\pythonGit> ...

  4. luogu P1476 休息中的小呆

    题目描述 当大家在考场中接受考验(折磨?)的时候,小呆正在悠闲(欠扁)地玩一个叫“最初梦想”的游戏.游戏描述的是一个叫pass的有志少年在不同的时空穿越对抗传说中的大魔王chinesesonic的故事 ...

  5. Kafka windows下的安装

    1. 安装JDK 1.1 安装文件:http://www.oracle.com/technetwork/java/javase/downloads/index.html 下载JDK1.2 安装完成后需 ...

  6. systemtap初体验

    https://phpor.net/blog/post/3471 写在前面: systemtap依赖的debuginfo可以从这里(http://debuginfo.centos.org/6/x86_ ...

  7. 浅析keepalived vip漂移原理与VRRP协议

    2017-01-18 Martin 开源技术社区 简介 什么是keepalived呢?keepalived是实现高可用的一种轻量级的技术手段,主要用来防止单点故障(单点故障是指一旦某一点出现故障就会导 ...

  8. android 获得电池状态

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  9. 在Ubuntu 10.10下安装JDK配置Eclipse及Tomcat

    1.安装JDK 1.1.到官网下载相关的JDK 这里下载的是 jdk-6u23-linux-i586.bin. 下载地址:http://www.oracle.com/technetwork/java/ ...

  10. 【Hibernate】(2)Hibernate配置与session、transaction

    1. Hibernate经常使用配置 使用hibernate.default_schema属性能够让全部生成的表都带一个指定的前缀. 2. session简单介绍 不建议直接使用jdbc的connec ...