MENU

POJ1988 Cube Stacking(带权并查集)

2018 年 04 月 14 日 • 阅读: 1688 • 数据结构阅读设置

Cube Stacking

  • Time Limit: 2000MS
  • Memory Limit: 30000K
  • Total Submissions: 26967
  • Accepted: 9443
  • Case Time Limit: 1000MS

Description

Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes labeled 1 through N. They start with N stacks, each containing a single cube. Farmer John asks Betsy to perform P (1<= P <= 100,000) operation. There are two types of operations:
moves and counts.
* In a move operation, Farmer John asks Bessie to move the stack containing cube X on top of the stack containing cube Y.
* In a count operation, Farmer John asks Bessie to count the number of cubes on the stack with cube X that are under the cube X and report that value.
Write a program that can verify the results of the game.

Input

* Line 1: A single integer, P
* Lines 2..P+1: Each of these lines describes a legal operation. Line 2 describes the first operation, etc. Each line begins with a 'M' for a move operation or a 'C' for a count operation. For move operations, the line also contains two integers: X and Y.For count operations, the line also contains a single integer: X.
Note that the value for N does not appear in the input file. No move operation will request a move a stack onto itself.

Output

Print the output from each of the count operations in the same order as the input file.

Sample Input

6
M 1 6
C 1
M 2 4
M 2 6
C 3
C 4

Sample Output

1
0
2

Source

USACO 2004 U S Open


链接

http://poj.org/problem?id=1988


题意

有$N$个方块从$1$标记到$N$,最初时分成$N$堆,每堆一个。现有两个操作:

  1. $M \ X \ Y$,将$X$方块所在的堆移动至$Y$方块所在的堆的顶部
  2. $C \ X$,查询$X$方块下面方块的数量

题解

使用并查集来维护,$cnt[x]$表示$x$下面方块的数量,$sum[x]$表示$x$所在堆的方块数量,$F[x]$表示方块$x$的父节点。

  • 合并两个方块$x$和$y$时,总是把下方的方块作为父节点,让$F[x]=y$,每堆的根节点为该堆最下面的一个方块
  • 合并$x$和$y$方块所在的两个堆$X$和$Y$时,首先找到$X$堆的根节点$fx(=find(x))$,然后找到$Y$堆的根节点$fy(=find(y))$,让$F[fx]=fy$,$cnt[fx] = sum[fy]$,$sum[fy] += sum[fx]$
  • 在查询根节点的路径压缩操作中,对$cnt[x]$进行更新,$cnt[x] += cnt[F[x]]$

从图中我们知道$X$堆的方块$3$已经完成了更新,但是方块$3$上面的两个方块$2$和$x$呢? 我们需要在查询它们父节点的时候(路径压缩过程中)再进行更新,所以最后的$find(x)$必不可少,类似线段树中的lazy标记

代码

  • Status:Accepted
  • Time:922ms
  • Memory:1080kB
  • Length:982
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 30010;
int F[maxn];
int sum[maxn];
int cnt[maxn];

void init()
{
    for (int i = 0; i < maxn; ++i)
    {
        F[i] = i;
        sum[i] = 1;
        cnt[i] = 0;
    }
}

int find(int root)
{
    if (root == F[root])
        return root;
    int tmp = find(F[root]); // 找到root的根节点
    cnt[root] += cnt[F[root]]; // 更新root的方块数
    F[root] = tmp; // 更新root的根节点
    return tmp;
}

void merge(int u, int v)
{
    int fx = find(u), fy = find(v);
    if (fx != fy)
    {
        F[fx] = fy; // x堆放到y堆的上方,y堆最下方元素作为根节点
        cnt[fx] = sum[fy]; // 方块fx下面的方块树即为y堆的数量
        sum[fy] += sum[fx]; // y堆的方块数更新为两个堆的方块总数
    }
}

int main()
{
    init();
    int n, x, y;
    char ch;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
    {
        cin >> ch;
        if (ch == 'M')
        {
            scanf("%d%d", &x, &y);
            merge(x, y);
        }
        else
        {
            scanf("%d", &x);
            find(x); // 注意更新!!!
            printf("%d\n", cnt[x]);
        }
    }
    return 0;
}

这道题搞了好久好久,要是能想通就非常简单,就是简单的合并和查询,要是想不通那就麻烦了。看来自己对并查集的路径压缩操作以及递归不是特别熟悉。


The end.
2018年04月15日 星期天
最后编辑于: 2018 年 04 月 26 日