uva 11987 并查集
2013-07-27 16:29
204 查看
Problem A
Almost Union-Find
I hope you know the beautiful Union-Find structure. In this problem, you're to implement something similar, but not identical.The data structure you need to write is also a collection of disjoint sets, supporting 3 operations:
1 p q
Union the sets containing p and q. If p and q are already in the same set, ignore this command.
2 p q
Move p to the set containing q. If p and q are already in the same set, ignore this command
3 p
Return the number of elements and the sum of elements in the set containing p.
Initially, the collection contains n sets: {1}, {2}, {3}, ..., {n}.
Input
There are several test cases. Each test case begins with a line containing two integers n and m (1<=n,m<=100,000), the number of integers, and the number of commands. Each of the next m lines contains a command.For every operation, 1<=p,q<=n. The input is terminated by end-of-file (EOF). The size of input file does not exceed 5MB.
Output
For each type-3 command, output 2 integers: the number of elements and the sum of elements.Sample Input
5 7 1 1 2 2 3 4 1 3 5 3 4 2 4 1 3 4 3 3
Output for the Sample Input
3 12 3 7 2 8
这是一道很有的意思的并查集的变式题,其实题目并没有什么特别大的难度,关键就是并查集并没有删除这种操作,而该题正是要将并查集加上删除的操作,菜鸟今天从大牛那里学到了一招就是不需要删除 点,只需要在新建一个结点,。代码被删除的结点现在的位置,保证了删除的结点对原集合的影响降到了0.
题意:给定一些操作:
1 a b是合并两个数a b(直接用merge(a,b))2 a b是将a从原来所在集合中删除,将a移动到b所在的集合中(delete(a),merge(a,b))3 a 是查询a所在的集合有 多少个数,以及这些数的和,需要打印下面是代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=200050;
int id[maxn],num[maxn];//id是关键记录下现在的结点的位置
int pre[maxn];
char cmd;
long long sum[maxn];
int cnt;
int n,m;
void init(){
for(int i=1;i<=n;i++){
id[i]=pre[i]=sum[i]=i;
num[i]=1;
cnt=n;
}
}
int find(int v){
return pre[v]==v?v:find(pre[v]);
}
void join(int a,int b){
int fx=find(id[a]),fy=find(id[b]);
if(fx!=fy){
pre[fx]=fy;
num[fy]+=num[fx];
sum[fy]+=sum[fx];
}
}
void Delete(int a){
int root=find(id[a]);
num[root]=num[root]-1;
sum[root]-=a;
id[a]=++cnt;
pre[id[a]]=id[a];//最开始脑残忘了写这一步了
num[id[a]]=1;
sum[id[a]]=a;
}
int main(){
while(scanf("%d%d",&n,&m)==2){
int p,q;
init();
for(int i=0;i<m;i++){//分情况讨论
cin>>cmd;
if(cmd=='1')
{
cin>>p>>q;
if(find(id[p])!=find(id[q]))
join(p,q);
}
if(cmd=='2'){
cin>>p>>q;
if(find(id[p])!=find(id[q])){
Delete(p);
join(p,q);
}
}
if(cmd=='3'){
cin>>p;
printf("%d %lld\n", num[find(id[p])], sum[find(id[p])]);
}
}
}
return 0;
}
相关文章推荐
- UVa:11987 Almost Union-Find(并查集)
- uva 11987 Almost Union-Find (并查集)
- UVA 11987 - Almost Union-Find(并查集)
- uva 11987 Almost Union-Find(带删除操作的并查集)
- uva 11987 Almost Union-Find (并查集删点,转移)
- UVA 11987 Almost Union-Find(并查集)
- uva 11987 删点并查集
- UVA11987 并查集
- UVa 11987 - Almost Union-Find (并查集 需要一点技巧)
- UVa-11987 Almost union-find(带删除操作的并查集)
- UVA 11987 Almost Union-Find 并查集单点修改
- UVA 11987 - Almost Union-Find(并查集)
- UVA - 11987 Almost Union-Find (并查集)
- UVA - 11987 Almost Union-Find[并查集 删除]
- UVA 11987 Almost Union-Find 并查集的删除操作 懒惰标记
- UVa 11987 并查集 Almost Union-Find
- 【UVA 11987】并查集
- 并查集(删除) UVA 11987 Almost Union-Find
- UVA11987 - Almost Union-Find (并查集带删除)
- uva 11987 带删除操作的并查集与并查集的可持久化