|
1#include <iostream> 2#include <cstdio> 3#include <cstring> 4#define maxsize 30010 5using namespace std; 6typedef struct tree1{ 7 int pre; 8 int num; 9}Tree; 10Tree tree[maxsize]; 11 12int find(int x){ 13 if(tree[x].pre == x){ 14 return x; 15 } 16 return (tree[x].pre = find(tree[x].pre)); 17} 18 19void merge(int x, int y){ 20 int f1 = find(x); 21 int f2 = find(y); 22 if(f1!=f2){ 23 tree[f1].num += tree[f2].num; 24 tree[f2].pre = f1; 25 } 26} 27 28int main() 29{ 30 int m, n, i, j, k; 31 while(scanf("%d%d",&n, &m)!=EOF){ 32 if(0==n&&0==m){ 33 break; 34 } 35 for(i = 0; i < n; i++){ 36 tree[i].pre = i; 37 tree[i].num = 1; 38 } 39 while(m--){ 40 scanf("%d", &k); 41 int a, b; 42 scanf("%d", &a); 43 for(i = 1; i < k; i++){ 44 scanf("%d", &b); 45 merge(a,b); 46 } 47 } 48 int t = find(0); 49 printf("%d\n", tree[t].num); 50 } 51 return 0; 52} 53/**//* 54100 4 552 1 2 565 10 13 11 12 14 572 0 1 582 99 2 59200 2 601 5 615 1 2 3 4 5 621 0 630 0 64*/ 65http://acm.hrbeu.edu.cn/index.php?act=problem&id=1003&cid=50
|