Description
Given a connected undirected graph, tell if it minimum spanning is unique.
Definition 1 (Spanning tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G are a subgraph of g, say T = (V ', E '), with the following properties:
V ' = v.
T is connected and acyclic.
Definition 2 (Minimum spanning Tree): Consider a edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E ') of the "G" the spanning tree which has the smallest total cost. The total cost of T means the sum of the "weights" on "all" edges in E '.
Input
The ' a single integer t (1 <= t <=), the number of test cases, contains. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <=), 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 a edge with Weig HT = wi. For all 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 the 1 2
Sample Output
3 Not
unique!
the
Given a graph, the minimum spanning tree is asked if it is unique, and the output is not unique! if the unique output is the minimum spanning tree weights.
train of Thought
The second niche of the given graph is directly to the tree, then enumerate all the edges that are not in the MST, and replace the edges of the maximum edge right. If the result is still the original MST edge right and, the minimum spanning tree is not unique, otherwise unique.
AC Code
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <cmath> #include <
Algorithm> #include <iostream> using namespace std;
const int MAXN = 110;
const int inf = 0X3F3F3F;
BOOL VIS[MAXN];
int LOWC[MAXN];
int PRE[MAXN];
int MAX[MAXN][MAXN];
BOOL USED[MAXN][MAXN];
int COST[MAXN][MAXN];
int prim (int n) {int ans=0;
memset (Vis) (vis,false,sizeof);
memset (max,0,sizeof (Max));
memset (used,false,sizeof (used));
Vis[0]=true;
Pre[0]=-1;
for (int i=1; i<n; i++) {lowc[i]=cost[0][i];
pre[i]=0;
} lowc[0]=0;
for (int i=1; i<n; i++) {int minc=inf;
int p=-1; for (int j=0; j<n; J + +) {if (!vis[j]&&minc>lowc[j)) {Minc=lo
WC[J];
P=j;
} if (Minc==inf) return-1;
Ans+=minc;
Vis[p]=true;
Used[p][pre[p]]=used[pre[p]][p]=true; for (int j=0; j<n;
J + +) {if (vis[j]&&j!=p) Max[j][p]=max[p][j]=max (Max[j][pre[p]],lowc[p]);
if (!vis[j]&&lowc[j]>cost[p][j]) {lowc[j]=cost[p][j];
Pre[j]=p;
}} return ans;
} void init (int n) {memset (cost,inf,sizeof (cost));
for (int i=0; i<n; i++) cost[i][i]=0; } void solve (int n) {int ans=prim (n);
Minimum spanning tree weights and int ss=inf; for (int i=0; i<n; i++)//enum two point for the edge for (int j=i+1; j<n; J + +) if (cost[i][j]!=inf&&!used[i
][J]//If there is an edge between (I,J) and is not ss=min (Ss,ans+cost[i][j]-max[i][j] in the MST);
if (Ss==ans) printf ("Not unique!\n");
else printf ("%d\n", ans);
int main () {int t,n,m;
scanf ("%d", &t);
while (t--) {scanf ("%d%d", &n,&m);
Init (n);
for (int i=0; i<m; i++) {int a,b,c; scanf ("%d%d%d",&A,&B,&C);
Cost[a-1][b-1]=cost[b-1][a-1]=c;
} solve (n);
return 0; }