| Time Limit: 2000MS | Memory Limit: 30000K | |
| Total Submissions: 17359 | Accepted: 5991 | |
| Case Time Limit: 1000MS | ||
Description
Input
Output
Sample Input
6 M 1 6 C 1 M 2 4 M 2 6 C 3 C 4
Sample Output
1 0 2
Source
#include<stdio.h>
const int maxn=30001;
int f[maxn],h[maxn],deep[maxn];
using namespace std;
int get(int u)
{
if (u==f[u]) return u;
int temp=f[u];
f[u]=get(f[u]);
h[u]+=h[temp];
return f[u];
}
void Union(int x,int y)
{
f[x]=y;
h[x]=deep[y];
deep[y]+=deep[x];
deep[x]=0;
}
int main()
{
int n,x,y;scanf("%ld",&n);
char enter,c;scanf("%c",&enter);
for (int i=1;i<maxn;i++)
{
f[i]=i;
h[i]=0;
deep[i]=1;
}
while (n>0)
{
n--;
scanf("%c",&c);
if (c==‘M‘)
{
scanf("%ld%ld",&x,&y);
int fx=get(x);
int fy=get(y);
if (fx!=fy) Union(fx,fy);
}
else
{
scanf("%ld",&x);
int p=get(x);
printf("%ld\n",h[x]);
}
scanf("%c",&enter);
}
return 0;
}usaco 2004 Open Cube Stacking 堆方块 题解,布布扣,bubuko.com
usaco 2004 Open Cube Stacking 堆方块 题解
原文:http://blog.csdn.net/jiangshibiao/article/details/20133907