POJ 1611 The Suspects 并查集 Union Find

本题也是个标准的并查集题解。

操作完并查集之后,就是要找和0节点在同一个集合的元素有多少。

注意这个操作,须要先找到0的父母节点。然后查找有多少个节点的额父母节点和0的父母节点同样。

这个时候须要对每一个节点使用find parent操作。由于最后状态的时候,节点的parent不一定是本集合的根节点。

#include <stdio.h>

const int MAX_N = 30001;
struct SubSet
{
	int p, rank;
}sub[MAX_N];

int N, M;

void initSub()
{
	for (int i = 0; i < N; i++)
	{
		sub[i].p = i;
		sub[i].rank = 0;
	}
}

int find(int x)
{
	if (x != sub[x].p) sub[x].p = find(sub[x].p);
	return sub[x].p;
}

void unionTwo(int x, int y)
{
	int xroot = find(x);
	int yroot = find(y);
	if (sub[xroot].rank < sub[yroot].rank) sub[xroot].p = yroot;
	else
	{
		if (sub[xroot].rank == sub[yroot].rank) sub[xroot].rank++;
		sub[yroot].p = xroot;
	}
}

int main()
{
	int a, b, k;
	while (scanf("%d %d", &N, &M) && (N || M))
	{
		initSub();
		for (int i = 0; i < M; i++)
		{
			scanf("%d", &k);
			if (k > 0) scanf("%d", &a);
			for (int j = 1; j < k; j++)
			{
				scanf("%d", &b);
				unionTwo(a, b);
			}
		}
		int sus = 1, p = find(0);
		for (int i = 1; i < N; i++)
		{
			if (find(i) == p) sus++;
		}
		printf("%d\n", sus);
	}
	return 0;
}
时间: 2024-08-07 19:21:52

POJ 1611 The Suspects 并查集 Union Find的相关文章

[ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21586   Accepted: 10456 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. T

POJ 1611 The Suspects (并查集+数组记录子孙个数 )

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 24134   Accepted: 11787 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. T

POJ 1611 The Suspects (并查集求数量)

Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others. In t

POJ 1611 The Suspects (并查集)

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 25130   Accepted: 12313 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. T

POJ 1611 The Suspects(并查集)

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 22296   Accepted: 10842 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. T

POJ 1611 The Suspects(特别无语的并查集)

很简单的一道题目,开始用的是并查集的分离集合森林做,不知道怎么的特别不稳定,太奇怪了,WA无数次,无奈之下改成一维数组了..sad AC #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <math.h> #define PI acos(-1,0) using namespa

poj 1611:The Suspects(并查集,经典题)

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21472   Accepted: 10393 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. T

poj 1182:食物链(并查集,食物链问题)

食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44168   Accepted: 12878 Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用两种说法对这N个动物所构成的食物链关系进行描述: 第一种说法是"1 X Y",表示X和Y是同

POJ 2524 Ubiquitous Religions (幷查集)

Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23090   Accepted: 11378 Description There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in findi