Problem Description
某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?
Input
测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N (
#include
#include
int father[1001];
int FindGF(int a)//找祖父节点
{
while (father[a] != a)
a = father[a];
return a;
}
void Branch(int a, int b)//合并不同的祖父节点
{
int fx, fy;
fx = FindGF(a);
fy = FindGF(b);
if (fx != fy)
father[fx] = fy;
}
int main()
{
int n, m, i, x, y, ans;
while (scanf(“%d”, &n) && n != 0)
{
for (i = 1; i
授权协议:创作共用 署名-非商业性使用 2.5 中国大陆
除注明外,本站文章均为原创;转载时请保留上述链接。
不错
不错
不错
不错