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
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.
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 2Sample Output
3 Not Unique!
考查次小生成树。使用Kruskal算法可以求得最小生成树,求MST的时候记录每条边的编号,然后跑次小生成树的时候依次枚举其他边。
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#define MAX 100 + 10
using namespace std;
typedef struct Edge {
Edge( int f, int t, int c ) { from = f; to = t; w = c; }
int from;
int to;
int w;
} Edge;
vector<Edge> edge;
vector<int> edgeId;
int father[MAX];
int n, m;
void initSet() {
for( int i = 0; i < MAX; i++ ) father[i] = i;
}
int findFather( int x ) {
int a = x;
while( x != father[x] ) x = father[x];
while( a != father[a] ) {
int z = a;
a = father[a];
father[z] = x;
}
return x;
}
int cmp( Edge a, Edge b ) {
return a.w < b.w;
}
int kruskal() {
initSet();
sort( edge.begin(), edge.end(), cmp );
int sum = 0;
int cnt = 0;
for( int i = 0; i < m; i++ ) {
int faA = findFather( edge[i].from );
int faB = findFather( edge[i].to );
if( faA != faB ) {
father[faA] = faB;
sum += edge[i].w;
edgeId.push_back( i );
cnt++;
}
}
if( cnt == n - 1 ) return sum;
return -1;
}
int uniqueMST() {
sort( edge.begin(), edge.end(), cmp );
int sum = kruskal();
//printf( "MST = %d\n", sum );
bool flag = true;
for( int i = 0; i < edgeId.size(); i++ ) {
initSet();
int cnt = 0;
int cur = 0;
for( int j = 0; j < m; j++ ) {
if( j == edgeId[i] ) continue;
int faA = findFather( edge[j].from );
int faB = findFather( edge[j].to );
if( faA != faB ) {
father[faA] = faB;
cnt++;
cur = cur + edge[j].w;
}
}
//printf( "cur = %d\n", cur );
if( cnt == n - 1 && cur == sum ) {
flag = false;
break;
}
}
return flag ? sum : -1;
}
int main() {
int T;
scanf( "%d", &T );
while( T-- ) {
edge.clear();
edgeId.clear();
scanf( "%d%d", &n, &m );
for( int i = 0; i < m; i++ ) {
int f, t, w;
scanf( "%d%d%d", &f, &t, &w );
edge.push_back( Edge( f, t, w ) );
}
int ans = uniqueMST();
if( ans == -1 ) printf( "Not Unique!\n" );
else printf( "%d\n", ans );
}
return 0;
}