The Unique MST
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 30039 | Accepted: 10754 |
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
POJ Monthly--2004.06.27 srbga@POJ
这道题是最简单的次小生成树了,不明白的看这篇文章http://blog.csdn.net/qq_27437781/article/details/70821413
AC代码:
这道题是最简单的次小生成树了,不明白的看这篇文章http://blog.csdn.net/qq_27437781/article/details/70821413
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAX 105
const int INF=0x3f3f3f3f;
int g[MAX][MAX],dist[MAX],mmax[MAX][MAX]; ///g保存地图 dist保存从起点到其余各点的距离 maxn保存从i到j的最大边权值
int pre[MAX]; ///pre保存j的离它最近的是哪个点
bool mark[MAX]; ///相当于vis 用来标记该点是否已经用过
bool connect[MAX][MAX]; ///保存i-j的那条边是否加入了最小生成树 false 加入 true 没有
int mst,mint; ///mst保存最小生成树的权值和
int n,m;
int prim()
{
int res=0,fa,p,min,i,j;
memset(mmax,0,sizeof(mmax));
for(i=1;i<=n;i++)
{
dist[i]=g[1][i];
pre[i]=1;
mark[i]=false;
}
dist[1]=0;
mark[1]=true;
for(i=1;i<n;i++)
{
p=-1;min=INF;
for(j=1;j<=n;j++)
{
if(!mark[j]&&dist[j]<min)
{
p=j;
min=dist[j];
}
}
if(p==-1) return res;
mark[p]=true;
res+=dist[p];
fa=pre[p]; ///找到离p最近的点
connect[fa][p]=false;
connect[p][fa]=false;
mmax[fa][p]=min;
///遍历所有的点 求其余点到p的最大权值
for(j=1;j<=n;j++)
mmax[j][p]=(mmax[fa][p]>mmax[j][fa])?mmax[fa][p]:mmax[j][fa];
for(j=1;j<=n;j++)
{
if(!mark[j]&&dist[j]>g[p][j])
{
dist[j]=g[p][j];
pre[j]=p;
}
}
}
return res;
}
int main()
{
int tc;
scanf("%d",&tc);
while(tc--)
{
scanf("%d %d",&n,&m);
memset(g,INF,sizeof(g));
memset(connect,false,sizeof(connect));
while(m--)
{
int u,v,c;
scanf("%d %d %d",&u,&v,&c);
g[u][v]=c;
g[v][u]=c;
connect[u][v]=true;
connect[v][u]=true;
}
mst=prim();
int i,j;
bool flag=false;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
///如果i-j这条边加入了最小生成树 或者i-j这条路不通 continue
if(connect[i][j]==false||g[i][j]==INF)
continue;
///如果加入的边和删除的边的大小是一样的 说明次小生成树的权值和等于最小生成树的权值和
///也就是说最小生成树不唯一
if(g[i][j]==mmax[i][j])
{
flag=true;
break;
}
}
if(flag)
printf("Not Unique!\n");
else
printf("%d\n",mst);
}
return 0;
}