在进行并的时候不能瞎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. 【linux驱动分析】之dm9000驱动分析(三):sk_buff结构分析

    [linux驱动分析]之dm9000驱动分析(一):dm9000原理及硬件分析 [linux驱动分析]之dm9000驱动分析(二):定义在板文件里的资源和设备以及几个宏 [linux驱动分析]之dm9 ...

  2. maven插件介绍之maven-jar-plugin

    maven-jar-plugin 插件的maven依赖为: <dependency> <groupId>org.apache.maven.plugins</groupId ...

  3. 【DataStructure】Description and Introduction of Tree

    [Description] At ree is a nonlinear data structure that models a hierarchical organization. The char ...

  4. Android自己定义组件系列【11】——实现3D立体旋转效果

    今天在网上看到一篇文章写关于Android实现3D旋转(ca=drs-">http://www.ibm.com/developerworks/cn/opensource/os-cn-a ...

  5. (转)typedef用法

    Typedef 声明有助于创建平台无关类型,甚至能隐藏复杂和难以理解的语法.不管怎样,使用 typedef 能为代码带来意想不到的好处,通过本文你可以学习用 typedef 避免缺欠,从而使代码更健壮 ...

  6. 漫谈程序猿系列:无BUG不生活

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZm9ydW9r/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...

  7. LeetCode(28)题解:Implement strStr()

    https://leetcode.com/problems/implement-strstr/ 题目: Implement strStr(). Returns the index of the fir ...

  8. Python 005- 使用Pyecharts来绘制各种各样的图形

    本文转载自:https://blog.csdn.net/qq_39143076/article/details/79065448,如有侵权,请联系删除啊 如何做Python 的数据可视化? pyech ...

  9. sanic官方文档解析之静态文件和版本

    1,静态文件 就向图片文件一样,静态文件和指导性的文件,当通过Sanic服务端用app.static()方法注册的时候,这种方法采用端点url和文件名称获得.这样的文件的指定,将会通过指定的端点访问. ...

  10. 20170225-ALV tree 显示

    1.写程序, 2.话屏幕9000,CALL SCREEN 9000.(双击屏幕进去画个容器就ok,+OK_CODE,+去掉注释) 3.处理好ALV 的PBO 初始化,处理用户事件PAO,user co ...