CSU-ACM 1004: Xi and Bo

#CSU-ACM 1004: Xi and Bo
##Description
Bo has been in Changsha for four years. However he spends most of his time staying his small dormitory. One day he decides to get out of the dormitory and see the beautiful city. So he asks to Xi to know whether he can get to another bus station from a bus station. Xi is not a good man because he doesn’t tell Bo directly. He tells to Bo about some buses’ routes. Now Bo turns to you and he hopes you to tell him whether he can get to another bus station from a bus station directly according to the Xi’s information.
##Input
The first line of the input contains a single integer T (0<T<30) which is the number of test cases. For each test case, the first contains two different numbers representing the starting station and the ending station that Bo asks. The second line is the number n (0<n<=50) of buses’ routes which Xi tells. For each of the following n lines, the first number m (2<=m<= 100) which stands for the number of bus station in the bus’ route. The remaining m numbers represents the m bus station. All of the bus stations are represented by a number, which is between 0 and 100.So you can think that there are only 100 bus stations in Changsha.
##Output
For each test case, output the “Yes” if Bo can get to the ending station from the starting station by taking some of buses which Xi tells. Otherwise output “No”. One line per each case. Quotes should not be included.
##Sample Input

3
0 3
3
3 1 2 3
3 4 5 6
3 1 5 6
0 4
2
3 0 2 3
2 2 4
3 2
1
4 2 1 0 3
##Sample Output
No
Yes
Yes
##C++ Source code

#include <iostream>
using namespace std;
#define N 100
int s[N];
int in[N];
void make_set()
{
	for (int i = 0;i < N;i++) s[i] = i;
}
// 找到元素i所在集合的代表元素
int find_set(int i)
{
	return s[i] == i ? i : s[i] = find_set(s[i]);
}
bool union_set(int s1, int s2)
{
	int p_s1 = find_set(s1);// 找到s1和s2所在集合的代表元素(或者说组号)
	int p_s2 = find_set(s2);
	if (p_s1 != p_s2)
	{
		for (int i = 0;i < N;i++)
		{
			if (s[i] == p_s2)
				s[i] = p_s1;
		}
		return true;
	}
	return false;
}


int main()
{
	make_set();
	int t;
	cin >> t;
	while (t--)
	{
		int start, end;// 表示起始站
		cin >> start >> end;
		int num;// 表示给出的线路数目
		cin >> num;
		for (int i = 0;i < num;i++)
		{
			int count;
			cin >> count;
			for (int i = 0;i < count;i++)
				cin >> in[i];
			for (int i = 1;i < count;i++)
				union_set(in[i - 1], in[i]);
		}
		cout << (find_set(start) == find_set(end) ? "Yes" : "No") << endl;
		make_set();
	}
	return 0;
}
/**************************************************************
	Problem: 1004
	User: someone
	Language: C++
	Result: AC
	Time:0 ms
	Memory:1696 kb
/****************************************************************/

##解题思路

###这里涉及到并查集算法
定义

并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题。常常在使用中以森林来表示集就是让每个元素构成一个单元素的集合,也就是按一定顺序将属于同一组的元素所在的集合合并。

主要操作

用该算法解决问题一般实现三个方法(分为三个步骤):

step1: 初始化,将每个节点所在集合初始化为本身。用数组实现的话,数组下标表示节点,数组下标所对应的值来表示集合ID.
step2: 查找,比如说给定一个节点,找到该节点所在的集合,用数组处理的话一般
用将集合的值(即ID)设置为代表元素的下标值.
step3: 合并,给定一个节点对(x,y),表示x,y之间是可以连通的,首先分别找到节点所在的集合.如果两个节点在相同的集合,则不需要合并.如果在不同的集合,需要将两个集合进行合并,合并的方法通常是将一个集合中的元素全部加入到另一个集合中,即需要遍历数组,将y集合中的元素集合ID设置为x的集合ID

下面举个例子:
初始情况是数组元素这样的,每个节点自成集合:

01234567
01234567

比如现在给出一个节点对(1,2),通过查找函数发现节点1对应的集合ID为1,集合2对应的集合ID为2,二者是不相等的.那么将集合ID为2中的节点并入集合1中,因为我们并不知道有哪些节点对应的集合ID为2,所以需要遍历数组,然后将其集合ID设置为1,完成集合的合并.

01234567
01134567

比如下面又给出了节点对(4,7),和上面相同的做法.

01234567
01134564

这时候如何判断1和7是否连通呢?通过查看下标1和7的值发现他们的集合ID是不同的,所以1到7是不可以到达的.
然后再加一个节点对(2,4),数组就变成这样子啦.

01234567
01131561

或者也可以写成下面这样.

01234567
04434564

这时候判断1和7是否连通,只需看他们是否在一个集合中就行了,他们的集合ID是相同的所以可以互通.

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值