TYVJ 1017 冗余关系 by C++

就机会复习一下并查集…
   1: #include<iostream>
   2: #include<string.h>
   3: using namespace std;
   4: const int maxn=1001;
   5: int f[maxn];
   6: int n,m,tot=0;
   7:  
   8: int find(int x)
   9: {
  10:     int i=x;
  11:     while (f[i]!=0) i=f[i];
  12:     return i;
  13: }
  14:  
  15: int main()
  16: {
  17:     memset(f,0,sizeof(f));
  18:     cin >> n >> m;
  19:     int prex,prey,fx,fy;
  20:     for (int i=0;i<n;i++){
  21:         cin >> prex >> prey;
  22:         fx=find(prex);
  23:         fy=find(prey);
  24:         fx==fy?tot++:f[fx]=fy;
  25:     }
  26:     cout << tot << endl;
  27:     return 0;
  28: }
posted @ 2012-07-07 20:42  NoRush  阅读(208)  评论(0编辑  收藏  举报