hdu 1679 The Unique MST (克鲁斯卡尔)

探讨通过克鲁斯卡尔算法解决最小生成树是否唯一的问题,介绍如何判断图中是否存在等效边。

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

The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 24152 Accepted: 8587

Description

Given a connected undirected graph, tell if its minimum spanning tree is unique.

Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the following properties:
1. V' = V.
2. T is connected and acyclic.

Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E') of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E'.

Input

The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.

Output

For each input, if the MST is unique, print the total cost of it, or otherwise print the string 'Not Unique!'.

Sample Input

2
3 3
1 2 1
2 3 2
3 1 3
4 4
1 2 2
2 3 2
3 4 2
4 1 2

Sample Output

3
Not Unique!

题目链接:http://poj.org/problem?

id=1679

题目大意:n个点m条路。给出每条路以及边权。推断最小生成树是否是唯一的。

解题思路:克鲁斯卡尔,推断是否存在等效边。这题数据太弱了。我推断等效边的方法不太对,竟然过了= =

代码例如以下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int fa[102];
struct EG
{
	int u,v,w;
}eg[5005];
void get_fa()
{
	for(int i=0;i<105;i++)
		fa[i]=i;
}
int find (int x)
{
	return x==fa[x]?x:fa[x]=find(fa[x]);
}
void Union(int a,int b)
{
	int a1=find(a);
	int b1=find(b);
	if(a1!=b1)
		fa[a1]=b1;
}
int cmp(EG a,EG b)
{
	return a.w<b.w;
}
int main(void)
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,m,ans=0,p=0,cnt=0;
		get_fa();
		scanf("%d%d",&n,&m);
		for(int i=0;i<m;i++)
		{
			scanf("%d%d%d",&eg[i].u,&eg[i].v,&eg[i].w);
		}
		sort(eg,eg+m,cmp);
		for(int i=0;i<m;i++)
		{
			if(find(eg[i].u)!=find(eg[i].v))//假设当前边须要增加且下一条边也须要增加且它们权值相等即为等效边
			{
				if(i+1<m&&find(eg[i+1].u)!=find(eg[i+1].v)&&eg[i].w==eg[i+1].w)
				{
					p=1;
					break;
				}
				Union(eg[i].u,eg[i].v);
				ans+=eg[i].w;
				cnt++;
			}
			if(cnt>=n)
				break;
		}
		if(!p)
			printf("%d\n",ans );
		else
			printf("Not Unique!\n");
	}
}


基于开源大模型的教学实训智能体软件,帮助教师生成课前备课设计、课后检测问答,提升效率与效果,提供学生全时在线练习与指导,实现教学相长。 智能教学辅助系统 这是一个智能教学辅助系统的前端项目,基于 Vue3+TypeScript 开发,使用 Ant Design Vue 作为 UI 组件库。 功能模块 用户模块 登录/注册功能,支持学生和教师角色 毛玻璃效果的登录界面 教师模块 备课与设计:根据课程大纲自动设计教学内容 考核内容生成:自动生成多样化考核题目及参考答案 学情数据分析:自动化检测学生答案,提供数据分析 学生模块 在线学习助手:结合教学内容解答问题 实时练习评测助手:生成随练题目并纠错 管理模块 用户管理:管理员/教师/学生等用户基本管理 课件资源管理:按学科列表管理教师备课资源 大屏概览:使用统计、效率指数、学习效果等 技术栈 Vue3 TypeScript Pinia 状态管理 Ant Design Vue 组件库 Axios 请求库 ByteMD 编辑器 ECharts 图表库 Monaco 编辑器 双主题支持(专业科技风/暗黑风) 开发指南 # 安装依赖 npm install # 启动开发服务器 npm run dev # 构建生产版本 npm run build 简介 本项目旨在开发一个基于开源大模型的教学实训智能体软件,帮助教师生成课前备课设计、课后检测问答,提升效率与效果,提供学生全时在线练习与指导,实现教学相长。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值