HDU - 1213 How Many Tables & POJ - 1611 The Suspects(并查集)

并查集 - HDU1213 & POJ1611

HDU - 1213 How Many Table

Problem Description
Today is Ignatius’ birthday. He invites a lot of friends. Now it’s dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.
One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.
For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.
今天是Ignatius的生日。他邀请了很多朋友。现在是晚饭时间。Ignatius想知道他至少需要多少张桌子。您必须注意,并不是所有的朋友都彼此认识,并且所有的朋友都不想和陌生人呆在一起。
解决此问题的一条重要规则是,如果我告诉您A认识B,而B认识C,则意味着A,B,C彼此认识,因此它们可以呆在一张桌子上。

Input
The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.
输入以整数T(1 <= T <= 25)开头,该整数表示测试用例的数量。然后是T测试用例。每个测试用例均以两个整数N和M(1 <= N,M <= 1000)开头。N表示朋友的数量,朋友从1到N标记。然后跟随M行。每行包含两个整数A和B(A != B),这意味着朋友A和朋友B彼此认识。两种情况之间将有一个空白行。

Output
For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
对于每个测试用例,只需输出Ignatius至少需要多少张桌子。请勿打印任何空白。

Sample Input
2
5 3
1 2
2 3
4 5

5 1
2 5

Sample Output
2
4

思路:
并查集。把认识的人都并到一组,最后统计有多少组即可,也就是 f [ i ] = = i f[i]==i f[i]==i 的个数。

Code:

#include <cstdio>
using namespace std;
int f[1005];
void init(int n)
{
	for (int i = 0; i <= n; i++)
		f[i] = i;
}
int getf(int x)
{
	if (f[x] != x)
		f[x] = getf(f[x]);
	return f[x];
}
void Merge(int x, int y)
{
	int fx = getf(x);
	int fy = getf(y);
	if (fx != fy)
		f[fy] = fx;
}
int main()
{
	int T;
	scanf("%d", &T);
	while (T--)
	{
		int n, m, a, b;
		scanf("%d%d", &n, &m);
		init(n);
		for (int i = 1; i <= m; i++)
		{
			scanf("%d%d", &a, &b);
			Merge(a, b);
		}
		int cnt = 0;
		for (int i = 1; i <= n; i++)
			if (f[i] == i)
				cnt++;
		printf("%d\n", cnt);
	}
	return 0;
}

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 the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).
Once a member in a group is a suspect, all members in the group are suspects.
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.
严重急性呼吸道综合症(SARS)是一种病因不明的非典型肺炎,在2003年3月中旬被认为是全球性威胁。为了最大程度地减少向他人的传播,最好的策略是将嫌疑犯与其他人分开。
在不蔓延疾病大学(NSYSU)中,有很多学生团体。同一组中的学生经常互相交流,一个学生可以加入多个组。为了防止可能的SARS传播,NSYSU收集所有学生团体的成员列表,并在其标准操作程序(SOP)中制定以下规则。
一旦组中的某个成员成为可疑对象,该组中的所有成员都将成为可疑对象。
但是,他们发现,当一个学生被确认为犯罪嫌疑人时,要识别所有犯罪嫌疑人并不容易。您的工作是编写一个找到所有嫌疑犯的程序。

Input
The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
输入文件包含几种情况。每个测试用例都以一行中的两个整数n和m开头,其中n是学生数,m是组数。您可以假设0 <n <= 30000并且0 <= m <=500。每个学生都用0到n-1之间的唯一整数编号,并且最初在所有情况下,学生0都被视为犯罪嫌疑人。该行之后是组的m个成员列表,每组一行。每行以一个整数k开头,该整数k代表组中成员的数量。在成员数之后,有k个整数表示该组中的学生。一行中的所有整数至少间隔一个空格。
n = 0和m = 0的情况表示输入的结尾,无需处理。

Output
For each case, output the number of suspects in one line.
对于每种情况,在一行中输出可疑事件的数量。

Sample Input
100 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
200 2
1 5
5 1 2 3 4 5
1 0
0 0
Sample Output
4
1
1

思路:
并查集。把 m 组成员分别并一个集合中,最后统计 0 所在集合的人数。这里最后要进行一次路径压缩,如果不压缩,可能导致某一个数的祖先不是它的最终祖先。这个题中我合并(merge)时是按照 小的数 是 大的数 的祖先,所以最后 0 所在的集合中祖先肯定是 0。

Code:

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
int f[maxn];
void init(int n)
{
	for (int i = 0; i < n; i++)
		f[i] = i;
}
int findd(int x)
{
	if (f[x] != x)
		f[x] = findd(f[x]);
	return f[x];
}
void mergee(int x, int y)
{
	int xx = findd(x);
	int yy = findd(y);
	if (xx != yy)
	{
		if (xx > yy)
			swap(xx, yy);
		f[yy] = xx;
	}
}
int main()
{
	int n, m;
	while (cin >> n >> m)
	{
		if (!n && !m)
			break;
		init(n);
		int k, x;
		while (m--)
		{
			cin >> k;
			vector<int> v;
			for (int i = 0; i < k; i++)
				cin >> x, v.push_back(x);
			for (int i = 1; i < k; i++)
				mergee(v[i - 1], v[i]);
		}
		int ans = 0;
		for (int i = 0; i < n; i++)
			if (findd(i) == 0) //这里就边压缩边判断,也可以在前面单独一个for循环来压缩,压缩路径就是找一遍祖先就OK了。
				ans++;
		cout << ans << "\n";
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值