POJ-1679 The Unique MST

本文介绍了一种判断图的最小生成树是否唯一的算法,并通过Kruskal算法实现。该算法首先找到一个最小生成树(MST),记录其总权重,并尝试移除MST中的每条边来检查是否有相同权重的替代MST。

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

The Unique MST
Time Limit: 1000MS Memory Limit: 10000K

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

————————————————————集训10.1的分割线————————————————————

前言:诶。今天被模拟网络赛了,蓝皮书只做了一道题。

得到了几点教训:

1. 如果会输入顶点的编号(一般都是从1开始),需要考虑是不是还从0开始

2. 模板之类的一定不能出错

3. 不要同时有两种思路做题

思路:这道题考验的是编码能力。。。首先跑第一次Kruskal得到一个MST的权值。标记下这棵MST上有哪些边。然后枚举删除这些边当中的一条,跑一遍Kruskal得到的是不是同样权值的MST。(记得判断删边之后造成了不连通的情况)。

PS.因为存在n = 1的情况,所以从1开始计数。

代码如下:

/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
#define INF 2e9
using namespace std;
/****************************************/
const int N = 105, M = N*N;
int n, m, fa[N], same[M];
bool use[M], del[M];
bool fir;
struct Node
{
	int u, v, w;
}edge[M];

bool cmp(Node a, Node b)
{
	return a.w < b.w;
}

int Find(int x)
{
	if(x != fa[x]) fa[x] = Find(fa[x]);
	return fa[x];
}

int Kruskal()
{
	for(int i = 1; i <= n; i++)
		fa[i] = i;
	int sum = 0, cur = 0;
	for(int i = 1; i <= m; i++) if(!del[i]) {
		int fx = Find(edge[i].u), fy = Find(edge[i].v);
		if(fx != fy) {
			sum += edge[i].w;
			fa[fy] = fx;
			cur++;
			if(fir) use[i] = true;
		}
		if(cur == n-1) break;
	}
	int Set = 0;
	for(int i = 1; i <= n; i++) {
		if(fa[i] == i) {
			Set++;//存在1个以上的集合,则不连通
			if(Set > 1) return -1;
		}
	}
	return sum;
}

int main()
{
#ifdef J_Sure
	freopen("111.in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
    int T;
    scanf("%d", &T);
    while(T--) {
		memset(del, 0, sizeof(del));
		memset(use, 0, sizeof(use));
		scanf("%d%d", &n, &m);
		for(int i = 1; i <= m; i++) {
			scanf("%d%d%d", &edge[i].u, &edge[i].v, &edge[i].w);
		}
		sort(edge+1, edge+m+1, cmp);
		fir = true;
		int ans_st = Kruskal();
		fir = false;
		bool ok = true;
		for(int i = 1; i <= m; i++) if(use[i]) {
			del[i] = true;
			int sum2 = Kruskal();
			if(ans_st == sum2) {
				ok = false;
				break;
			}
			del[i] = false;
		}
		if(ok) {
			printf("%d\n", ans_st);
		}
		else {
			puts("Not Unique!");
		}
	}
    return 0;
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值