解题思路:并查集水题,多年前曾经用暴力水过。
解题代码:
1 // File Name: c.c 2 // Author: darkdream 3 // Created Time: 2013年03月01日 星期五 00时37分28秒 4 5 #include<stdio.h> 6 #include<string.h> 7 #include<stdlib.h> 8 #include<time.h> 9 #include<math.h> 10 11 bool a[102][102] = {0}; 12 int f[102]; 13 int b[102] = {0}; 14 int main(){ 15 16 // freopen("input.txt","r",stdin); 17 // freopen("output.txt","w",stdout); 18 int n , m , p = 0; 19 scanf("%d %d",&n,&m); 20 for(int i = 1; i <= n; i++) 21 { 22 int t; 23 scanf("%d",&f[i]); 24 if(f[i] == 0 ) 25 { p++; 26 b[i] = 1; 27 28 } 29 for (int j = 1 ; j <= f[i] ;j++) 30 { 31 int k ; 32 scanf("%d",&k); 33 a[i][k] = 1; 34 } 35 36 } 37 for(int s = 1; s<= 3;s ++) 38 for(int i = 1; i<= n; i++) 39 { if(!b[i]) 40 for(int j = i+1; j <= n ;j++) 41 { 42 for(int t = 1; t <= m ; t++) 43 if(a[i][t] ==1 && a[j][t] == 1) 44 { 45 b[j] = 1 ; 46 for (int k = 1; k <= m ;k ++) 47 a[i][k] = a[i][k] || a[j][k]; 48 break; 49 } 50 } 51 } 52 int sum = 0; 53 for(int i = 1; i <= n; i++) 54 if(b[i] == 0) 55 { 56 sum++; 57 } 58 if(sum == 0) 59 sum = 1; 60 sum = sum-1 +p; 61 printf("%d",sum ); 62 return 0 ; 63 }