Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:谁能帮我看看,WA,自己测了很多数据都没问题.

Posted by asdqwe at 2006-07-20 21:04:46 on Problem 1988
In Reply To:谁能帮我看看,WA,自己测了很多数据都没问题. Posted by:asdqwe at 2006-07-20 13:14:50
> #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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator