Building Block HDU - 2818 (并查集)

Building Block

HDU - 2818

题意:搬砖。。。每一次可以把a所在的那一堆放到b所在的那一堆上面,问第x号砖下面有几块砖。

记录一下到根节点的距离(dw),以及根节点上方有几块砖(up)。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3
 4 const int maxn=30010;
 5 int f[maxn],up[maxn],dw[maxn];
 6
 7 int gf(int x){
 8     if(x==f[x]){
 9         return x;
10     }
11     int r=f[x];
12     f[x]=gf(r);
13     dw[x]+=dw[r];
14     return f[x];
15 }
16
17 int n;
18 int main(){
19    // freopen("in.txt","r",stdin);
20     while(scanf("%d",&n)!=EOF){
21         int ans=0;
22         for(int i=0;i<maxn;i++) {f[i]=i;up[i]=1;dw[i]=0;}
23         for(int i=0;i<n;i++){
24             int a,b;
25             char c;
26             getchar();
27             scanf("%c",&c);
28             if(c==‘M‘){
29                 scanf("%d%d",&a,&b);
30                 int pa=gf(a),pb=gf(b);
31                 if(pa!=pb){
32                     f[pa]=pb;
33                     dw[pa]+=up[pb];
34                     up[pb]+=up[pa];
35                 }
36             }else{
37                 scanf("%d",&a);
38                 gf(a);
39                 printf("%d\n",dw[a]);
40             }
41         }
42     }
43     return 0;
44 }

时间: 2024-12-15 22:07:28

Building Block HDU - 2818 (并查集)的相关文章

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 (带权并查集,很优美的题目)

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 1051 并查集+贪心

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11694    Accepted Submission(s): 4837 Problem Description There is a pile of n wooden sticks. The length and weight of each stick ar

HDU 1512 并查集+左偏树

Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3105    Accepted Submission(s): 1330 Problem Description Once in a forest, there lived N aggressive monkeys. At the beginning, they e

hdu 1829 并查集(食物链的弱化版)

http://acm.hdu.edu.cn/showproblem.php?pid=1829 Problem Description Background  Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of

hdu 4514 并查集+树形dp

湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 4539    Accepted Submission(s): 816 Problem Description 随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,

hdu 1856 并查集

http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)Total Submission(s): 13672    Accepted Submission(s): 5008 Problem Description Mr Wang wants some boys

HDU 1181 并查集 or SPFA

变形课 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 12773    Accepted Submission(s): 4733 Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规

hdu 1213 并查集入门

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12538    Accepted Submission(s): 6145 Problem Description Today is Ignatius' b