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'.
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!'.
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
3 Not Unique!
判断最小生成树是否唯一,如果唯一就输出最小值,如果不唯一就输出Not Unique!。思路是这样的,小跑一遍Kruskal,求得最小生成树,把首次求得的最小生成树的边记录下来,然后枚举记录下来的边,每次删除一条边,再求最小生成树,如果求得的最小生成树的值与第一次求得值一样,就说明这个最小生成树不唯一。比如第二组样例:
1 2 2; 2 3 2;3 4 2; 可以形成最小生成树
1 2 2; 3 4 2; 1 4 2; 也可以形成最小生成树,并且两个最小生成树的值一样,所以最小生成树不唯一。
具体请看代码:
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> using namespace std; int f[105]; struct edge{ int x, y; int cost; }s[10005]; int e[10005]; bool cmp(edge a, edge b) { return a.cost < b.cost; } void init(int n) { for(int i = 1; i <= n; i++) { f[i] = i; } } int getf(int v) { if(f[v] != v) { f[v] = getf(f[v]); } return f[v]; } int main() { int N; int n, m; int ans, p; scanf("%d", &N); while(N--) { p = 0; ans = 0; scanf("%d %d", &n, &m); init(n); for(int i = 1; i <= m; i++) { scanf("%d %d %d", &s[i].x, &s[i].y, &s[i].cost); } sort(s+1, s+m+1, cmp); for(int i = 1; i <= m; i++) {//求最小生成树 int t1 = getf(s[i].x); int t2 = getf(s[i].y); if(t1 != t2) { f[t1] = t2; ans += s[i].cost; e[p++] = i; //记录边,枚举 } } bool flag = true; for(int i = 0; i < p; i++) {//开始枚举 init(n); int ans1 = 0, s1 = 0; for(int j = 1; j <= m; j++) { if(e[i] == j) continue; int t1 = getf(s[j].x); int t2 = getf(s[j].y); if(t1 != t2) { f[t1] = t2; ans1 += s[j].cost; } } for(int j = 1; j <= n; j++) {//判断是否成为最小生成树 if(f[j] == j) { s1++; } } if(s1 == 1) { if(ans1 == ans) { flag = false; break; } } } if(flag) printf("%d\n", ans); else puts("Not Unique!"); } return 0; }