# HDOJ 1232 畅通工程（并查集）这道题，为什么WA，麻烦给看下吧 :)

0
[已关闭问题] 关闭于 2011-02-13 16:46

` 1 #include <iostream> 2 #include <cstdio> 3  4  #define MAX 1000 5  6 using namespace std; 7  8 int parent[MAX]; 9 int N;10 11 void init()12 {13     for (int i = 0; i < N; i++) parent[i] = i;14 }15 16 int find(int x)17 {18     int i = x, r = x;19     while (r != parent[r]) r = parent[r];20     int j;21     while (i != r)22     {23         j = parent[i];24         parent[i] = r;25         i = j;26     }27     return r;28 }29 30 void merge(int x, int y)31 {32     x = find(x);33     y = find(y);34     if (x != y) parent[x] = y;35 }36 37 int main()38 {39     //freopen("in.txt", "r", stdin);40     int m;41     while (scanf("%d", &N), N != 0)42     {43         scanf("%d", &m);44         int res = 0;45         init();46         while (m--)47         {48             int x, y;49             scanf("%d %d", &x, &y);50             merge(x, y);51         }52         for (int i = 0; i < N; i++)53         {54             if (i == parent[i]) res++;55         }56         printf("%d\n", res - 1);57     }58 }59 `

cbq926 | 初学一级 | 园豆：200

您需要登录以后才能回答，未注册用户请先注册