首页 > 代码库 > 加权并查集——(POJ1988)Cube Stacking

加权并查集——(POJ1988)Cube Stacking

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

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

Sample Output

102

Source

USACO 2004 U S Open
思路:
  加权并查集

  题意: 说是有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<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int maxn = 30000+10;int fa[maxn];int rank[maxn];int dist[maxn];void init() {    for(int i=0; i<maxn; i++) {        fa[i]=i;        rank[i] = 1;        dist[i] = 0;    }}int find(int x) {    if(x != fa[x]) {        int t = fa[x];        fa[x] = find(fa[x]);        dist[x] += dist[t];//dis[t]表示t,即x曾经的根节点距离根的距离,x到根的距离为x到t的距离加上t到根的距离    }    return fa[x];}int main() {    int n;    while(scanf("%d",&n)==1) {        init();        char op;        while(n--) {            cin>>op;            int a,b;            if(op==M) {                scanf("%d%d",&a,&b);                int faA = find(a);                int faB = find(b);                if(faA != faB) {                    fa[faB]=faA;                    dist[faB]=rank[faA];//下面的砖的根距离如今的连通分量的根的距离                    rank[faA]+=rank[faB];//节点相加                }            } else {                scanf("%d",&a);                int x = find(a);                printf("%d\n",rank[x]-dist[a]-1);//连通分量节点数-根节点的距离-1             }        }    }    return 0;}

 

自己选的路,跪着也要走完!!!

 

加权并查集——(POJ1988)Cube Stacking