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'.
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.
For each input, if the MST is unique, print the total cost of it, or otherwise print the string 'Not Unique!'.
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
3 Not Unique!
次小生成树:
下面介绍一下利用prim求次小生成树的主要步骤。
1.先求出来最小生成树。并将最小生成树任意两点之间路径当中的权值最大的那一条找出来,为什么要找最大的呢,因为生成树加入一条边之后一定构成了回路,那么肯定要去掉这个回路当中一条边才是生成树,那么,怎么去边才是次小的,那就去掉除了刚刚添加的一条边之外回路当中权值最大的一个,所以留下的就是最小的。
2.枚举最小生成树外的每一条边。找出最小的就是次小生成树。
题意: 题意:给定图,让求它的最小生成树是否唯一。如果唯一的话输出最小生成树的权值和,否则输出Not Unique!
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Max 1010
#define INF 0x3f3f3f3f
int n,m;
int use[Max][Max];//判断该边是否加入最小生成树
int map[Max][Max];
int dis[Max];
int book[Max];
int M[Max][Max]; //表示最小生成树中i到j的最大边权
int pre[Max]; // 记录节点的前驱;
//int hou[Max]; // 以后在一棵树中,就不要记录一个点的后继了,树中结点的后继可能有多个
// 而前驱就一个;
int prim(int star,int k)
{
int i,j;
memset(book,0,sizeof(book));
memset(M,0,sizeof(M));
int ans = 0;
for(i=1;i<=n;i++)
{
dis[i] = map[star][i];
pre[i] = star;
}
book[star] = 1;
for(i=1;i<=n-1;i++)
{
int tt = INF;
for(j=1;j<=n;j++)
{
if(!book[j]&&tt>dis[j])
{
tt = dis[j];
star = j;
}
}
if(tt==INF) return -1;
ans += dis[star];
book[star] = 1;
use[star][pre[star]] = use[pre[star]][star] = 1; // 在这记录使用过的边时,一定要用use[star][per[star]]
// 不能用被标记的倒数第二个节点和倒数第一个结点;例子 如use[star][v] = 1;
// 在这个例子中star为被标记的倒数第二个节点,v为 被标记的倒数第一个结点,
// 你一定要理解我说的,因为你自己很容易用错;
for(j=1;j<=n;j++)
{
if(book[j]&&j!=star)
M[j][star] = M[star][j] = max(M[j][pre[star]],dis[star]);
// M[j][pre[star]] 是最小生成树中节点 j 到star的前驱的节点 这段路中的最大权边;
// dis[star] 是 star的前驱的节点 到 star 的权值;
// 这里一定要加上j!=star,就这道题而言,不加竟然也过了,下面有一组数据,不加这个就是唯一的,正确答案不唯一;
// 因为这样M[i][i] = dis[i];这样会影响后面的M数组中数据;例如当先从 1-2 权16,M[1][2] = 16
// M[2][2] = 16,下一步,从 2-3 权为5, 这是star==3,per[star] = 2;
// 当j==2时,M[j][star] = Max(M[j][per[star]],dis[star]);M[2][2] ==16,dis[star] = 5;
// 所以这时,j==2,star==3 M[2][3] = 16;本来应该为 M[2][3] = dis[3] = 5;
if(!book[j]&&dis[j]>map[star][j])
{
dis[j] = map[star][j];
pre[j] = star;
}
}
}
return ans;
}
int smst(int ans)
{
int mi = INF;
int i,j;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(map[i][j]!=INF&&!use[i][j])
{
if(map[i][j]==M[i][j]) // 若节点i到节点j的权边没有用,且和最小生成树中的节点i到j中的最大权边相等,
{ // 这时候就可以用i到j的权边替换最小生成树中的i到j最大权边,且最小生成树的权值不变;
return ans;
}
//mi = min(mi,ans+map[i][j]-M[i][j]);
}
}
}
return -1;
}
void init()
{
int i,j;
for(i=0;i<=n;i++)
for(j = i;j<=n;j++)
{
if(i==j)
map[i][j] = 0;
else
map[i][j] = map[j][i] = INF;
use[i][j] = use[j][i] = 0;
}
}
int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
int x,y,z;
init();
for(i=0;i<m;i++)
{
scanf("%d%d%d",&x,&y,&z);
map[x][y] = z;
map[y][x] = z;
}
int ans = prim(1,0);
//printf("ans == %d\n",ans);
if(ans==-1) printf("Not Unique!\n");
else
{
if(smst(ans)==ans)
{
printf("Not Unique!\n");
}
else printf("%d\n",ans);
}
}
return 0;
}
6 10
2 3 5
2 4 6
3 4 6
2 6 6
4 6 6
1 2 16
4 5 18
1 5 19
1 6 21
5 6 33
输出 Not Unique!
本文介绍了一种判断给定图的最小生成树是否唯一的方法,并提供了详细的算法步骤及实现代码。通过Prim算法求出最小生成树后,再进一步判断其唯一性。
1万+

被折叠的 条评论
为什么被折叠?



