hdu 2818 Building Block
来源:互联网 发布:万能钥匙mac版 编辑:程序博客网 时间:2024/06/11 16:48
点击打开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;}
- 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
- HDU-2818-Building Block
- HDU 2818 Building Block(并查集)
- hdu 2818 Building Block(并查集)
- 三种平均值的比较
- 从就手机恢复应用的数据
- 装箱问题
- OpenCV下车牌定位算法实现代码
- JFreeChart教程(一)
- hdu 2818 Building Block
- Java本地工具化类的简单例子
- UVA 10564 Paths through the Hourglass
- hadoop深入研究:(十八)——Avro schema兼容
- JFreeChart教程(二)
- Knight Moves hdu BFS
- liunx上安装mysql总结
- 收款银行swift ABA码
- HDU 3491 Thieves 最小点割集+拆点==最大流(建图可贵)