【洛谷】【最小生成树】P1536 村村通

【题目描述:】

某市调查城镇交通状况,得到现有城镇道路统计表。表中列出了每条道路直接连通的城镇。市政府“村村通工程”的目标是使全市任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要相互之间可达即可)。请你计算出最少还需要建设多少条道路?

【输入格式:】

每个输入文件包含若干组测试测试数据,每组测试数据的第一行给出两个用空格隔开的正整数,分别是城镇数目N(N<1000)和道路数目M;随后的M行对应M条道路,每行给出一对用空格隔开的正整数,分别是该条道路直接相连的两个城镇的编号。简单起见,城镇从1到N编号。

注意:两个城市间可以有多条道路相通。例如:

3 3 1 2 1 2 2 1 这组数据也是合法的。当N为0时,输入结束。

【输出格式:】

对于每组数据,对应一行一个整数。表示最少还需要建设的道路数目。



[算法分析:]

其实就是生成树,一开始有的道路加入到生成树里,

最后对\((n-1)\)个点进行枚举,如果没有就加入,一直到边数等于\(n-1\)

统计最后累加的边数就好.



\([Code:]\)

#include<iostream>
#include<cstdio>
using namespace std;

const int MAXN = 1000 + 1;
const int MAXM = 500500 + 1;

int n, m;
int fa[MAXN];

struct Edge {
    int x, y;
}h[MAXM];

int find(int x) {
    if(fa[x] != x) fa[x] = find(fa[x]);
    return fa[x];
}

inline int read() {
    int x=0; char ch = getchar();
    while(ch<'0' || ch>'9') ch = getchar();
    while(ch>='0' && ch<='9')
      x=(x<<3)+(x<<1)+ch-48, ch=getchar();
    return x;
}

int main() {
    n = read();
    while(n) {
        m = read();
        for(int i=1; i<=n; ++i) fa[i] = i;
        for(int i=1; i<=m; ++i) {
            h[i].x = read(), h[i].y = read();
            int fx = find(h[i].x), fy = find(h[i].y);
            if(fx != fy) fa[fx] = fy;
        }
        int tot = 0;
        for(int i=1; i<n; ++i) {
            int fx = find(i), fy = find(i+1);
            if(fx != fy) {
                fa[fx] = fy;
                ++tot;
            }
            if(tot == n-1) break;
        }
        printf("%d\n", tot);
        n = read();
    }
}

猜你喜欢

转载自www.cnblogs.com/devilk-sjj/p/9088661.html
今日推荐