HDU1213通畅工程-并查集求解

并查集的经典题目。

并查集。经典题目是HDU1232通畅工程。

题目描述:

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

Output:

对每个测试用例,在1行里输出最少还需要建设的道路数目。

Sample Input:

测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。
注意:两个城市之间可以有多条道路相通,也就是说
3 3
1 2
1 2
2 1
这种输入也是合法的
当N为0时,输入结束,该用例不被处理。

Sample Output:

4 2
1 3
4 3
3 3
1 2
1 3
2 3
5 2
1 2
3 5
999 0
0

思路

相互连通的城市组成城市群。城市群有一个代表。不断输入道路意味着城市之间的互联,如果这两个城市原来属于不同城市群则发生城市群合并时间。问询某个城市的城市群id,依赖于其“父节点”,父节点再从它的父节点询问,直到Boss节点。

find过程:查找城市群Boss id的过程。存在路径优化。
union过程:合并原有的两个城市群。利用rank合并,避免最坏情况线性N级树结构导致find低效。

一个union的case:

       (3)
  A----------C
 /|        /|
  |          |
  |(1)       |(2)
  |          |
  B          D
 /|
  |(4)
  |
  E

代码

翻出7年前的AC代码,发现没有路径优化、各种全局变量、

//hdu1213
//find_union_set
#include <iostream>
#include <stdio.h>
using namespace std;
int p[1001];
int find(int x)
{
	return p[x]==x?x:p[x]=find(p[x]);
}
void Union(int a,int b)
{
	p[find(a)]=find(b);
}
int main()
{
//	freopen("Chris.txt","r",stdin);
	int n,m;
	while(scanf("%d",&n)!=EOF)
	{
		if(n==0)break;
		scanf("%d",&m);
		int i;
		for(i=0;i<=n;i++)
			p[i]=i;
		for(i=0;i<m;i++)
		{
			int a,b;
			scanf("%d%d",&a,&b);
			if(find(a)!=find(b))
			{	
				Union(a,b);
				n--;
			}
		}
		printf("%d
",n-1);
	}
	return 0;
}

考虑去掉全局变量,添加路径压缩、利用rank优化union过程、封装为结构体、添加注释,代码如下:

#include <stdio.h>
#include <string.h>

#define MAXN 1005

//FSU: Find-Union-Set algorithm node struct
typedef struct FSU_Node FSU_Node;
struct FSU_Node{
	int p; // parent id
	int rank; // rank is defined as number of childern nodes
};

//find node (whose id is x)'s belonging group's root node id
// @param x: node id
// @param nodes: all the nodes (contain several node groups)
int fus_find(int x, FSU_Node* nodes) {
	if (nodes[x].p == x) return x;
	nodes[x].p = fus_find(nodes[x].p, nodes); // squeeze the finding path
	return nodes[x].p;
}

//merge two node groups
// @param a: a node from one node group
// @param b: a node from another node group
void fus_union(int a, int b, FSU_Node* nodes) {
	int root_a = fus_find(a, nodes);
	int root_b = fus_find(b, nodes);
	if (root_a == root_b) return;
	
	// merge node groups according to rank
	// which avoids the worst case: all nodes in one line (so many level of nodes)
	if (nodes[root_a].rank > nodes[root_b].rank) {
		nodes[root_b].p = root_a;
	}
	else {
		if (nodes[root_a].rank == nodes[root_b].rank) {
			nodes[root_b].rank++;
		}
		nodes[root_a].p = root_b;
	}
}


#define DEBUG
int main() {
#ifdef DEBUG
	freopen("F:/zhangzhuo/debug/OJ/HDU-1232.txt", "r", stdin);
#endif

	FSU_Node nodes[MAXN];

	int n, m, i, j, k;
	while (scanf("%d", &n) && n) {
		for (i = 0; i <= n; i++) {
			nodes[i].p = i;
			nodes[i].rank = 1;
		}

		scanf("%d", &m);
		int cityId1, cityId2;

		for (i = 0; i < m; i++) {
			scanf("%d %d", &cityId1, &cityId2);
			if (fus_find(cityId1, nodes) != fus_find(cityId2, nodes)) {
				fus_union(cityId1, cityId2, nodes);
				n--;
			}
		}
		printf("%d
", n - 1);
	}

	return 0;
}
原文地址:https://www.cnblogs.com/zjutzz/p/11017424.html