hdu 2818 Building Block
2013-08-11 14:37
225 查看
点击打开hdu 2818
思路: 带权并查集
分析:
1 题目给定2种指令 M x y把x的集合放在y集合的上面,C x求x的下面有多少个元素
2 我们用rank[x]表示x以下有多少个元素,那么对于指令M x y我们始终把左边的合并到右边,那么这样rank就满足压缩的性质
3 但是因为这边的合并和普通不一样,它是把x所在的集合放在y所在集合上面,实际上是x的跟节点合并到y集合的最远点,所以我们应该开个数组记录当前集合最远的点
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 30010;
int n;
int father[MAXN];
int rank[MAXN];
int maxNum[MAXN];
void init(){
memset(rank , 0 , sizeof(rank));
memset(maxNum , 0 , sizeof(maxNum));
for(int i = 0 ; i < MAXN ; i++)
father[i] = i;
}
int find(int x){
if(father[x] != x){
int fa = father[x];
father[x] = find(fa);
rank[x] += rank[fa];
}
return father[x];
}
void Union(int x , int y){
int fx = find(x);
int fy = find(y);
if(fx != fy){
father[fx] = fy;
rank[fx] += maxNum[fy]+1;
maxNum[fy] += maxNum[fx]+1;
}
}
int main(){
char c;
int x , y;
while(scanf("%d%*c" , &n) != EOF){
init();
while(n--){
c = getchar();
if(c == 'M'){
scanf("%d%d%*C" , &x , &y);
Union(x , y);
}
else{
scanf("%d%*C" , &x);
find(x);
printf("%d\n" , rank[x]);
}
}
}
return 0;
}
思路: 带权并查集
分析:
1 题目给定2种指令 M x y把x的集合放在y集合的上面,C x求x的下面有多少个元素
2 我们用rank[x]表示x以下有多少个元素,那么对于指令M x y我们始终把左边的合并到右边,那么这样rank就满足压缩的性质
3 但是因为这边的合并和普通不一样,它是把x所在的集合放在y所在集合上面,实际上是x的跟节点合并到y集合的最远点,所以我们应该开个数组记录当前集合最远的点
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 30010;
int n;
int father[MAXN];
int rank[MAXN];
int maxNum[MAXN];
void init(){
memset(rank , 0 , sizeof(rank));
memset(maxNum , 0 , sizeof(maxNum));
for(int i = 0 ; i < MAXN ; i++)
father[i] = i;
}
int find(int x){
if(father[x] != x){
int fa = father[x];
father[x] = find(fa);
rank[x] += rank[fa];
}
return father[x];
}
void Union(int x , int y){
int fx = find(x);
int fy = find(y);
if(fx != fy){
father[fx] = fy;
rank[fx] += maxNum[fy]+1;
maxNum[fy] += maxNum[fx]+1;
}
}
int main(){
char c;
int x , y;
while(scanf("%d%*c" , &n) != EOF){
init();
while(n--){
c = getchar();
if(c == 'M'){
scanf("%d%d%*C" , &x , &y);
Union(x , y);
}
else{
scanf("%d%*C" , &x);
find(x);
printf("%d\n" , rank[x]);
}
}
}
return 0;
}
相关文章推荐
- hdu 2818--Building Block
- hdu 2818 Building Block(带权并查集)
- HDU 2818 Building Block
- HDU 2818 Building Block 带权并查集
- HDU 2818 Building Block, poj 1988 Cube Stacking(带权并查集)
- hdu 2818 Building Block【基础带权并查集】
- 【HDU 2818】 Building Block 【带权并查集】
- HDU-2818-Building Block
- hdu 2818 Building Block
- hdu 2818 Building Block
- hdu 2818 Building Block
- 【HDU】2818-Building Block(并查集)
- hdu 2818 Building Block
- 【HDU】 2818 Building Block
- HDU 2818 Building Block(并查集)
- HDU 2818 Building Block
- ?HDU 2818 Building Block 求大神讲解 (并查集)
- HDU 2818 Building Block (带权并查集)
- hdu 2818 Building Block
- HDU 2818 Building Block(带权并查集)