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 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

Recommend

gaojie   |   We have carefully selected several similar problems for you:  2819 2817 2821 2820 2822

多组数据、、貌似因为这个WA好多次了、、

 1 #include <algorithm>
 2 #include <cstring>
 3 #include <cstdio>
 4
 5 using namespace std;
 6
 7 const int N(30005);
 8 int fa[N],sum[N],beh[N];
 9
10 int find(int x)
11 {
12     if(fa[x]==x) return x;
13     int dad=fa[x];
14     fa[x]=find(fa[x]);
15     beh[x]+=beh[dad];
16     return fa[x];
17 }
18 void combine(int x,int y)
19 {
20     x=find(x),y=find(y);
21     if(x==y) return ;
22     beh[x]=sum[y];
23     sum[y]+=sum[x];
24     fa[x]=y;
25 }
26 void init()
27 {
28     for(int i=0;i<N;i++) fa[i]=i,sum[i]=1,beh[i]=0;
29 }
30
31 int main()
32 {
33     for(int p,u,v;~scanf("%d",&p);)
34     {
35         init();
36         for(char ch[2];p--;)
37         {
38             scanf("%s%d",ch,&u);
39             if(ch[0]==‘M‘)
40             {
41                 scanf("%d",&v);
42                 combine(u,v);
43             }
44             else find(u),printf("%d\n",beh[u]);
45         }
46     }
47     return 0;
48 }
时间: 2024-12-15 00:18:53

HDU——T 2818 Building Block的相关文章

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

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): 3986    Accepted Submission(s): 1235 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,用结构体是