POJ1611The Suspects

http://poj.org/problem?id=1611

这个题的话一看就知道是并查集,0号是已经被感染的,凡是与0号在一组的都是感染者

 1 #include<cstdio>

 2 #include<cstring>

 3 #include<iostream>

 4 using namespace std ;

 5 int bing[30100],a[30100];

 6 int findx(int x)

 7 {

 8     int r = x ;

 9     while(bing[r] != r)

10         r = bing[r] ;

11     return r ;

12 }

13 int findy(int x)//递归往下找一组的

14 {

15     if(x != bing[x])

16         bing[x] = findy(bing[x]) ;

17     return bing[x] ;

18 }

19 void merge(int x,int y)

20 {

21     int fx,fy ;

22     fx = findx(x) ;

23     fy = findx(y) ;

24     if(fx != fy)

25     {

26         if(a[fx] > a[fy])

27         {

28             bing[fy] = fx ;

29             a[fx] += a[fy];

30         }

31         else

32         {

33             bing[fx] = fy ;

34             a[fy] += a[fx] ;

35         }

36     }

37 }

38 int main()

39 {

40     int n,m,x,y,count ;

41     while(~scanf("%d %d",&m,&n))

42     {

43         if(m == 0&& n == 0) break ;

44         for(int i = 0 ; i <= m-1 ; i++)

45 

46            {

47                bing[i] = i ;

48                a[i] = 1 ;

49            }

50         int s ;

51         for(int i = 1 ; i <= n ; i++)

52         {

53             scanf("%d",&s) ;

54             scanf("%d",&x) ;

55             for(int j = 1 ; j <= s-1 ; j++)

56             {

57                 scanf("%d",&y) ;

58                 merge(x,y) ;

59             }

60         }

61         count = findy(0);

62         printf("%d\n",a[count]);

63     }

64     return 0 ;

65 }
View Code

 

你可能感兴趣的:(poj)