| 
		
			| 
	
	
		
			一个并查集题目,地址:http://acm.pku.edu.cn/JudgeOnline/problem?id=1988  #include <stdio.h> 
  
  #include <string.h> 
  
   int num[30005];/**//*路径*/ 
   int c[30005];/**//*集合中比C大的数的数量*/ 
  
  void make ( int n ) 
    { 
  
  for ( int i=0; i<n; i++ ) 
     { 
  num[i] = -1; 
  c[i] = 0; 
  } 
  } 
  
  int find ( int a ) 
    { 
  
  if ( num[a] < 0 ) 
     { 
  return a; 
  } 
  int r = find ( num[a] ); 
  c[a] += c[ num[a] ]; 
  num[a] = r; 
  
  return r; 
  } 
  
  void un ( int a, int b ) 
    { 
  
  int ra = find ( a ); 
  int rb = find ( b ); 
  
  c[rb] += -num[ra]; 
  num[ra] += num[rb]; 
  num[rb] = ra; 
  } 
  
  int main () 
    { 
  
  int p; 
  char in[5]; 
  int a, b; 
  
  while ( scanf ( "%d", &p ) != EOF ) 
     { 
  make ( 30000 ); 
  for ( int i=0; i<p; i++ ) 
     { 
  scanf ( "%s", in ); 
  if ( ! strcmp ( in, "M" ) ) 
     { 
  scanf ( "%d%d", &a, &b ); 
  if ( find ( a-1 ) != find ( b-1 ) ) 
     { 
  un ( a-1, b-1 ); 
  } 
  } 
  else 
     { 
  scanf ( "%d", &a ); 
  int ra = find ( a-1 ); 
  
  printf ( "%d\n", -num[ra]-c[a-1]-1 ); 
  } 
  } 
  } 
  return 0; 
  } 
      
	    
    
 |  | 
		
			| 
			
				| 
	|  |  | 日 | 一 | 二 | 三 | 四 | 五 | 六 | 
|---|
 | 30 | 31 | 1 | 2 | 3 | 4 | 5 |  | 6 | 7 | 8 | 9 | 10 | 11 | 12 |  | 13 | 14 | 15 | 16 | 17 | 18 | 19 |  | 20 | 21 | 22 | 23 | 24 | 25 | 26 |  | 27 | 28 | 29 | 30 | 1 | 2 | 3 |  | 4 | 5 | 6 | 7 | 8 | 9 | 10 |  |  公告决定从线程开始!! 常用链接留言簿(6)随笔档案搜索最新评论
	阅读排行榜评论排行榜
 |  |