hdu 2818 Building Block 种类并查集
在进行并的时候不能瞎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 种类并查集的更多相关文章
- hdu 2818 Building Block(并查集,有点点复杂)
Building Block Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 2818 Building Block (带权并查集,很优美的题目)
Problem Description John are playing with blocks. There are N blocks ( <= N <= ) numbered ...N ...
- hdu 2818 Building Block
Building Block Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 2818 Building Block(加权并查集)2009 Multi-University Training Contest 1
题意: 一共有30000个箱子,刚开始时都是分开放置的.接下来会有两种操作: 1. M x y,表示把x箱子所在的一摞放到y箱子那一摞上. 2. C y,表示询问y下方有多少个箱子. 输入: 首行输入 ...
- 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 ...
- hdu 1182 A Bug's Life(简单种类并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 题意:就是给你m条关系a与b有性关系,问这些关系中是否有同性恋 这是一道简单的种类并查集,而且也 ...
- hdu 3038 How Many Answers Are Wrong(种类并查集)2009 Multi-University Training Contest 13
了解了种类并查集,同时还知道了一个小技巧,这道题就比较容易了. 其实这是我碰到的第一道种类并查集,实在不会,只好看着别人的代码写.最后半懂不懂的写完了.然后又和别人的代码进行比较,还是不懂,但还是交了 ...
- 【进阶——种类并查集】hdu 1829 A Bug's Life (基础种类并查集)TUD Programming Contest 2005, Darmstadt, Germany
先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组 ...
- HDU - 3038 种类并查集
思路:种类并查集的每个节点应该保存它的父节点以及他和父节点之间的关系.假设root表示根结点,sum[i-1]表示i到根结点的和,那么sum[j-1] - sum[i]可以得到区间[j, i]的和.那 ...
随机推荐
- Cocoapods Undefined symbols for architecture armv7s\arm64
此类错误 "_OBJC_CLASS_$_AFURLSessionManager", referenced from: 解决的方法 在other linker flags里加入一行 ...
- HDU 1242 rescue (优先队列模板题)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- iOS 相似淘宝商品详情查看翻页效果的实现
基本思路: 1.设置一个 UIScrollView 作为视图底层,而且设置分页为两页 2.然后在第一个分页上加入一个 UITableView 而且设置表格可以上提载入(上拉操作即为让视图滚动到下一页) ...
- DRP——Dom4j使用
dom4j是一个Java的XMLAPI,类似于jdom.用来读写XML文件的.dom4j是一个很很优秀的JavaXMLAPI.具有性能优异.功能强大和极端易用使用的特点.Dom4j是一个易用的.开源的 ...
- notpad++快捷键
Notpad++快捷键 Notepad++选中行操作 快捷键 使用技巧 作者: Rememberautumn 分类: 其他 发布时间: 2014-09-04 14:18 阅读: 60,106 微信小 ...
- 【bzoj4653】[Noi2016]区间
离散化+线段树 #include<algorithm> #include<iostream> #include<cstdlib> #include<cstri ...
- spring boot 使用Ehcache
1-引入maven依赖: 2-增加ehcache.xml 3-bootstrap.yml配置ehcache.xml的路径 4-启动类加注解@EnableCaching 5-使用处加注解@Cacheab ...
- Mac OS X 开发环境搭建之利用 Parallel Desktop 安装 CentOS 7 [转载]
背景 在企业级软件系统中,数据库的地位是比较高的,而且一般都要求支持多种数据库,如 Oracle. DB2.MySQL 等,它们的最新版本大多都不再支持 Mac OS X 系统,所以装一个 Linux ...
- Vue.js 使用 Swiper.js 在 iOS 11 时出现错误
前言 在H5项目中,需要用到翻页效果,通过 Swiper 来实现,安装 Swiper npm i swiper -S 但是实际使用中,发现低版本 iOS < 11 会出现下面这个错误: Synt ...
- adb获取Android系统属性(adb shell getprop ***)数据来源
在Android系统中,它的根文件系统下有几个用于启动系统时需要的配置文件: /init.rc /default.prop /system/build.prop 通常我们可以通过命令getprop获取 ...