poj 2524 Ubiquitous Religions

//这题是我接触并查集的第二题,好顺利AC了!
//题目给出几个数据对,在同一个数据对中的人的信仰是相同的,需要你求出在给定的人数中,有几种信仰!
//将有相同信仰的人组成一颗树,然后统计数的棵树,再加上还有统计的人数,就可以得出了答案!
#include "iostream"
#include "memory.h"
#include "set"
using namespace std;

const int maxsize = 50010;
set<int> s;
int pa[maxsize];//集合每一个元素的父节点是自己本身!
int ranks[maxsize];//集合每一个元素的秩
bool visited[maxsize];//集合中的元素是否已经访问!

//下面是并查集的三大步骤:先初始化,路径压缩+递归的查找,根据秩的合并!
void make_set(int x)
{
	pa[x] = x;
	ranks[x] = 0;
}

int find_set(int x)
{
	if (x != pa[x])
		pa[x] = find_set(pa[x]);
	return pa[x];
}

void union_set(int x, int y)
{
	x = find_set(x);
	y = find_set(y);
	if (x == y) return;
	if (ranks[x] > ranks[y])
		pa[y] = x;
	else
	{
		pa[x] = y;
		if (ranks[x] == ranks[y])
			ranks[y]++;
	}
}

int main()
{
	int n, m, i, x, y, count = 0, ans;
	while (cin >> n >> m)
	{
		if (n == 0 && m == 0) break;
		memset(visited, false, sizeof(visited));
		ans = 0;
		s.clear();
		count++;
		for (i = 1; i <= n; i++)
			make_set(i);
		for (i = 1; i <= m; i++)
		{
			cin >> x >> y;
			if (!visited[x])
				visited[x] = true;
			if (!visited[y])
				visited[y] = true;
			union_set(x, y);
		}
		for (i = 1; i <= n; i++)
		{
			if (!visited[i])
				ans++;//没有访问过的元素作为一种信仰!
			if (visited[i])
			    s.insert(find_set(i));//统计树的个数!
		}
		ans += s.size();
		cout << "Case " << count << ": " << ans << endl;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值