标签:
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 483 Accepted Submission(s): 262
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <cmath> 6 #include <cstdlib> 7 #include <cctype> 8 #include <queue> 9 #include <stack> 10 #include <map> 11 #include <vector> 12 #include <set> 13 #include <utility> 14 typedef long long ll; 15 const int inf=0x3f3f3f3f; 16 using namespace std; 17 18 int a[3000],n,m; 19 void init() //初始化 20 { 21 for(int i=0; i<=n; i++) 22 a[i]=i; 23 } 24 // 25 int father(int v) //寻找父亲结点 26 { 27 if(a[v]==v) return v; 28 return a[v]=father(a[v]); 29 } 30 void unite(int u,int v) //合并 31 { 32 int tu=father(u),tv=father(v); 33 if(tv!=tu) a[tv]=tu; 34 // 35 } 36 int main() 37 { 38 //freopen("input.txt","r",stdin); 39 int u,v; 40 while(scanf("%d%d",&n,&m)!=EOF) 41 { 42 init(); 43 int ans=0; 44 while(m--) 45 { 46 scanf("%d%d",&u,&v); 47 if(father(u)==father(v)) 48 ans++; 49 else 50 unite(u,v); 51 } 52 // 53 printf("%d\n",ans); 54 } 55 return 0; 56 }
标签:
原文地址:http://www.cnblogs.com/geek1116/p/5733618.html