Building Block
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3250 Accepted Submission(s): 973
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
M 1 6
C 1
M 2 4
M 2 6
C 3
C 4
Sample Output
1
0
2
0
2
Source
题意:
/*有N个砖头, 编号1—N(实际上是0-N),然后有两种操作,第一种是M x y 把x所在的那一堆砖头全部移动放到y所在的那堆上面。 第二种操作是 C x ,即查询x下面有多少个砖头 ,并且输出。 */
--->带权值的并查集
代码:
#include<cstring>
#include<cstdio>
#include<cstdlib>
#define maxn 30030
using namespace std;
int father[maxn];
__int64 rank[maxn],under[maxn];
int p; void init(){ for(int i=;i<maxn ;i++) {
father[i]=i;
rank[i]=;
under[i]=;
} } int fin(int x){ if(x == father[x])
return father[x];
int tem = father[x] ;
father[x] = fin(father[x]);
under[x]+=under[tem]; return father[x]; } void Union(int a,int b){ int x = fin(a);
int y = fin(b);
if( x==y ) return ;
father[x] = y ; //将a所在的堆放在b的堆上
under[x] = rank[y];
rank[y] += rank[x];
rank[x] = ; } int main()
{
char str[];
int a,b; while(scanf("%d",&p)!=EOF){
init();
while(p--){
scanf("%s",str);
if(str[]=='M'){
scanf("%d%d",&a,&b);
Union(a,b);
}
else{
scanf("%d",&a);
fin(a);
printf("%I64d\n",under[a]);
} } }
return ;
}