【POJ1988】Cube Stacking(并查集)

记录一个菜逼的成长。。

这里附带几道关系并查集题目。
poj1182(食物链):http://poj.org/problem?id=1182
poj1703(Find them, Catch them):http://poj.org/problem?id=1703
poj2492(A Bug’s Life):http://poj.org/problem?id=2492
poj1988(Cube Stacking):http://poj.org/problem?id=1988
还有一道跟这题几乎一模一样的题 NOI2002 的叫银河英雄传说
————————–poj1988的分割线—————————

题目大意:

给你一个数p,表示有p次操作。
有两种操作,
1.M x y 表示将x所在的那个栈放到y所在的那个栈的上面
2.C x 表示查询在x所在的那个栈里 在x下面有多少个cube。

这里设置三个数组,
pre[i] := 表示i的父亲。
d[i] := 表示i到pre[i]的数量(包括父亲)。
s[i] := 表示i所在的栈的大小。

合并操作:
对于 M x y
1.只需将pre[findr(x)] = findr(y);
2.将y所在的的栈的大小加上x所在的栈的大小 s[findr(y)] += s[findr(x)];
3.并将x所在的栈的大小设为0;s[findr(x)] = 0;

查询语句组织摘自http://blog.csdn.net/liangzhaoyang1/article/details/50900231
查询:
我们设置了一个d数组,d[i]代表i到pre[i]的距离(即cube的数量),当使用路径压缩时,pre[i]会改变,因此d[i]也会发生改变。我们假设对于点x,有pre[pre[x]]=find(x),即父亲的父亲是代表元,那么d[pre[x]]就是父亲到代表元的距离,这样就可以很容易地推出,d[x]=d[pre[x]]+d[x]。现在,d数组的更新问题也解决了。查询结果就是d[x];

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <deque>
#include <cctype>
#include <bitset>
#include <cmath>
using namespace std;
#define ALL(v) (v).begin(),(v).end()
#define cl(a,b) memset(a,b,sizeof(a))
#define bp __builtin_popcount
#define pb push_back
#define mp make_pair
#define fin freopen("D://in.txt","r",stdin)
#define fout freopen("D://out.txt","w",stdout)
#define lson t<<1,l,mid
#define rson t<<1|1,mid+1,r
#define seglen (node[t].r-node[t].l+1)
#define pi 3.1415926
#define exp  2.718281828459
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
template <typename T>
inline void read(T &x){
    T ans=0;
    char last=' ',ch=getchar();
    while(ch<'0' || ch>'9')last=ch,ch=getchar();
    while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
    if(last=='-')ans=-ans;
    x = ans;
}
inline bool DBread(double &num)
{
    char in;double Dec=0.1;
    bool IsN=false,IsD=false;
    in=getchar();
    if(in==EOF) return false;
    while(in!='-'&&in!='.'&&(in<'0'||in>'9'))
        in=getchar();
    if(in=='-'){IsN=true;num=0;}
    else if(in=='.'){IsD=true;num=0;}
    else num=in-'0';
    if(!IsD){
        while(in=getchar(),in>='0'&&in<='9'){
            num*=10;num+=in-'0';}
    }
    if(in!='.'){
        if(IsN) num=-num;
            return true;
    }else{
        while(in=getchar(),in>='0'&&in<='9'){
                num+=Dec*(in-'0');Dec*=0.1;
        }
    }
    if(IsN) num=-num;
    return true;
}
template <typename T>
inline void write(T a) {
    if(a < 0) { putchar('-'); a = -a; }
    if(a >= 10) write(a / 10);
    putchar(a % 10 + '0');
}
/******************head***********************/
const int maxn = 30000 + 10;
int pre[maxn],d[maxn],s[maxn];
void init()
{
    cl(d,0);
    for( int i = 1; i <= maxn; i++ ){
        pre[i] = i;
        s[i] = 1;
    }
}
int findr(int x)
{
    if(pre[x] == x)return x;
    int t = pre[x];
    int fx = findr(pre[x]);
    if(fx != pre[x])
        d[x] = d[t] + d[x];
    return pre[x] = fx;
}
void unio(int x,int y)
{
    int fx = findr(x),fy = findr(y);
    if(fx != fy){
        pre[fx] = fy;
        d[fx] = s[fy];
        s[fy] += s[fx];
        s[fx] = 0;
    }
}
int main()
{
    int Q;
    while(~scanf("%d",&Q)){
        char ope[5];
        int x,y;
        init();
        while(Q--){
            scanf("%s",ope);
            if(ope[0] == 'M'){
                scanf("%d%d",&x,&y);
                unio(x,y);
            }
            else {
                scanf("%d",&x);
                findr(x);//路径压缩
                printf("%d\n",d[x]);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值