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'.
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!
次小生成树。判断一个图的最小生成树是否唯一
枚举删除每一条最小生成树的边,判断是否次小生成树和最小生成树权值想同,想同则不唯一。
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int maxn = 110;
int n, m, map[maxn][maxn], par[maxn], mst[maxn];
struct node
{
/* data */
int a, b, c;
}e[5555];
bool cmp(node a, node b)
{
if(a.c < b.c) return true;
return false;
}
int find(int u)
{
while(par[u] >= 0)
u = par[u];
return u;
}
void merge(int x, int y)
{
if(par[x] < par[y]) {
par[x] = par[x] + par[y];
par[y] = x;
}
else {
par[y] = par[x] + par[y];
par[x] = y;
}
}
void kru()
{
memset(par, -1, sizeof(par));
memset(mst, 0, sizeof(mst));
sort(e + 1, e + m + 1, cmp);
int ans = 0, k = 0;
for(int i = 1; i <= m && k < n - 1; ++i) {
int a = find(e[i].a), b = find(e[i].b);
if(a != b) {
merge(a, b);
k++;
ans += e[i].c;
mst[k] = i;
}
}
for(int i = 1; i <= n - 1; ++i) {
int ans2 = 0, k2 = 0;
memset(par, -1, sizeof(par));
for(int j = 1; j <= m; ++j) {
if(j == mst[i]) continue;
int a = find(e[j].a), b = find(e[j].b);
if(a != b) {
merge(a, b);
k2++;
ans2 += e[j].c;
}
}
if(k2 != n - 1) continue;
if(ans2 == ans) {
printf("Not Unique!\n");
return;
}
}
printf("%d\n", ans);
}
int main(int argc, char const *argv[])
{
int t;
scanf("%d", &t);
while(t--) {
scanf("%d%d", &n, &m);
memset(e, 0, sizeof(e));
for(int i = 1; i <= m; ++i)
scanf("%d%d%d", &e[i].a, &e[i].b, &e[i].c);
kru();
}
return 0;
}