并查集(POJ1988)


Language:
Cube Stacking
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 23340 Accepted: 8182
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

感觉这个纯粹就是经验了,好吧,我是属于没有经验的,,,,,,,

题意: 说是有n块砖,编号从1到n,有两种操作,第一是把含有x编号的那一堆砖放到含有编号y的那一堆砖的上面,第二是查询编号为x的砖的下面有多少块砖。用count[x]表示下面有多少块砖。

现在需要把两堆砖合并,显然要用上并查集,可是普通的合并之后如何知道x的下面有多少块砖呢,思考合并的过程,对于一堆砖,移动到另一堆砖上时,上面那一堆上每块砖的count[i]应该加上下面一堆砖的数量,这个操作对于上面一堆砖的根来说是简单的,我使用uset[i]表示连通分量,舒适化时所有的uset[i]为-1,负数代表这个节点为根,1代表这个连通分量的节点总数为1,以样例为例,首先将1放到6上面,即将6合并到1所在的连通分量中,合并的过程中我们知道两个信息,第一是当前连通分量6->1的节点数量为2,6距离1的距离为1,同理,将2放到4上面,这个连通分量节点个数为2,,4到2的距离为1,最后,我们将包含6的这个连通分量合并到包含2的这个连通分量中,此时连通分量数为4,曾经的6->1连通分量的根距离合并后的连通分量的根的距离为2,就是4->2的连通分量的节点数

说了半天有什么用处呢,经过上面这个过程,我们知道了每一个节点到它第一次被合并时的那个根节点的距离,6->1的距离为1,1到4的距离为2,2到4的距离为1,这样我们在查询6的下面有多少块砖时,直接用4(连通分量节点数)-(1+2)(6到根节点的距离)-1,


#include 
#include 

const int maxn=30005;
int uset[maxn];
int dis[maxn];

int find(int x)
{
    if(uset[x]<0) //找到根
        return x;
    int temp = uset[x]; 
    uset[x] = find(temp);//路径压缩
    dis[x] += dis[temp];//dis[temp]表示temp,即x曾经的根节点距离根的距离,x到根的距离为x到temp的距离加上temp到根的距离
    return uset[x];
}

int main()
{
    //freopen("input.txt","r",stdin);
    int T;
    scanf("%d",&T);
    memset(dis,0,sizeof(dis)); //初始时自己为根,到自己的距离为0
    memset(uset,-1,sizeof(uset)); //一个节点,自己为根
    while(T--)
    {
        char op;
        scanf("%*c%c",&op); //操作类型
        if(op=='M')
        {
            int up,down;
            scanf("%d%d",&up,&down); //把包含up的这堆砖放到包含down的一堆砖中
            up =find(up); //查询并更新up到根的距离
            down=find(down);
            if(up!=down)
            {
                dis[down] = (-uset[up]);//下面的砖的根距离如今的连通分量的根的距离
                uset[up]+=uset[down]; //节点相加
                uset[down]=up; //合并
            }
        }
        else
        {
            int x;
            scanf("%d",&x);
            printf("%d\n",uset[find(x)]*(-1)-dis[x]-1);//count
        }
    }
    return 0;
}


你可能感兴趣的:(曾经水过的题)