| ||||||||||
| 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 | |||||||||
谁能帮我看看,WA,自己测了很多数据都没问题.#include <iostream>
using namespace std;
int uset[30011],num[30011],sub[300011];
void init(int n)
{
int i;
for (i=1;i<n;i++)
uset[i]=i,num[i]=1,sub[i]=0;
}
int root(int v)
{
if(uset[v] == v)
return v;
uset[v] = root(uset[v]);
return uset[v];
}
void merge(int a,int b)
{
int x,y;
x=root(a),y=root(b);
uset[y]=uset[x];
sub[y]=num[x];
num[x]+=num[y];
}
int count (int a)
{
int total=0,x=a;
while (uset[x]!=x)
{
total+=sub[x];
x=uset[x];
}
uset[a]=x;
sub[a]=total;
return num[x]-sub[a]-1;
}
main ()
{
// #ifndef ONLINE_JUDGE
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
// #endif
int a,b,n,p;
char c[3];
scanf ("%d",&p);
init (30011);
while (p--)
{
scanf ("%s",&c);
if (c[0]=='C')
{
scanf ("%d",&a);
printf ("%d\n",count(a));
}
else
{
scanf ("%d%d",&a,&b);
merge(a,b);
}
}
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator