Follows 793.c (Total 65 lines):

/* @JUDGE_ID:4461XX 793 C */
/* A */
#include<stdio.h>
#include<string.h>

#define MAX 1000

int com[MAX] , n , yes , no ;

int traceup( int computer )
{
	for( ; com[computer]!=computer ; computer=com[computer] ) ;

	return computer ;
}
void connect( int c1 , int c2 )
{
	c1 = traceup( c1 ) ;
	c2 = traceup( c2 ) ;
	if( c1>c2 ) c1 ^= c2 ^= c1 ^= c2 ; /* swap */

	com[c2] = c1 ;
}
void question( int c1 , int c2 )
{
	c1 = traceup( c1 ) ;
	c2 = traceup( c2 ) ;

	if( c1==c2 ) ++yes ;
	else ++no ;
}
void ToRun( void )
{
	int i , c1 , c2 ;
	char input[100] , ch ;

	for( i=0 ; i<n ; ++i ) com[i] = i ; /* initial */
	yes = no = 0 ;

	while( gets( input ) ){
		if( !strlen( input ) ) break ;

		sscanf( input , "%c %d %d" , &ch , &c1 , &c2 ) ;
		--c1 ;
		--c2 ;
		switch( ch ){
			case 'c' : connect( c1 , c2 ) ; break ;
			case 'q' : question( c1 , c2 ) ; break ;
		}
	}
}
int main( void )
{
	int cases ;

	scanf( "%d\n\n" , &cases ) ;
	for( ; cases ; --cases ){
		scanf( "%d\n" , &n ) ;
		ToRun() ;
		printf( "%d,%d\n" , yes , no ) ;
		if( cases-1 ) putchar( '\n' ) ;
	}

	return 0 ;
}

Back to statistics
Ya-Lin Huang (C)
huangyl@gmail.com