洛谷P1536-村村通-题解
村村通
题目描述
某市调查城镇交通状况,得到现有城镇道路统计表。表中列出了每条道路直接连通的城镇。市政府 “村村通工程” 的目标是使全市任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要相互之间可达即可)。请你计算出最少还需要建设多少条道路?
输入格式
输入包含若干组测试数据,每组测试数据的第一行给出两个用空格隔开的正整数,分别是城镇数目 和道路数目 ;随后的 行对应 条道路,每行给出一对用空格隔开的正整数,分别是该条道路直接相连的两个城镇的编号。简单起见,城镇从 到 编号。
注意:两个城市间可以有多条道路相通。
在输入数据的最后,为一行一个整数 ,代表测试数据的结尾。
输出格式
对于每组数据,对应一行一个整数。表示最少还需要建设的道路数目。
样例 #1
样例输入 #1
4 2
1 3
4 3
3 3
1 2
1 3
2 3
5 2
1 2
3 5
999 0
0样例输出 #1
1
0
2
998提示
数据规模与约定
对于 的数据,保证 。
思路
这题就是一道简单的并查集,和修复公路-题解太像了,不过那道题只是问能不能通路,这题还多了一个要修多少条才能通。其实也不难,暴力遍历寻找有没通的两个村,找到一个就给答案加上一次,同时,假装给他修上路(添加到并查集中),遍历结束后,答案就出来了
C++代码
#include <iostream>
#include <vector>
#include <numeric>
template<typename T>
class UnionFindSet {
std::vector<int> trees, size;
public:
explicit UnionFindSet(const T &len) : trees(len), size(len, 1) {
iota(trees.begin(), trees.end(), 0);
}
T find(const T &x) {
return trees[x] == x ? x : trees[x] = find(trees[x]);
}
void unite(const T &x, const T &y) {
T &&fx = find(x), &&fy = find(y);
if (fx == fy) return;
if (size[fx] < size[fy]) std::swap(fx, fy);
trees[fy] = fx;
size[fx] += size[fy];
}
bool isSameSet(const T &x, const T &y) {
return find(x) == find(y);
}
};
using namespace std;
int main() {
int n, m, x, y, ans;
//a ^ b 逻辑上等价 a != b
while(cin >> n and n ^ 0) {
cin >> m;
ans = 0;
UnionFindSet<int> ufs(n + 1);
while(m--) {
cin >> x >> y;
ufs.unite(x,y);
}
for(int i = 1; i <= n; ++i)
for(int j = i + 1; j <= n; ++j)
if(not ufs.isSameSet(i,j)) {
++ans;
ufs.unite(i,j);
}
cout << ans << endl;
}
return 0;
}洛谷P1536-村村通-题解
https://winterl-blog.netlify.app/2023/08/05/洛谷P1536-村村通-题解/