标签:
1 //It is made by jump~ 2 #include <iostream> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cstdio> 6 #include <cmath> 7 #include <algorithm> 8 #include <ctime> 9 #include <vector> 10 #include <queue> 11 #include <map> 12 #include <set> 13 #ifdef WIN32 14 #define OT "%I64d" 15 #else 16 #define OT "%lld" 17 #endif 18 using namespace std; 19 typedef long long LL; 20 const int MAXN = 1011; 21 int n,m,cnt; 22 int father[MAXN]; 23 24 inline int getint() 25 { 26 int w=0,q=0; 27 char c=getchar(); 28 while((c<‘0‘ || c>‘9‘) && c!=‘-‘) c=getchar(); 29 if (c==‘-‘) q=1, c=getchar(); 30 while (c>=‘0‘ && c<=‘9‘) w=w*10+c-‘0‘, c=getchar(); 31 return q ? -w : w; 32 } 33 34 inline int find(int x){ 35 if(father[x]!=x) father[x]=find(father[x]); 36 return father[x]; 37 } 38 39 inline void hebing(int x,int y){ 40 int r1=find(x),r2=find(y); 41 if(r1!=r2) father[r2]=r1; 42 } 43 44 inline void work(){ 45 while(1) { 46 n=getint(); if(n==0) break; 47 m=getint(); int x,y; cnt=0; 48 for(int i=1;i<=n;i++) father[i]=i; 49 for(int i=1;i<=m;i++) x=getint(),y=getint(),hebing(x,y); 50 for(int i=1;i<=n;i++) if(father[i]==i) cnt++; 51 printf("%d\n",cnt-1); 52 } 53 } 54 55 int main() 56 { 57 work(); 58 return 0; 59 }
标签:
原文地址:http://www.cnblogs.com/ljh2000-jump/p/5764422.html