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 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 2824 2821 2820 2822

点击打开题目链接

有N个砖头,编号为1~N。有两种操作, 第一种是M  x  y,  把 x 所在的那一堆砖头全部移动放到 y 所在的那堆上面。  第二种操作是C  x,  即查询 x 下面有多少个砖头。

带权并查集的应用, 用 Num[ x ] 来表示以 x 为根的这棵树的节点总数(x 必须是根,没有父亲节点), Under[ x ] 数组来表示 x 砖头下面有多少个砖头。

如果把a堆砖放到b堆砖上面, 那么a堆最底面那个砖头root_a的下面原本是有0个砖头的, 搬过去之后,变成了有 b 堆砖的数量个。然后root_a之上的数量便根据root_a的值进行更新。更新的步骤在查找时路径压缩的那一步进行。

#include <cstdio>
#include <iostream>
using namespace std;

const int MAXN = 30000 + 10;
int Fa[MAXN], Under[MAXN], Num[MAXN];

void Init()
{
    for (int i = 0; i < MAXN; i++)
    {
        Fa[i] = i;              //初始化并查集,自成一个连通分量
        Num[i] = 1;             //i是根节点,初始化以它为根的树共有1个节点
    }
}

int Find(int x)
{
    if (x == Fa[x]) return x;
    int fa = Fa[x];
    Fa[x] = Find(Fa[x]);        //递归查找老祖宗
    Under[x] += Under[fa];      //加上父亲的
    return Fa[x];
}

void Union(int x, int y)
{
    int tx = Find(x), ty = Find(y); //先查找两者的祖先
    if (tx == ty) return;
    Fa[tx] = ty;                //合并
    Under[tx] = Num[ty];        //tx下面多了Num[ty]个
    Num[ty] += Num[tx];         //以ty为根的树多了Num[tx]个节点
    Num[tx] = 0;                //以tx为根的树没有结点(此时tx已经不是一个树的真正根节点)
}

int main()
{
    int t, x, y;
    char cmd[10];
    Init();
    scanf("%d", &t);
    while (t--)
    {
        scanf("%s", cmd);
        if (cmd[0] == 'C')
        {
            scanf("%d", &x);
            Find(x);
            printf("%d\n", Under[x]);
        }
        else
        {
            scanf("%d%d", &x, &y);
            Union(x, y);
        }
    }
    return 0;
}
时间: 2024-12-12 11:58:40

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 3407.Zjnu Stadium 加权并查集

Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3726    Accepted Submission(s): 1415 Problem Description In 12th Zhejiang College Students Games 2007, there was a new stadium built

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

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 求大神讲解 (并查集)

#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(带权并查集)

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

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(并查集)

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

P1196 银河英雄传说(加权并查集)

P1196 银河英雄传说 题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山压 顶集团派宇宙舰队司令莱因哈特率领十万余艘战舰出征,气吞山河集团点名将杨 威利组织麾下三万艘战舰迎敌. 杨威利擅长排兵布阵,巧妙运用各种战术屡次以少胜多,难免恣生骄气.在 这次决战中,他将巴米利恩星域战场划分成30000列,每列依次编号为1, 2, …, 30000.之后