【图论05】并查集 1006 Zjnu Stadium

本文详细介绍了并查集算法的应用及实现方式,特别是在路径压缩和权值动态更新方面的技巧。通过具体实例展示了如何使用并查集解决图论问题,并对比了不同输入方式对程序效率的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

算法思路:并查集。

刚开始没想明白该怎么保存权值也就是距离dis,后来看了大牛的代码恍然大悟,其思想还是在findset函数里进行 “路径压缩 + 权值动态更新” 。  关键还是要找准在什么位置进行“权值的更新”比较好(下面代码中有两处更新权值的地方)。

说明:dis刚开始保存的是到父节点的距离,执行过findset之后,路径上的结点的dis变成了到根节点的距离。还有由于进行了路劲压缩所有这一句代码 "dis[a1] = (x + dis[b] - dis[a] + 300) % 300;" 是没有问题的,只要更新 a1 的dis就行了。

下面有两份代码,其区别只是改了一下输入语句,可以看到用scanf节省的时间开销还是很可观的!!!


515msAC代码:


//模板开始
#include <string>   
#include <vector>   
#include <algorithm>   
#include <iostream>   
#include <sstream>   
#include <fstream>   
#include <map>   
#include <set>   
#include <cstdio>   
#include <cmath>   
#include <cstdlib>   
#include <ctime>
#include<iomanip>
#include<string.h>
#define SZ(x) (int(x.size()))
using namespace std;

int toInt(string s){
	istringstream sin(s); 
	int t; 
	sin>>t; 
	return t;
}
template<class T> string toString(T x){
	ostringstream sout; 
	sout<<x; 
	return sout.str();
}
typedef long long int64;
int64 toInt64(string s){
	istringstream sin(s); 
	int64 t; 
	sin>>t;
	return t;
}
template<class T> T gcd(T a, T b){ 
	if(a<0) 
		return gcd(-a, b);
	if(b<0) 
		return gcd(a, -b);
	return (b == 0)? a : gcd(b, a % b);
}
//模板结束(通用部分)

#define ifs cin


#define MAX_SIZE 50005
int next_node[MAX_SIZE];		//存储边
//int flag[MAX_SIZE];		//标记节点是否存在
//int in[MAX_SIZE];		//存储节点的入度
//int out[MAX_SIZE];		//存储节点的出度
int dis[MAX_SIZE];		//节点的权值

int m;

void init()		//初始化
{
	for(int i = 0; i < m; i++)
	{
		next_node[i] = i;
		dis[i] = 0;
	}
	//memset(in, 0, sizeof(in));
	//memset(out, 0, sizeof(out));
	//memset(flag, 0, sizeof(flag));
}

int findset(int a)		//找元素所在集合的代表元(因为用了路径压缩,路径压缩的主要目的是为了尽快的确定元素所在的集合)
{
	if(a == next_node[a])
	{
		return next_node[a];
	}
	else
	{
		int t = next_node[a];
		next_node[a] = findset(next_node[a]);		//路径压缩(递归)
		dis[a] = (dis[a] + dis[t]) % 300;		//更新dis
		return next_node[a];
	}
}

int union_nodes(int a, int b, int x)		//集合合并
{
	int a1 = findset(a);
	int b1 = findset(b);
	if(a1 == b1)
	{
		if((dis[b] + x) % 300 == dis[a])
		{
			return 0;
		}
		else
		{
			return 1;
		}
	}
	else
	{
		next_node[a1] = b1;
		dis[a1] = (x + dis[b] - dis[a] + 300) % 300;
		return 0;
	}
}

//【图论05】并查集 1006 Zjnu Stadium

int main()
{
	//ifstream ifs("shuju.txt", ios::in);
	int n;
	int a, b, x;
	while(ifs>>m>>n)
	{
		init();
		int count = 0;
		for(int i = 0; i < n; i++)
		{
			ifs>>a>>b>>x;
			count += union_nodes(a, b, x);
		}
		cout<<count<<endl;
	}

	return 0;
}


140msAC代码:

//模板开始
#include <string>   
#include <vector>   
#include <algorithm>   
#include <iostream>   
#include <sstream>   
#include <fstream>   
#include <map>   
#include <set>   
#include <cstdio>   
#include <cmath>   
#include <cstdlib>   
#include <ctime>
#include<iomanip>
#include<string.h>
#define SZ(x) (int(x.size()))
using namespace std;

int toInt(string s){
	istringstream sin(s); 
	int t; 
	sin>>t; 
	return t;
}
template<class T> string toString(T x){
	ostringstream sout; 
	sout<<x; 
	return sout.str();
}
typedef long long int64;
int64 toInt64(string s){
	istringstream sin(s); 
	int64 t; 
	sin>>t;
	return t;
}
template<class T> T gcd(T a, T b){ 
	if(a<0) 
		return gcd(-a, b);
	if(b<0) 
		return gcd(a, -b);
	return (b == 0)? a : gcd(b, a % b);
}
//模板结束(通用部分)

#define ifs cin


#define MAX_SIZE 50005
int next_node[MAX_SIZE];		//存储边
//int flag[MAX_SIZE];		//标记节点是否存在
//int in[MAX_SIZE];		//存储节点的入度
//int out[MAX_SIZE];		//存储节点的出度
int dis[MAX_SIZE];		//节点的权值

int m;

void init()		//初始化
{
	for(int i = 0; i < m; i++)
	{
		next_node[i] = i;
		dis[i] = 0;
	}
	//memset(in, 0, sizeof(in));
	//memset(out, 0, sizeof(out));
	//memset(flag, 0, sizeof(flag));
}

int findset(int a)		//找元素所在集合的代表元(因为用了路径压缩,路径压缩的主要目的是为了尽快的确定元素所在的集合)
{
	if(a == next_node[a])
	{
		return next_node[a];
	}
	else
	{
		int t = next_node[a];
		next_node[a] = findset(next_node[a]);		//路径压缩(递归)
		dis[a] = (dis[a] + dis[t]) % 300;		//更新dis
		return next_node[a];
	}
}

int union_nodes(int a, int b, int x)		//集合合并
{
	int a1 = findset(a);
	int b1 = findset(b);
	if(a1 == b1)
	{
		if((dis[b] + x) % 300 == dis[a])
		{
			return 0;
		}
		else
		{
			return 1;
		}
	}
	else
	{
		next_node[a1] = b1;
		dis[a1] = (x + dis[b] - dis[a] + 300) % 300;
		return 0;
	}
}

//【图论05】并查集 1006 Zjnu Stadium

int main()
{
	//ifstream ifs("shuju.txt", ios::in);
	int n;
	int a, b, x;
	while(ifs>>m>>n)
	{
		init();
		int count = 0;
		for(int i = 0; i < n; i++)
		{
			scanf("%d%d%d", &a, &b, &x);
			count += union_nodes(a, b, x);
		}
		cout<<count<<endl;
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值