1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <cstdlib> 5 using namespace std; 6 7 #define MAXN 1005 8 int n, m; 9 int fa[MAXN]; //父节点 10 int mark[MAXN]; 11 12 void init() 13 { 14 for (int i = 0; i <= n; i++) 15 { 16 fa[i] = i; 17 mark[i] = 0; 18 } 19 } 20 21 int find(int x) 22 { 23 if (x == fa[x]) 24 return x; 25 else 26 return fa[x] = find(fa[x]); 27 } 28 29 void set_union(int x, int y) 30 { 31 int x1 = find(x); 32 int y1 = find(y); 33 34 if (x1 == y1) 35 return; 36 if (mark[x1] < mark[y1]) 37 { 38 fa[x1] = y1; 39 } 40 else 41 fa[y1] = x1; 42 if (mark[x1] == mark[y1]) 43 mark[x1]++; 44 } 45 46 int main() 47 { 48 while (~scanf("%d",&n)) 49 { 50 if (n == 0) 51 break; 52 scanf("%d", &m); 53 init(); 54 for (int i = 0; i < m; i++) 55 { 56 int a, b; 57 scanf("%d %d", &a, &b); 58 set_union(a, b); 59 } 60 61 int ans = 0; 62 for (int i = 1; i <= n; i++) 63 { 64 if (find(i) == i) 65 ans++; 66 } 67 printf("%d ", ans - 1); 68 } 69 //system("pause"); 70 return 0; 71 }