1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 int f[1001]; 6 int vis[1001][1001]; 7 int vis2[1001]; 8 int find(int x) 9 { 10 if(x!=f[x]) 11 f[x]=find(f[x]); 12 return x; 13 } 14 void unionn(int x,int y) 15 { 16 int fx=find(x); 17 int fy=find(y); 18 f[fy]=fx; 19 } 20 int dfs(int i) 21 { 22 vis2[i]=1; 23 if(i!=f[i]) 24 { 25 vis2[f[i]]=1; 26 dfs(f[i]); 27 } 28 return i; 29 } 30 31 int main() 32 { 33 int n,m; 34 while(scanf("%d",&n)) 35 { 36 37 memset(vis,0,sizeof(vis)); 38 memset(vis2,0,sizeof(vis2)); 39 int tot=0; 40 if(n==0)break; 41 scanf("%d",&m); 42 for(int i=1;i<=n;i++) 43 f[i]=i; 44 for(int i=1;i<=m;i++) 45 { 46 int x,y; 47 scanf("%d%d",&x,&y); 48 if(x==y)continue; 49 if(vis[x][y]==1||vis[y][x]==1)continue; 50 if(x>y) 51 { 52 swap(x,y); 53 } 54 unionn(x,y); 55 vis[x][y]=1; 56 vis[y][x]=1; 57 } 58 for(int i=n;i>=1;i--) 59 { 60 if(vis2[i]==0) 61 { 62 dfs(i); 63 tot++; 64 } 65 66 } 67 //cout<<endl; 68 cout<<tot-1; 69 //cout<<endl; 70 //cout<<endl; 71 } 72 73 return 0; 74 }