Building Block
Building Block |
| Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
| Total Submission(s): 156 Accepted Submission(s): 70 |
|
Problem Description
John are playing with blocks. There are N blocks (1 <= N <= 30000) numbered 1...N。Initially, there are N piles, and each pile contains one block. Then John do some operations P times (1 <= P <= 1000000). There are two kinds of operation:
M X Y : Put the whole pile containing block X up to the pile containing Y. If X and Y are in the same pile, just ignore this command. You are request to find out the output for each C operation. |
|
Input
The first line contains integer P. Then P lines follow, each of which contain an operation describe above.
|
|
Output
Output the count for each C operations in one line.
|
|
Sample Input
6 |
|
Sample Output
1 |
|
Source
2009 Multi-University Training Contest 1 - Host by TJU
|
|
Recommend
gaojie
|
/*
题意:两种操作:
M X Y : 将x所在的木块堆上的所有木块转移到y所在的木块堆
C X : 查询x所在的木块堆x下方的木块的数量 初步思路:很明显并查集 #错误:T了一发,没有压缩路径,wa了一发,压缩路径写惨了
*/
#include<bits/stdc++.h>
using namespace std;
int bin[];
int under[];//表示以i为根节点的数有多少木块(也就是i节点一下有多少木块)
int Rank[];//表示i节点所在的集合的木块数量
int findx(int x){
if(x!=bin[x]){
int tmp=findx(bin[x]);
under[x]+=under[bin[x]];
bin[x]=tmp;
}
return bin[x];
}
void merge(int x,int y){
int fx=findx(x);
int fy=findx(y);
if(fx!=fy){
bin[fx]=fy;//将x放到y节点上面
under[fx]+=Rank[fy];//x节点下面增加的肯定是y节点所在的全部木块
Rank[fy]+=Rank[fx];
}
}
int n;
char op[];
int x,y;
void init(){
for(int i=;i<;i++){
bin[i]=i;
Rank[i]=;
under[i]=;
}
}
int main(){
//freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF){
init();
while(n--){
scanf("%s",op);
if(op[]=='M'){
scanf("%d%d",&x,&y);
merge(x,y);
}else{//查找x这棵树有多少木块
scanf("%d",&x);
findx(x);
printf("%d\n",under[x]);
}
}
}
return ;
}
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(并查集,有点点复杂)
Building Block Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Building Block[HDU2818]
Building Block Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 2818 Building Block (带权并查集,很优美的题目)
Problem Description John are playing with blocks. There are N blocks ( <= N <= ) numbered ...N ...
- HDU——T 2818 Building Block
http://acm.hdu.edu.cn/showproblem.php?pid=2818 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- hdu2818 Building Block
Problem Description John are playing with blocks. There are N blocks (1 <= N <= 30000) numbere ...
- [HDOJ2818]Building Block(带权并查集,路径压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2818 题意:有N个块,每次有两个操作: M x y表示把x所在的那一堆全部移到y所在的那一堆的下方. ...
- hdu 2818 Building Block(加权并查集)2009 Multi-University Training Contest 1
题意: 一共有30000个箱子,刚开始时都是分开放置的.接下来会有两种操作: 1. M x y,表示把x箱子所在的一摞放到y箱子那一摞上. 2. C y,表示询问y下方有多少个箱子. 输入: 首行输入 ...
- hdu 2818 Building Block 种类并查集
在进行并的时候不能瞎jb并,比如(x, y)就必须把x并给y ,即fa[x] = y #include <iostream> #include <string> #includ ...
随机推荐
- React——高阶组件
1.在React中higher-order component (HOC)是一种重用组件逻辑的高级技术.HOC不是React API中的一部分.HOC是一个函数,该函数接收一个组件并且返回一个新组件. ...
- iOS9.3越狱
转载:http://bbs.feng.com/read-htm-tid-10680439.html 首先是Windows英文版越狱的的教程 下载 Cydia Impactor 工具(用来安装越狱A ...
- OpenJudge_1321:棋盘问题
题目描述 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆 ...
- UI自动化测试(三)对页面中定位到的元素对象做相应操作
前两天分别讲述了UI自动化测试基础以及对页面元素该如何进行定位,这一篇自然就是对定位到的页面元素对象进行相应操作啦. 阅读目录 1.常用操作元素对象的方法 2.鼠标事件操作 3.键盘事件操作 4.We ...
- Dynamic Inversions II 逆序数的性质 树状数组求逆序数
Dynamic Inversions II Time Limit: 6000/3000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Other ...
- hdu3695 ac自动机入门
Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/1280 ...
- windown快速安装xgboost
记录xgboost的快速安装方式,该方式适合pyhton3.5/3.6版本. 系统: win10 64bit python版本:3.6 1. 下载xgboost编译好的whl包 下载路径为:http: ...
- Python3常用学习网站总结(随时更新)
Python资源大全 http://python.jobbole.com/84464/ https://github.com/jobbole/awesome-python-cn scrapy: h ...
- vue2组件之select2调用
目前,项目中使用了纯前端的静态项目+RESTFul接口的模式.为了更好的对数据进行操作,前端使用了vue2的mvvm功能,但是由于不是单页面应用,所以,并没有涉及到其它的如vue-route等功能,也 ...
- 翻译连载 | 第 10 章:异步的函数式(下)-《JavaScript轻量级函数式编程》 |《你不知道的JS》姊妹篇
原文地址:Functional-Light-JS 原文作者:Kyle Simpson-<You-Dont-Know-JS>作者 关于译者:这是一个流淌着沪江血液的纯粹工程:认真,是 HTM ...