传送门
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!'.
题意:判断最小生成树的是否唯一
思路:裸的次小生成树模板,判断次小生成树的权值和是否等于最小生成树的权值。
次小生成树求法:我们枚举每条不在最小生成树上的边,并把这条边放到最小生成树上面,然后就一定会形成环,那么我们在这条环路中取出一条最长的路(除了新加入的那一条边)。最终我们得到的权值就是次小生成树的权值。
参考博客:https://blog.csdn.net/li1615882553/article/details/80011884
附上代码:
///#include<bits/stdc++.h>
///#include<unordered_map>
///#include<unordered_set>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<bitset>
#include<set>
#include<stack>
#include<map>
#include<list>
#include<new>
#include<vector>
#define MT(a,b) memset(a,b,sizeof(a));
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double pai=acos(-1.0);
const double E=2.718281828459;
const ll mod=1e12;
const int INF=0x3f3f3f3f;
int n,m;
struct node
{
int l;
int r;
int d;
bool vis; ///记录该边是否用于最小生成树
bool friend operator<(node a,node b)
{
return a.d<b.d;
}
}load[5000];
int p[105];
int maxn[105][105]; ///maxn[i][j]表示从i点到j点之间所有边权的最大值
vector<int>point[105];
int find(int x)
{
return p[x]==x?x:p[x]=find(p[x]);
}
void kruskal()
{
int min_tree=0;
sort(load+1,load+1+m);
///并查集初始化
for(int i=1;i<=n;i++)
{
point[i].clear();
point[i].push_back(i);
p[i]=i;
}
for(int i=1;i<=m;i++)
{
int x=find(load[i].l);
int y=find(load[i].r);
int c=load[i].d;
if(x!=y)
{
min_tree+=c;
load[i].vis=true;
int d1=point[x].size();
int d2=point[y].size();
///更新maxn数组
for(int j=0;j<d1;j++)
{
for(int k=0;k<d2;k++)
{
int s=point[x][j];
int e=point[y][k];
maxn[s][e]=maxn[e][s]=c;
}
}
///合并关系
p[y]=x;
for(int j=0;j<d2;j++)
point[x].push_back(point[y][j]);
}
}
int smaller=INF;
for(int i=1;i<=m;i++)
if(!load[i].vis)
smaller=min(smaller,min_tree-maxn[load[i].l][load[i].r]+load[i].d);
if(smaller==min_tree)
printf("Not Unique!\n");
else
printf("%d\n",min_tree);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&load[i].l,&load[i].r,&load[i].d);
load[i].vis=false;
}
kruskal();
}
return 0;
}