The Unique MST
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 28295 | Accepted: 10106 |
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!
题意:判断最小生成树是否唯一。
思路:求出次小生成树的权值和最小生成树对比看是否相同。
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include<string.h>
#include<vector>
#include<queue>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<map>
#include<stdlib.h>
#include<time.h>
#include<stack>
#include<set>
#include<deque>
using namespace std;
typedef long long ll;
struct data
{
int a,b,w;
bool vis;
} p[20010];
vector<int>g[110];
int f[110],len[110][110];
const int oo=1e9;
bool cmp(data a,data b)
{
if(a.w!=b.w)
return a.w<b.w;
if(a.a!=b.a)
return a.a<b.a;
return a.b<b.b;
}
int fa(int x)
{
if(x!=f[x])
return f[x]=fa(f[x]);
return f[x];
}
int main()
{
int n,m,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0; i<m; i++)
scanf("%d%d%d",&p[i].a,&p[i].b,&p[i].w),p[i].vis=0;
sort(p,p+m,cmp);
for(int i=0; i<=n; i++)
{
g[i].clear();
g[i].push_back(i);
f[i]=i;
}
int sum=0;
for(int i=0; i<m; i++)
{
int x1=fa(p[i].a),x2=fa(p[i].b);
if(x1!=x2)
{
p[i].vis=1;
sum+=p[i].w;
int l=g[x1].size(),ll=g[x2].size();
for(int j=0; j<l; j++)
for(int k=0; k<ll; k++)
len[g[x1][j]][g[x2][k]]=len[g[x2][k]][g[x1][j]]=p[i].w;
f[x1]=x2;
int tem[110];
for(int j=0; j<ll; j++)
tem[j]=g[x2][j];
for(int j=0; j<l; j++)
g[x2].push_back(g[x1][j]);
for(int j=0; j<ll; j++)
g[x1].push_back(tem[j]);
}
}
int cisum=oo;
for(int i=0; i<m; i++)
if(!p[i].vis)
cisum=min(cisum,sum+p[i].w-len[p[i].a][p[i].b]);
if(cisum==sum)
puts("Not Unique!");
else
printf("%d\n",sum);
}
return 0;
}