在进行并的时候不能瞎jb并,比如(x, y)就必须把x并给y ,即fa[x] = y

 #include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + ;
int a[maxn];
int fa[maxn];
int Rank[maxn];
int cnt[maxn]; //种类 int Find(int x){
if (x == fa[x])
return x;
int t = Find(fa[x]);
cnt[x] += cnt[fa[x]];
return fa[x] = t;
} void init(){
memset(cnt, , sizeof(cnt));
for (int i = ; i < maxn; i++){
fa[i] = i;
Rank[i] = ;
}
} void set_union(int x, int y){
int xx = Find(x);
int yy = Find(y);
if (xx == yy)
return;
fa[xx] = yy;
cnt[xx] = Rank[yy];
Rank[yy] += Rank[xx];
} int main(){
std::ios::sync_with_stdio(false);
init();
int t;
cin >> t;
while (t--){
string s;
int x, y;
cin >> s;
if (s == "M"){
cin >> x >> y;
set_union(x, y);
}
else{
cin >> x;
int t = Find(x);
cout << cnt[x] << endl;
}
}
//system("pause");
return ;
}

hdu 2818 Building Block 种类并查集的更多相关文章

  1. hdu 2818 Building Block(并查集,有点点复杂)

    Building Block Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. hdu 2818 Building Block (带权并查集,很优美的题目)

    Problem Description John are playing with blocks. There are N blocks ( <= N <= ) numbered ...N ...

  3. hdu 2818 Building Block

    Building Block Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. hdu 2818 Building Block(加权并查集)2009 Multi-University Training Contest 1

    题意: 一共有30000个箱子,刚开始时都是分开放置的.接下来会有两种操作: 1. M x y,表示把x箱子所在的一摞放到y箱子那一摞上. 2. C y,表示询问y下方有多少个箱子. 输入: 首行输入 ...

  5. HDU 1829 A Bug's Life (种类并查集)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1829 A Bug's Life Time Limit: 15000/5000 MS (Java/Oth ...

  6. hdu 1182 A Bug's Life(简单种类并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 题意:就是给你m条关系a与b有性关系,问这些关系中是否有同性恋 这是一道简单的种类并查集,而且也 ...

  7. hdu 3038 How Many Answers Are Wrong(种类并查集)2009 Multi-University Training Contest 13

    了解了种类并查集,同时还知道了一个小技巧,这道题就比较容易了. 其实这是我碰到的第一道种类并查集,实在不会,只好看着别人的代码写.最后半懂不懂的写完了.然后又和别人的代码进行比较,还是不懂,但还是交了 ...

  8. 【进阶——种类并查集】hdu 1829 A Bug's Life (基础种类并查集)TUD Programming Contest 2005, Darmstadt, Germany

    先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组 ...

  9. HDU - 3038 种类并查集

    思路:种类并查集的每个节点应该保存它的父节点以及他和父节点之间的关系.假设root表示根结点,sum[i-1]表示i到根结点的和,那么sum[j-1] - sum[i]可以得到区间[j, i]的和.那 ...

随机推荐

  1. POJ--2284--That Nice Euler Circuit【平面图欧拉公式】

    链接:id=2284">http://poj.org/problem?id=2284 题意:一个自己主动绘图的机器在纸上(无限大)绘图,笔尖从不离开纸,有n个指令,每一个指令是一个坐标 ...

  2. 简单的glib测试(三)

    #include <stdlib.h> #include <stdio.h> #include <string.h> #include <locale.h&g ...

  3. Memory cycles about Block

    Block keeps a strong point to all object referenced in side of them, so all object will stay in heap ...

  4. delphi三层架构(使用SATRDA改造,客户端代码不变)

    我们的delphi程序很多是以前开发的,采用典型的CS架构,由程序直接连接数据库.现在需要改成在外网可以直接操作软件.先把数据库搬到了阿里云上,测试发现直接连数据库和VPN连接测试速度很慢,直连还容易 ...

  5. CSS中的那点事儿(一)--- CSS中的单位1

    单位主要用在规定元素的数值性css属性的,这里主要讨论应用的比较多的是width height  padding margin font-size,而单位中应用最广泛就是%.px.em 百分比 百分比 ...

  6. 关于animate的一些属性

    animate() 方法执行 CSS 属性集的自定义动画.该方法通过CSS样式将元素从一个状态改变为另一个状态.CSS属性值是逐渐改变的,这样就可以创建动画效果.只有数字值可创建动画(比如 " ...

  7. i2c_set_clientdata函数【转】

    本文转载自‘:http://blog.csdn.net/jk198310/article/details/43738367 在i2c驱动中有很多函数和数据结构,很多一时难以理解,所以写下本文共同学习. ...

  8. Swift(二)控制流

    要处理条件逻辑,使用 if 和 switch ,要处理循环逻辑,使用 for-in, for, while, 和 do-while .包着条件或者循环的括号可加可不加.处理逻辑体的花括弧是必须加的. ...

  9. Lucene 的四大索引查询 ——bool 域搜索 通配符 范围搜索

    Lucene 的四大索引查询  清单1:使用布尔操作符 Java代码      //Test boolean operator blic void testOperator(String indexD ...

  10. 编程方式取得Spring上下文的Properties

    在spring初始化时,可以使用Properties配置器把properties文件装载到Spring的上下文中. ... xmlns:context="http://www.springf ...