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. Linux 下 GCC 编译共享库控制导出函数的方法

    通过一些实际项目的开发,发现这样一个现象,在 Windows 下可以通过指定 __declspec(dllexport) 定义来控制 DLL(动态链接库)中哪些函数可以导出,暴露给其他程序链接使用,哪 ...

  2. 记住密码后,密码框Password会自动带出数据

    一般登陆之后浏览器会询问是否记住密码,如果把密码记住在浏览器上,下次登陆的时候浏览器会把用户名和密码自动填充到登录页面.前段时间服务站平台的员工账号模块提测后,测试提出360浏览器记住密码后会自用把登 ...

  3. 程序防止SqlServer使用SqlServer Profiler跟踪

    思路: 1.使用默认函数(fn_trace_getinfo)查询跟踪列表: 2.调用系统存储过程(sp_trace_setstatus)修改跟踪状态. 相关Sql : declare @default ...

  4. Entity Farmework领域建模方式 3种编程方式

    一个业务领域由各个实体和各个相互关联且有格子的属性和行为的实体组成,每个实体都有其状态和验证规则需要维护,Entity Framework (后面简称EF)实体框架设计的出现是为了允许开发人员着重关注 ...

  5. T1553 互斥的数 codevs

    http://codevs.cn/problem/1553/ 题目描述 Description 有这样的一个集合,集合中的元素个数由给定的N决定,集合的元素为N个不同的正整数,一旦集合中的两个数x,y ...

  6. HUD 1506 Largest Rectangle in a Histogram

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  7. Linux出现cannot create temp file for here-document: No space left on device的问题解决

    在终端输入:cd /ho 按tab键时,显示错误: bash: cannot create temp file for here-document: No space left on device 这 ...

  8. uibutton去掉点击后背景有阴影的方法

    1,将normal和highlight两种方式都设置上图片即可 UIButton *goback = [[UIButton alloc]initWithFrame:CGRectMake(5.0f, 5 ...

  9. 【IntelliJ IDEA】2017.3.4版本永久破解

    [本版本软件包和破解jar在网盘上有    我的网盘--技术--idea破解所需要的] 1.idea官网下载 历史版本 选择2017.3.4版本下载 https://www.jetbrains.com ...

  10. 转:一个android开发者独立开发社交app全过程

    http://www.cnblogs.com/linguanh/p/5683069.html