The Unique MST
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 29566 | Accepted: 10583 |
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!
Source
题意:给你N个点M条边的图,图的最小生成树是否唯一
解题思路:次小生成树
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
#define MAXV 110
#define MAXE 10100
struct node
{
int s,e,w,flag;
}x[MAXE];
int n,m;
int f[MAXV];
int cmp(node a,node b)
{
return a.w<b.w;
}
int Find(int x)
{
if(f[x]==x) return x;
else return f[x]=Find(f[x]);
}
bool Union(int a,int b)
{
int aa=Find(a);
int bb=Find(b);
if(aa==bb) return 0;
f[aa]=bb;
return 1;
}
int kruskal()
{
int sum=0,cnt=0;
for(int i=0; i<=n; i++) f[i]=i;
for(int i=0; i<m; i++)
{
if(Union(x[i].s,x[i].e))
{
sum+=x[i].w;
x[i].flag=1;
cnt++;
if(cnt==n-1) break;
}
}
if(cnt==n-1) return sum;
return -1;
}
int kruskalt()
{
int sum=0,cnt=0;
for(int i=0;i<=n;i++) f[i]=i;
for(int i=0;i<m;i++)
{
if(Union(x[i].s,x[i].e))
{
sum+=x[i].w;
cnt++;
if(cnt==n-1) break;
}
}
if(cnt==n-1) return sum;
return -1;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&x[i].s,&x[i].e,&x[i].w);
x[i].flag=0;
}
sort(x,x+m,cmp);
int ans=kruskal();
int flag=0;
for(int i=0; i<m; i++)
{
if(x[i].flag)
{
int s=x[i].s;
x[i].s=x[i].e;
int tmp=kruskalt();
if(tmp!=-1&&ans==tmp)
{
flag=1;
break;
}
x[i].s=s;
}
}
if(!flag) printf("%d\n",ans);
else printf("Not Unique!\n");
}
return 0;
}