| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
终于AC了,原来只是一个小小的错误,还我WA了好多次……无语,po一下偶的代码#include<stdio.h>
#define N 30001
int count[N], num[N], pre[N];
int find(int x)
{
if(pre[x] == x)
{
return x;
}
int t = find(pre[x]);
count[x] += count[pre[x]];
pre[x] = t;
return t;
}
void Union(int x, int y)
{
int i = find(x);
int j = find(y);
if(i == j)
{
return;
}
count[i] = num[j];
num[j] += num[i];
pre[i] = j;
}
int main()
{
int i, x, y, n;
char s[2];
scanf("%d",&n);
for(i = 1; i <= N; i++)
{
pre[i] = i;
num[i] = 1;
count[i] = 0;
}
for(i = 0; i < n; i++)
{
scanf("%s",s);
if(s[0] == 'M')
{
scanf("%d%d",&x,&y);
Union(x,y);
}
else if(s[0] == 'C')
{
scanf("%d",&x);
int c = find(x);
printf("%d\n",count[x]);
}
}
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator