[备战NOI同步赛]适合ACM-ICPC的并查集模板

并查集模板使用时需要注意一下,不同的题目对并查集的要求不同,某些题目的并查集可能需要携带附加信息,这时需要修改模板的并查集初始化和合并内容,而本并查集模板只支持对并查集结点的父节点、子树深度、子树元素个数进行初始化、合并、查找工作!

/*
并查集模板
By:qpswwww(ZYK)
包含findSet(),makeSet(),Union(),使用路径压缩式查找
注:不同的题目对合并的要求不同,并查集也可能携带附加信息
*/

#define MAXN 30050

int f[MAXN],depth[MAXN],num[MAXN]; //f[i]=点i的父节点,depth[i]=点i的深度,num[i]=i结点对应下面的元素个数

void makeSet(int i) //并查集初始化
{
	f[i]=i;
	num[i]=1;
	depth[i]=1;
}

int findSet(int x) //带路径压缩的并查集查找
{
	if(f[x]==x) return x;
	return f[x]=findSet(f[x]);
}

void Union(int a,int b) //将a、b合并,有深度优化
{
	if(a==b) return;
	if(depth[a]>depth[b]) {f[b]=a; num[a]+=num[b];}
	else if(depth[a]<depth[b]) {f[a]=b; num[b]+=num[a];}
	else {f[a]=b; depth[b]=depth[b]+1; num[b]+=num[a];}
}

例题1: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.

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.

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

Source

 

题目大意为有多组输入数据,对于每组数据,有n个元素及m组元素,每一组元素对应一个集合,一个元素可存在于多个集合中,与邻元素A在同一集合的所有元素均为A的邻元素,0为最初的邻元素,求每组数据中邻元素的个数

题目可以直接套用上面的目标,下面是代码

/*
并查集模板
By:qpswwww(ZYK)
包含findSet(),makeSet(),Union(),使用路径压缩式查找
注:不同的题目对合并的要求不同,并查集也可能携带附加信息
*/
#include <stdio.h>

#define MAXN 30050

int f[MAXN],depth[MAXN],num[MAXN]; //f[i]=点i的父节点,depth[i]=点i的深度,num[i]=i结点对应下面的元素个数

void makeSet(int i) //并查集初始化
{
	f[i]=i;
	num[i]=1;
	depth[i]=1;
}

int findSet(int x) //带路径压缩的并查集查找
{
	if(f[x]==x) return x;
	return f[x]=findSet(f[x]);
}

void Union(int a,int b) //将a、b合并,有深度优化
{
	if(a==b) return;
	if(depth[a]>depth[b]) {f[b]=a; num[a]+=num[b];}
	else if(depth[a]<depth[b]) {f[a]=b; num[b]+=num[a];}
	else {f[a]=b; depth[b]=depth[b]+1; num[b]+=num[a];}
}

int main()
{
	int i,j,k,n,m,a,b,roota,rootb;
	while(1)
	{
		for(i=0;i<MAXN;i++) makeSet(i);
		scanf("%d%d",&n,&m);
		if(n==0&&m==0) break;
		for(i=1;i<=m;i++)
		{
			scanf("%d%d",&k,&a);
			for(j=1;j<k;j++)
			{
				scanf("%d",&b);
				{
					roota=findSet(a);
					rootb=findSet(b);
					Union(roota,rootb); //小组里的人都合并在一个集合里
				}
			}
		}
		roota=findSet(0);
		printf("%d\n",num[roota]);
	}
	return 0;
}

例题2:POJ 2524 Ubiquitous Religions

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 finding out how many different religions students in your university believe in.

You know that there are n students in your university (0 < n <= 50000). It is infeasible for you to ask every student their religious beliefs. Furthermore, many students are not comfortable expressing their beliefs. One way to avoid these problems is to ask m (0 <= m <= n(n-1)/2) pairs of students and ask them whether they believe in the same religion (e.g. they may know if they both attend the same church). From this data, you may not know what each person believes in, but you can get an idea of the upper bound of how many different religions can be possibly represented on campus. You may assume that each student subscribes to at most one religion.

Input

The input consists of a number of cases. Each case starts with a line specifying the integers n and m. The next m lines each consists of two integers i and j, specifying that students i and j believe in the same religion. The students are numbered 1 to n. The end of input is specified by a line in which n = m = 0.

Output

For each test case, print on a single line the case number (starting with 1) followed by the maximum number of different religions that the students in the university believe in.

Sample Input

10 9
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10 4
2 3
4 5
4 8
5 8
0 0

Sample Output

Case 1: 1
Case 2: 7

Hint

Huge input, scanf is recommended.

Source


题目大意是给出多组数据,对于每组数据,给出n,m,即n个元素,后面接m行,每行有两个数a,b,a和b在同一集合内,求互不相同的集合个数 

 先将每一对元素合并,所有元素合并完成后,遍历一遍n个元素,找出先根节点的个数,即不同的集合个数

#include <stdio.h>

#define MAXN 50050

int f[MAXN],depth[MAXN],num[MAXN]; //f[i]=点i的父节点,depth[i]=点i的深度,num[i]=i结点对应下面的元素个数

void makeSet(int i) //并查集初始化
{
	f[i]=i;
	num[i]=1;
	depth[i]=1;
}

int findSet(int x) //带路径压缩的并查集查找
{
	if(f[x]==x) return x;
	return f[x]=findSet(f[x]);
}

void Union(int a,int b) //将a、b合并,有深度优化
{
	if(a==b) return;
	if(depth[a]>depth[b]) {f[b]=a; num[a]+=num[b];}
	else if(depth[a]<depth[b]) {f[a]=b; num[b]+=num[a];}
	else {f[a]=b; depth[b]=depth[b]+1; num[b]+=num[a];}
}

int main()
{
	int i,j,n,m,a,b,tot=0,sum,roota,rootb;
	while(++tot)
	{
		sum=0;
		for(i=0;i<MAXN;i++) makeSet(i);
		scanf("%d%d",&n,&m);
		if(n==0&&m==0) break;
		for(i=1;i<=m;i++)
		{
			scanf("%d%d",&a,&b);
			roota=findSet(a);
			rootb=findSet(b);
			Union(roota,rootb);
		}
		for(i=1;i<=n;i++)
			if(findSet(i)==i) sum++; //统计不同集合(根节点)个数
		printf("Case %d: %d\n",tot,sum);
	}
	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1 图论 3 1.1 术语 3 1.2 独立集、覆盖集、支配集之间关系 3 1.3 DFS 4 1.3.1 割顶 6 1.3.2 桥 7 1.3.3 强连通分量 7 1.4 最小点基 7 1.5 拓扑排序 7 1.6 欧拉路 8 1.7 哈密顿路(正确?) 9 1.8 Bellman-ford 9 1.9 差分约束系统(用bellman-ford解) 10 1.10 dag最短路径 10 1.11 二分图匹配 11 1.11.1 匈牙利算法 11 1.11.2 KM算法 12 1.12 网络流 15 1.12.1 最大流 15 1.12.2 上下界的网络的最大流 17 1.12.3 上下界的网络的最小流 17 1.12.4 最小费用最大流 18 1.12.5 上下界的网络的最小费用最小流 21 2 数论 21 2.1 最大公约数gcd 21 2.2 最小公倍数lcm 22 2.3 快速幂取模B^LmodP(O(logb)) 22 2.4 Fermat小定理 22 2.5 Rabin-Miller伪素数测试 22 2.6 Pollard-rho 22 2.7 扩展欧几里德算法extended-gcd 24 2.8 欧拉定理 24 2.9 线性同余方程ax≡b(mod n) 24 2.10 中国剩余定理 25 2.11 Discrete Logging(BL == N (mod P)) 26 2.12 N!最后一个不为0的数字 27 2.13 2^14以内的素数 27 3 数据结构 31 3.1 堆(最小堆) 31 3.1.1 删除最小值元素: 31 3.1.2 插入元素和向上调整: 32 3.1.3 堆的建立 32 3.2 并查集 32 3.3 树状数组 33 3.3.1 LOWBIT 33 3.3.2 修改a[p] 33 3.3.3 前缀和A[1]+…+A[p] 34 3.3.4 一个二维树状数组的程序 34 3.4 线段树 35 3.5 字符串 38 3.5.1 字符串哈希 38 3.5.2 KMP算法 40 4 计算几何 41 4.1 直线交点 41 4.2 判断线段相交 41 4.3 三点外接圆圆心 42 4.4 判断点在多边形内 43 4.5 两圆交面积 43 4.6 最小包围圆 44 4.7 经纬度坐标 46 4.8 凸包 46 5 Problem 48 5.1 RMQ-LCA 48 5.1.1 Range Minimum Query(RMQ) 49 5.1.2 Lowest Common Ancestor (LCA) 53 5.1.3 Reduction from LCA to RMQ 56 5.1.4 From RMQ to LCA 57 5.1.5 An algorithm for the restricted RMQ 60 5.1.6 An AC programme 61 5.2 最长公共子序列LCS 64 5.3 最长上升子序列/最长不下降子序列(LIS) 65 5.3.1 O(n^2) 65 5.3.2 O(nlogn) 66 5.4 Joseph问题 67 5.5 0/1背包问题 68 6 组合数学相关 69 6.1 The Number of the Same BST 69 6.2 排列生成 71 6.3 逆序 72 6.3.1 归并排序求逆序 72 7 数值分析 72 7.1 二分法 72 7.2 迭代法(x=f(x)) 73 7.3 牛顿迭代 74 7.4 数值积分 74 7.5 高斯消元 75 8 其它 77
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值