The Unique MST
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 31994 | Accepted: 11572 |
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!
题意:
判断最小生成树唯一否。
POINT:
算出最小和次小生成树,比较一下。
不能生成最小生成树的输出0,题目里好像没给
#include <vector>
#include <stdio.h>
#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
#define LL long long
const LL inf = 0x3f3f3f3f;
const LL maxn =111;
LL cost[maxn][maxn];
LL used[maxn][maxn];
LL n,m;
LL Max[maxn][maxn];
LL low[maxn],pre[maxn];
LL prime()
{
memset(low,inf,sizeof low);
memset(Max,0,sizeof Max);
memset(used,0,sizeof used);
LL vis[maxn];
memset(vis,0,sizeof vis);
for(LL i=1;i<=n;i++) low[i]=cost[1][i],pre[i]=1;
low[1]=0;
vis[1]=1;
LL ans=0;
for(LL j=1;j<n;j++)
{
LL minw=inf;
LL p=-1;
for(LL i=1;i<=n;i++)
{
if(!vis[i]&&minw>low[i])
{
minw=low[i];
p=i;
}
}
if(minw==inf) return -1;
vis[p]=1;
ans+=minw;
used[p][pre[p]]=used[pre[p]][p]=1;
for(LL i=1;i<=n;i++)
{
if(vis[i]&&i!=p) Max[i][p]=Max[p][i]=max(Max[i][pre[p]],low[p]);
else
{
if(low[i]>cost[p][i])
{
low[i]=cost[p][i];
pre[i]=p;
}
}
}
}
return ans;
}
void doit()
{
LL ans=prime();
if(ans==-1)
{
printf("0\n");
}
else
{
LL ss=inf;
for(LL i=1;i<=n;i++)
{
for(LL j=1;j<=n;j++)
{
if(cost[i][j]==inf||used[i][j]) continue;
ss=min(ans-Max[i][j]+cost[i][j],ss);
}
}
if(ss==ans)
{
printf("Not Unique!\n");
}
else
printf("%lld\n",ans);
}
}
int main()
{
LL T;
scanf("%lld",&T);
while(T--)
{
memset(cost,inf,sizeof cost);
scanf("%lld %lld",&n,&m);
for(LL i=1;i<=m;i++)
{
LL u,v,w;scanf("%lld %lld %lld",&u,&v,&w);
cost[u][v]=cost[v][u]=w;
}
doit();
}
return 0;
}