首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

通畅工程

2013-04-12 
畅通工程题目#include stdio.h#include stdlib.h int father[1001] int find_set(int x)void union_

畅通工程
题目

#include <stdio.h>#include <stdlib.h> int father[1001]; int find_set(int x);void union_set(int x, int y); int main(){    int n, m, i, x, y, count;     while (scanf("%d", &n) != EOF && n != 0) {        for (i  = 1; i <= n; i ++) {            father[i] = i;        }         for (scanf("%d", &m); m > 0; m --) {            scanf("%d %d", &x, &y);            union_set(x, y);        }         for (count = 0, i = 1; i <= n; i ++) {            if (father[i] == i) {                count ++;            }        }         printf("%d\n", count - 1);    }     return 0;} int find_set(int x){    int r = x;     while (father[r] != r) {        r = father[r];    }     return r;} void union_set(int x, int y){    int fx, fy;    fx = find_set(x);    fy = find_set(y);     if (fx != fy)        father[fx] = fy;}/**************************************************************    Problem: 1012    User: wangzhengyi    Language: C    Result: Accepted    Time:10 ms    Memory:912 kb****************************************************************/


热点排行