CF lesson 并查集

step2

求最右

支持两种操作:

  1. 删除节点操作
  2. 询问某一节点右侧没有被删除的第一个节点。
//直线
const int N = 1e6 + 10;
int f[N];
int findf(int x)
{
	if(f[x] == x)	return x;
	f[x] = findf(f[x]);
	return f[x];
}
int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; i++)	f[i] = i;
	f[n + 1] = n + 1;
	for(int i = 1; i <= m; i++)
	{
		char opt;
		int x;
		scanf("\n%c%d", &opt, &x);
		if(opt == '-')
			if(f[x] == x)
				f[x] = x + 1; 
		else
		{
			int y = findf(x);
			if(y == n + 1)	printf("-1\n");
			else	printf("%d\n", y);
		}
	 } 
	return 0;
 } 
//环形
int f[N];
int findf(int x)
{
	if(f[x] == x)	return x;
	f[x] = findf(f[x]);
	return f[x];
}
void dsu(int x, int y)
{
	int fx = findf(x);
	int fy = findf(y);
	if(fx!= fy)	f[fx] = fy;
}
int main()
{
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++)	f[i] = i;
	f[n + 1] = 1;
	for(int i = 1; i <= n; i++)
	{
		int x;
		scanf("%d", &x);
		int y = findf(x);
		printf("%d ",y);
		dsu(y, y + 1);
	}
	return 0;
 } 

区间合并

支持三种操作:

  1. 合并两点
  2. 合并一段区间
  3. 询问两点是否在同一集合种
const int N = 2e5 + 10;
int f[N], nxt[N];
int findf(int x)
{
	if(x == f[x])	return x;
	f[x] = findf(f[x]);
	return f[x];
}
int join(int x, int y)
{
	int fx = findf(x);
	int fy = findf(y);
	if(fx != fy)	f[fy] = fx;
}
int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; i++)
		f[i] = i, nxt[i] = i + 1;
	for(int i = 1; i <= m; i++)
	{
		int opt, x, y;
		scanf("%d%d%d", &opt, &x, &y);
		if(opt == 1)	join(x, y);
		if(opt == 2)
		{
			int tar;
			for(int i = x + 1; i <= y; i = tar)
			{
				join(x, i);
				tar = nxt[i];
				nxt[i] = nxt[y];
			}
		}
		if(opt == 3)
		{
			int fx = findf(x);
			int fy = findf(y);
			if(fx == fy)	printf("YES\n");
			else	printf("NO\n");
		}
	}
	return 0;
}

带权并查集

支持两种操作:

  1. boss A 变成boss B的下级
  2. 询问x有多少层上级
int f[N], r[N];
int findf(int x)
{
	if(x == f[x])	return f[x];
	int p = f[x];
	f[x] = findf(f[x]);
	r[x] += r[p];
	return f[x];
}
int join(int x, int y)
{
	f[x] = y;
	r[x] = 1;
}
int main()
{
	int n, m, opt, x, y;
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; i++)
		f[i] = i, r[i] = 0;
	for(int i = 1; i <= m; i++)
	{
		scanf("%d", &opt);
		if(opt == 1)
		{
			scanf("%d%d", &x, &y);
			join(x, y);
		}
		else
		{
			scanf("%d", &x);
			int fx = findf(x);
			printf("%d\n", r[x]);
		}
	}
	return 0;
}

最小生成树

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
const int M = 2e5 + 10;
const int inf = 2e9 + 1;
struct EDGE{
	int x, y, v;
	bool operator < (const EDGE & other)
	const { return v < other.v;}
}E[M];
int f[N];
int findf(int x)
{
	if(f[x] == x)	return x;
	f[x] = findf(f[x]);
	return f[x];
}
int main()
{
	int n, m;
	LL ans = 0;
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= m; i++)	scanf("%d%d%d", &E[i].x, &E[i].y, &E[i].v);
	for(int i = 1; i <= n; i++)	f[i] = i;
	sort(E + 1, E + m + 1);
	int cnt = n; 
	for(int i = 1; i <= m; i++)
	{
		int fx = findf(E[i].x);
		int fy = findf(E[i].y);
		if(fx != fy)
		{
			f[fx] = fy;
			cnt--;
			ans += E[i].v;
		}
		if(cnt == 1)	break;
	} 
	cout<<ans<<endl;
	return 0;
}

边权差最小的生成树

边权最大值与最小值的差最小

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int N = 1e3 + 10;
const int M = 1e4 + 10;
const int inf = 2e9 + 1;
struct EDGE{
	int x, y, v;
	bool operator < (const EDGE & other)
	const { return v < other.v;}
}E[M];
int f[N];
int findf(int x)
{
	if(f[x] == x)	return x;
	f[x] = findf(f[x]);
	return f[x];
}
int main()
{
	int n, m;
	int ans = inf;
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= m; i++)	scanf("%d%d%d", &E[i].x, &E[i].y, &E[i].v);
	sort(E + 1, E + m + 1);
	for(int i = 1; i <= m; i++)
	{
		for(int j = 1; j <= n; j++)	f[j] = j;
		int cnt = n;
		for(int j = i; j <= m; j++)
		{
			int fx = findf(E[j].x);
			int fy = findf(E[j].y);
			if(fx != fy)
			{
				f[fx] = fy;
				cnt--;
			}
			if(cnt == 1)
			{
				ans = min(ans, E[j].v - E[i].v);
				break;
			}
		}
	} 
	if(ans < inf)	printf("YES\n%d\n",ans);
	else	printf("NO\n");
	return 0;
}

最大值最小生成树

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
const int M = 4e5 + 10;
const int inf = 2e9 + 1;
struct EDGE{
	int x, y, v;
	bool operator < (const EDGE & other)
	const { return v < other.v;}
}E[M];
int f[N];
int findf(int x)
{
	if(f[x] == x)	return x;
	f[x] = findf(f[x]);
	return f[x];
}
int main()
{
	int n, m;
	int ans = 0;
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= m; i++)	scanf("%d%d%d", &E[i].x, &E[i].y, &E[i].v);
	for(int i = 1; i <= n; i++)	f[i] = i;
	sort(E + 1, E + m + 1);
	int cnt = n; 
	for(int i = 1; i <= m; i++)
	{
		int fx = findf(E[i].x);
		int fy = findf(E[i].y);
		if(fx != fy)
		{
			f[fx] = fy;
			cnt--;
		}
		if(cnt == 1)
		{
			ans = E[i].v;
			break;
		}
	} 
	cout<<ans<<endl;
	return 0;
}

删除数量尽可能多的边,使得图依旧联通且删去的边权和不大于s

构造最大生成树,再从小到大加入边直到剩余的边的边权和小于等于s。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
typedef long long LL;
const int N = 5e4 + 10;
const int M = 1e5 + 10;
vector <int> vec;
struct EDGE{
	int x, y, v, id;
	bool operator <(const EDGE & other)
	const {return v > other.v || (v == other.v && id > other.id); }
}E[M];
int f[N], vist[M];
int findf(int x)
{
	if(f[x] == x)	return x;
	f[x] = findf(f[x]);
	return f[x];
}
int main()
{
	int n, m;
	LL s, sum = 0, now = 0;
	scanf("%d%d", &n, &m);
	cin>>s;
	for(int i = 1; i <= n; i++)	f[i] = i;
	for(int i = 1; i <= m; i++)
	{
		scanf("%d%d%d", &E[i].x, &E[i].y, &E[i].v);
		E[i].id = i;
		sum = sum + E[i].v;
	}
	sort(E + 1, E + m + 1);
	int cnt = n;
	for(int i = 1; i <= m; i++)
	{
		if(cnt == 1)	break;
		int fx = findf(E[i].x);
		int fy = findf(E[i].y);
		if(fx != fy)
		{
			f[fx] = fy;
			cnt--;
			vist[i] = 1;
			now += E[i].v;
		}
	}
	for(int i = 1; i <= m; i++)
	{
		if(now >= sum - s)	break;
		if(!vist[i])
		{
			vist[i] = 1;
			now += E[i].v;
		}
	}
	for(int i = 1; i<= m; i++)
		if(!vist[i])
			vec.push_back(E[i].id);
	printf("%d\n", vec.size());
	sort(vec.begin(), vec.end());
	for(int i = 0; i < vec.size(); i++)
		printf("%d ",vec[i]);
	printf("\n");
	return 0;
}

黑白染色(带权并查集)

一条边连接的两个点颜色不同,判断两点是否颜色相同。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
const int N = 2e5 + 10;
int f[N], r[N];
int findf(int x)
{
	if(f[x] == x)	return x;
	int p = f[x];
	f[x] = findf(f[x]);
	r[x] = (r[p] + r[x]) % 2;
	return f[x];
}
void join(int x, int y)
{
	int fx = findf(x);
	int fy = findf(y);
	if(fx != fy)
	{
		f[fx] = fy;
		r[fx] = (r[x] + r[y] + 1) % 2;
	} 
}
int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; i++)
		f[i] = i;
	int shift = 0;
	for(int i = 1; i <= m; i++)
	{
		int opt, x, y;
		scanf("%d%d%d", &opt, &x, &y);
		x = (x + shift) % n;
		y = (y + shift) % n;
		x++;	y++;
		if(opt == 0)
			join(x, y);
		else
		{
			int fx = findf(x);
			int fy = findf(y);
			if(r[x] == r[y])
			{
				shift = (shift + 1) % n;
				printf("YES\n");
			}
			else
				printf("NO\n");
		}
	}
	return 0;
 } 
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = 3e5 + 10;
int f[N], r[N];
int findf(int x)
{
	if(f[x] == x)	return x;
	int p = f[x];
	f[x] = findf(f[x]);
	r[x] = (r[p] + r[x]) % 2;
	return f[x];
}
int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; i++)	f[i] = i;
	for(int i = 1; i <= m; i++)
	{
		int x, y;
		scanf("%d%d", &x, &y);
		int fx = findf(x);
		int fy = findf(y);
		if(fx != fy)
		{
			f[fx] = fy;
			r[fx] = (r[x] + r[y] + 1) % 2;
		}
		else
		{
			if(r[x] == r[y])
			{
				printf("%d\n", i);
				return 0;; 
			} 
		}
	}
	printf("-1\n");
	return 0;
}
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 、5资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值