您的位置:首页 > 其它

POJ 1988 Cube Stacking

2016-07-23 17:21 316 查看
题目地址

total[x]记录最下方木块是x,这一堆总共有多少木块

under[x]记录x木块下面有几块木块

#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<cctype>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=30000 + 5;
int Par[maxn],total[maxn],under[maxn];
int getPar(int x){
if(Par[x]==x) return x;
int p=getPar(Par[x]);
under[x]+=under[Par[x]]; //加上到那一堆之前的所有根节点
Par[x]=p;
return p;
}
void merge(int a,int b){
int p1=getPar(a);
int p2=getPar(b);
if(p1==p2) return ;
Par[p2]=p1;
under[p2]=total[p1];
total[p1]+=total[p2];
}
int main()
{
int N;
cin>>N;
for(int i=1;i<maxn;i++){
total[i]=1;
under[i]=0;
Par[i]=i;
}
string cmd;
while(N--)
{
cin>>cmd;
if(cmd[0]=='M') {
int x,y; cin>>x>>y;
merge(y,x);
}
else {
int x; cin>>x;
getPar(x);
cout<<under[x]<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: