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:
- V’ = V.
- 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("");
}
}
}