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

why wa?why wa?

Posted by iiikkkkk at 2006-05-09 12:01:47 on Problem 1988
#include<stdio.h>
long i,p,x,y;
struct dd{
	long d,sum,top;
}a[30100];

long findroot(long xq){
	long it,kt,i1,jt;
	kt=0;
	it=xq;
	while(a[it].top!=it){
		kt+=a[it].d;		
		it=a[it].top;
	};
	i1=it;
	it=xq;
	while(a[it].top!=it){
		jt=a[it].top;
		a[it].top=i1;
		a[jt].d=kt-a[it].d;
		a[it].d=kt;
		kt=a[jt].d;
		it=jt;
	}
	return i1;
}
	
void uni(long xt,long yt){
	long iq,jq;
	iq=findroot(xt);
	jq=findroot(yt);
	a[jq].top=iq;
	a[jq].d=a[iq].sum;
	a[iq].sum+=a[jq].sum;
}
int main(){
    char op;
    scanf("%ld",&p);                   
	for (i=0;i<30004;i++){
		a[i].d=0;
		a[i].sum=1;
		a[i].top=i;
	}
    while(p--){
        scanf("\n%c",&op);            
        switch(op){
            case 'M':                
                scanf("%ld%ld",&x,&y);
                uni(x,y);            
                break;
            case 'C':
                scanf("%ld",&x);
                printf("%ld\n",a[findroot(x)].sum-a[x].d-1);
                break;
		}
    }
	return 0;
}

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