您的位置:首页 > 其它

poj 1988 Cube Stacking

2015-03-15 23:56 316 查看
不多说了,傻逼的一道题。。。
#include<stdio.h>

const int M = 30005;
int pa[M],cnt[M],dist[M];

void Init()
{
for (int i = 1;i <= M;i ++)
pa[i] = i,cnt[i] = 1,dist[i] = 0;
}

int findset (int x)
{
if (x != pa[x])
{
int root = findset (pa[x]);
dist[x] += dist[pa[x]];
return pa[x] = root;
}
else return x;
}
void Unin(int a,int b)
{
int af = findset (a);
int bf = findset (b);
pa[bf] = af;
dist[bf] = cnt[af];
cnt[af] += cnt[bf];
}
int main ()
{
int m,x,y;
char op[3];
while (~scanf ("%d",&m))
{
Init();
while (m --)
{
scanf ("%s",op);
if (op[0] == 'M')
{
scanf ("%d%d",&x,&y);
Unin(x,y);
}
else
{
scanf ("%d",&x);
int af = findset (x);
printf ("%d\n",cnt[af]-dist[x]-1);
}

}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: