POJ-The Unique MST

11 篇文章 0 订阅
4 篇文章 0 订阅

The Unique MST

题目描述

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’.

输入

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.

输出

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!

题目大意

询问这个图的最小生成树是否唯一。
判断是否唯一只需要比较其最小生成树和非严格次小生成树就可以了。
本题就是针对与非严格次小生成树所做出的。

我们在链接的时候,记录一下,两个集合每个元素到彼此每个元素的权值。因为要拿一个边去替换的话,我们必须知道替换下来的边的权值是多少。
好核心部分说明了,那么继续

Accepted Code

//#include<unordered_map>
#include<algorithm>
#include<iostream>
#include<string.h>
#include <iomanip>
#include<stdio.h>
#include<vector>
#include<string>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll ll_inf = 9223372036854775807;
const int int_inf = 2147483647;
const short short_inf = 32767;
const ll less_inf = 0x3f3f3f3f;
const char char_inf = 127;
#pragma GCC optimize(2)
#define accelerate cin.tie(NULL);cout.tie(NULL);ios::sync_with_stdio(false);
#define PI 3.141592653589793
#define EPS 1.0e-8
ll gcd(ll a, ll b) {
	return b ? gcd(b, a % b) : a;
}
ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}
inline ll read() {
	ll c = getchar(), Nig = 1, x = 0;
	while (!isdigit(c) && c != '-')c = getchar();
	if (c == '-')Nig = -1, c = getchar();
	while (isdigit(c))x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar();
	return Nig * x;
}
inline void out(ll a) {
	if (a < 0)putchar('-'), a = -a;
	if (a > 9)out(a / 10);
	putchar(a % 10 + '0');
}
ll qpow(ll x, ll n, ll mod) {
	ll res = 1;
	while (n > 0) {
		if (n & 1)res = (res * x) % mod;
		x = (x * x) % mod;
		n >>= 1;
	}
	return res;
}
#define read read()
struct node
{
	int sta, end, val;
	bool friend operator<(node a, node b)
	{
		return a.val < b.val;
	}
}save[10005];
int fa[105];
bool judge[10005];
int mp[105][105];
int find(int x)
{
	return fa[x] == x ? x : fa[x] = find(fa[x]);

}
struct link {
	int head, end, next;
}Link[105];
void merge(int x, int y)
{
	int fx = find(x), fy = find(y);
	if (fx != fy)fa[fx] = fy;
}
int main()
{
	int T = read;
	while (T--)
	{
		for (int i = 0; i < 105; i++)fa[i] = i;
		memset(judge, 0, sizeof(judge));
		memset(mp, 0, sizeof(mp));
		for (int i = 0; i < 105; i++)
			Link[i].head = Link[i].end = i, Link[i].next = -1;
		int n = read, m = read;
		for (int i = 0; i < m; i++)
			save[i].sta = read, save[i].end = read, save[i].val = read;
		sort(save, save + m);
		int num = 0, cnt = 0;
		int ans = 0;
		for (int i = 0; i < m; i++)
		{
			if (find(save[i].sta) != find(save[i].end))
			{
				merge(save[i].sta, save[i].end);
				ans += save[i].val;
				judge[i] = true;
				for (int u = Link[save[i].sta].head; ~u; u = Link[u].next)
					for (int v = Link[save[i].end].head; ~v; v = Link[v].next)
						mp[u][v] = mp[v][u] = save[i].val;
				Link[Link[save[i].sta].end].next = Link[save[i].end].head;
				Link[save[i].sta].end = Link[save[i].end].end;
				Link[save[i].end].head = Link[save[i].sta].head;
			}
		}
		int res = int_inf;
		for (int i = 0; i < m; i++)
			if (!judge[i])
				res = min(res, ans - mp[save[i].sta][save[i].end] + save[i].val);
		if (ans == res)puts("Not Unique!");
		else
		{
			out(ans);
			puts("");
		}
	}
}

By-Round Moon

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Round moon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值