标签:
Description
Input
Output
Sample Input
Sample Output
1 # include<iostream> 2 # include<cstdio> 3 # include<cstring> 4 # include<vector> 5 # include<set> 6 # include<vector> 7 # include<algorithm> 8 using namespace std; 9 int n,m; 10 int pre[1005]; 11 struct edge 12 { 13 int v1,v2; 14 }; 15 vector<edge>e; 16 int find(int x) 17 { 18 if(pre[x]==x) 19 return x; 20 return pre[x]=find(pre[x]); 21 /*int u=x; 22 while(u!=pre[u]) 23 u=pre[u]; 24 while(x!=pre[x]){ 25 int v=x; 26 x=pre[x]; 27 pre[v]=u; 28 } 29 return u;*/ 30 } 31 void solve(int v1,int v2) 32 { 33 34 //for(int i=0;i<e.size();++i){ 35 int u=find(v1); 36 int v=find(v2); 37 if(u!=v) 38 pre[u]=v; 39 //} 40 /*for(int i=0;i<e.size();++i){ 41 int u=find(e[i].v1); 42 int v=find(e[i].v2); 43 if(u!=v) 44 pre[u]=v; 45 }*/ 46 } 47 int main() 48 { 49 int a,b; 50 while(scanf("%d",&n)&&n) 51 { 52 for(int i=1;i<=n;++i) 53 pre[i]=i; 54 scanf("%d",&m); 55 while(m--){ 56 scanf("%d%d",&a,&b); 57 //if(!mark[a][b]){ 58 solve(a,b); 59 //mark[a][b]=mark[b][a]=1; 60 //} 61 } 62 int num=0; 63 for(int i=1;i<=n;++i) 64 if(pre[i]==i) 65 ++num; 66 printf("%d\n",num-1); 67 } 68 return 0; 69 }
标签:
原文地址:http://www.cnblogs.com/20143605--pcx/p/4681955.html