hdu 2818 Building Block

Building Block

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3986    Accepted Submission(s): 1235

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.

C X : Count the number of blocks under block X

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
M 1 6
C 1
M 2 4
M 2 6
C 3
C 4

Sample Output

1
0
2

Source

2009 Multi-University Training Contest 1 - Host by TJU

带权并查集

#include<stdio.h>
#include<string.h>
#define M 30000+10
int x[M],rank[M],up[M];//rank记录该点下边有几个点,up记录该点上边有几个
void init(){
	for(int i=0;i<M;i++){
		x[i]=i; rank[i]=0; up[i]=1; //up初始化为1,以便后边操作
	}
}
int find(int k){
	int temp=x[k];
	if(x[k]==k) return k;
	x[k]=find(x[k]);
	rank[k]+=rank[temp];//子节点下边的点的个数等于本身的个数加上父节点下边的个数
	return x[k];
}
void merge(int a,int b)
{
	int fa=find(a);
	int fb=find(b);
	if(fa==fb) return;
	x[fa]=fb;
	rank[fa]=up[fb];//合并时,fa下边几个就等于fb上边有几个因为刚开始初始化为1所以不用再加1
	up[fb]+=up[fa];//更新
}
int main()
{
	int p,a,b,c,i;
	char ch[5];
	while(scanf("%d",&p)!=EOF){
		init();
		for(i=0;i<p;i++){
			scanf("%s",ch);
			if(ch[0]=='M'){
				scanf("%d%d",&a,&b);
				merge(a,b);
			}else{
				scanf("%d",&c);
				int k=find(c);
				printf("%d\n",rank[c]);
			}
		}
	}
	return 0;
} 
时间: 2024-10-11 22:59:46

hdu 2818 Building Block的相关文章

hdu 2818 Building Block(带权并查集)

题目: 链接:点击打开链接 题意: 有N个积木,1到N编号.进行一些操作P次,M:X Y把X积木放到Y的上面,如果X和Y相等请忽略.C:X 计算X积木下的积木数目. 思路: 带权并查集的题目,定义数组sum[i]表示i积木下面积木的数目.遇到操作M,就把X和Y合并到同一个集合中.我们视每个结点为1个 Pile,其中rank[i]就表示每个Pile处的积木的个数,Initially, there are N piles, and each pile contains one block.所以,ra

hdu 2818 Building Block【基础带权并查集】

Building Block Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3365    Accepted Submission(s): 1009 Problem Description John are playing with blocks. There are N blocks (1 <= N <= 30000) numbe

HDU 2818 Building Block 加权并查集

Building Block                                                                      Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4548    Accepted Submission(s): 1408 Problem Description John

?HDU 2818 Building Block 求大神讲解 (并查集)

#include <stdio.h> #include <iostream> using namespace std; int ff[30005];//ff[x]表示x的父节点 int high[30005]; int low[30005]; void ii(int n) //初始化 { int i; for(i=0;i<=n;i++) //从1开始也是错误的 { ff[i]=i; low[i] = 0; high[i] = 1; } } int dd(int x) //带路

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

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

hdu 2818 Building Block(并查集)

每次总有新领悟.. 认为没堆最底层为祖先. 关于findset是怎么维护的,这次又好好理解了一下,这道题是每次更新x和x的上一层的结点,自底层向上(从祖先一层一层到本身). 1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<string> 6 #include<queue> 7 #include<a

HDU 2818 Building Block(带权并查集)

[题目链接]:Click here~~ [题意]: 给 n 块砖头,开始各为一堆,两种操作: 1.把 X 所在的那一堆箱子里的砖头放到 Y 所在的那一堆上面. 2.询问 X 下面有多少块砖. [解题思路]:好像大家都叫它带权并查集,那为了方便,这里也这样叫吧,因为涉及前面的和后面的箱子个数,对应的查找操作,一开始想用结构体来写,在结构体里定义每个箱子的前驱和后继,每次输入的时候统计一下相应的前驱和后继的个数,后来发现不行,因为涉及到合并操作,比如说M 2 4 M 2 6连续出现两个2,用结构体是

HDU——T 2818 Building Block

http://acm.hdu.edu.cn/showproblem.php?pid=2818 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5935    Accepted Submission(s): 1838 Problem Description John are playing with blocks. There are N

POJ——T 1988 Cube Stacking || HDU ——T 2818 Building Block

http://poj.org/problem?id=1988 Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 25865   Accepted: 9044 Case Time Limit: 1000MS Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes labeled 1 throug