(POJ - 1679)The Unique MST(kruscal求最小生成树)

题目:

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'. 

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!

题意:空和妹妹白这一天又很无聊,他们决定进行一场较量!
于是他们开始玩一个游戏:妹妹白先从N个点的图中选出N-1条边,使得N个点连通,并且所选的边的权值之和最小。哥哥空再来一次同样的操作,但是他不能跟妹妹选择的边完全一样,他需要找到另外一种方案,使得他找到的N-1条边的权值之和和妹妹相同,如果找不到他就输了。

分析:这道题目是对kruscal算法求最小生成树的一个变形,怎样确定存在两个最小生成树边权和相同但是边不完全相同呢?我们一开始利用kruscal算法先求出最小生成树包含的边,我们每次删去初始最小生成树中其中一条边再利用kruscal算法求最小生成树删去的含义就是本次最小生成树的构造过程中不能使用该边),如果求出来的最小生成树的边权之和等于初始最小生成树的边权之和则说明存在不同的最小生成树,但是有一点需要注意的是当我们删除初始最小生成树中其中一条边后可能会导致不连通,这种情况应该判断一下。

下面是代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
const int N=103,M=5003;
int fu[N];
int edge[N];
struct node{
	int x,y,w;
}p[M];
bool cmp(node a,node b)
{
	return a.w<b.w;
}
int find(int x)
{
	if(x!=fu[x]) fu[x]=find(fu[x]);
	return fu[x];
}
int main()
{
	int T,n,m;
	cin>>T;
	while(T--)
	{
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++) fu[i]=i;
		int u,v,w;
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d%d",&u,&v,&w);
			p[i]={u,v,w};
		}
		sort(p+1,p+m+1,cmp);
		int count=0,ans=0,tans;
		for(int i=1;i<=m;i++)
		{
			int f1=find(p[i].x),f2=find(p[i].y);
			if(f1==f2) continue;
			fu[f1]=f2;
			ans+=p[i].w;
			edge[++count]=i;//记录一组最小生成树包含的边 
			if(count==n-1) break;
		}
		bool flag=false;//标记是否找到第二种最小生成树边的组成方案 
		for(int i=1;i<n;i++)//枚举删除第i条边之后的最小生成树 
		{
			count=tans=0;
			for(int j=1;j<=n;j++) fu[j]=j;//别忘记将fu数组初始化 
			for(int j=1;j<=m;j++)
			{
				if(j==edge[i]) continue;//不能加入初始条件下最小生成树的第i条边 
				int f1=find(p[j].x),f2=find(p[j].y);
				if(f1==f2) continue;
				fu[f1]=f2;
				tans+=p[j].w;
				count++;
				if(count==n-1) break;
			}
			if(count!=n-1) continue;//删除某条边后可能会导致图不连通 
			if(tans==ans)
			{
				flag=true;
				break;
			}
		}
		if(flag) puts("Not Unique!");
		else printf("%d\n",ans);
	}
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值