題目:
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!'.
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!
這道題目是讓給你一個(gè)無(wú)向圖,讓你判斷它的最小生成樹(shù)是否具有唯一性,如果是就輸出最小生成樹(shù)的權(quán)值,否則輸出"Not Unique!"。
以下是我寫的代碼和我的個(gè)人理解,僅供參考。
參考代碼:
#include <iostream>//使用Kruskal算法;
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 10000+5;
const int M = 100+5;
int n, m;
int par[M];//保存每個(gè)點(diǎn)的根值;//并查集;
int vis[N];//保存訪問(wèn)過(guò)的邊;
int visnum;//已經(jīng)訪問(wèn)過(guò)多少條邊;
struct node {
int st, ed, cost;
} Points[N];
void init() {
memset(Points, 0, sizeof(Points));
for (int i = 1;i < M;++i) {//并查集初始化;
par[i] = i;
}
memset(vis, -1, sizeof(vis));
visnum = 0;
}
void input() {
int a, b, c;
for (int i = 0;i < m;++i) {
cin >> a >> b >> c;
Points[i].st = a;
Points[i].ed = b;
Points[i].cost = c;
}
}
bool cmp(const node &a, const node &b) {//按邊的權(quán)值排序;
return a.cost < b.cost;
}
int find(int x) {//查詢樹(shù)的根;
if (par[x] == x) return x;
else return find(par[x]);
}
void unite(int a, int b) {//合并a和b所屬的集合;
a = find(a);
b = find(b);
if (a != b)
par[a] = b;
}
int mintree() {//求最小生成樹(shù);//考慮不連通的情況;
int res = 0;
int cnt = 0;
for (int i = 0;i < m;++i) {
node e = Points[i];
int a = find(e.st), b = find(e.ed);//并查集;
if (a != b) {//a和b不屬于同一個(gè)集合, 表明該條邊沒(méi)有與其他邊構(gòu)成環(huán);
unite(e.st, e.ed);
vis[visnum++] = i;//表明該條邊已經(jīng)被用過(guò);
res += e.cost;
++cnt;
}
}
if (cnt < n - 1) return -1;//根據(jù)樹(shù)的性質(zhì);//樹(shù)的邊數(shù)=結(jié)點(diǎn)數(shù)-1;
return res;
}
int sectree() {//求次小生成樹(shù);
int res = 0;
int i;
int minnum = 999999;//尋找次小生成樹(shù);
for (int k = 0;k < visnum;++k) {//保證至少有一條邊不是最小生成樹(shù)的邊;
int cnt = 0;
res = 0;
for (int j = 1;j < M;++j) {//初始化并查集;
par[j] = j;
}
for (i = 0;i < m;++i) {
if (vis[k] != i) {
node e = Points[i];
int a = find(e.st), b = find(e.ed);//并查集;
if (a != b) {
unite(e.st, e.ed);
res += e.cost;
++cnt;
}
//cout << "xyz" << endl;
}
}
if (cnt < n - 1) continue;
if (vis[k] != i && minnum > res) {//尋找除最小生成樹(shù)以外的權(quán)值最小的樹(shù);
minnum = res;
}
}
return minnum;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
cin >> n >> m;
if (n == 1) {//特殊情況: 只有1個(gè)點(diǎn);
cout << 0 << endl;
continue;
}
if (m == 0) {//特殊情況: 沒(méi)有邊;
cout << "Not Unique!" << endl;
continue;
}
init();
input();
sort(Points, Points + m, cmp);//按權(quán)值大小排序;
int ans1 = mintree();
int ans2 = sectree();
if (ans1 == ans2 || ans1 == -1) {//若次小生成樹(shù)的大小不等于最小生成樹(shù)的大小, 則表明最小生成樹(shù)是唯一的;
cout << "Not Unique!" << endl;
}
else cout << ans1 << endl;
}
return 0;
}