poj The Unique MST【判断唯一最小生成树】

15 篇文章 0 订阅


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

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!

题意:给定每条边的边权以后,判断最小生成树是否唯一,如果唯一输出权值。

思路:主要思路是,将最小生成树求出以后,再去掉生成树上的每一条边求最小生成树权值,如果仍然存在权值和原本的最小生成树权值相等,说明最小生成树并不唯一。具体思路为:Kruskal求最小生成树过程中,会将边权从小到大进行合并,那么我们可以将加入最小生成树的边进行标记;枚举每一条边,如果该边已经被标记,则调用Kruskal算法求去掉该边后的最小生成树权值,由于存在边权为0的点,所以需要在返回权值之前判断能否构成一个生成树,否则返回inf。

数据:

3
1 0
4 5
1 2 1
2 3 1
3 4 1
1 4 2
2 4 1
6 7
1 3 1
1 2 2
2 3 3
3 4 0
4 6 5
4 5 4
5 6 6

输出

0
Not Unique!

12

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 1100;
#define inf 0x3f3f3f3f
int n,m;
int f[maxn],book[maxn][maxn];
struct node{
	int from,to,w;
};
struct node vis[maxn*100];
int cmp(struct node a,struct node b)
{
	return a.w < b.w ;
}
int find(int u)
{
	if(f[u] == u)
		return u;
	else
	{
		f[u] = find(f[u]);
		return f[u];
	}
}

int Union(int x,int y)
{
	int nx,ny;
	nx = find(f[x]);
	ny = find(f[y]);
	if(nx!=ny)
	{
		f[nx] = ny;
		return 1;
	}
	return 0;
}
int Kruskal(int x,int y)
{
	int cnt = 0;
	int sum = 0,i;
	for(i = 0; i <= n; i ++)
		f[i] = i;
	for(i = 0; i < m; i ++)
	{
		if(vis[i].from == x&&vis[i].to == y)
			continue;
		if(Union(vis[i].from ,vis[i].to))
		{
			sum += vis[i].w ;
			cnt ++;
		}
		if(cnt == n-1)
			break;
	}
	if(cnt == n-1)//去掉该边以后必须保证能够生成最小生成树才返回权值 
		return sum;
	return inf;
}
int main()
{
	int t,i,j,sum,flag,sum2,cnt;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		for(i = 0; i < m; i ++)
			scanf("%d%d%d",&vis[i].from,&vis[i].to ,&vis[i].w );
		for(i = 0; i <= n; i ++)
			f[i] = i;
		memset(book,0,sizeof(book));
		//kruskal求最小生成树的值 
		sort(vis,vis+m,cmp);
		cnt = 0;
		sum = 0;
		for(i = 0; i < m; i ++)
		{
			if(Union(vis[i].from ,vis[i].to))
			{
				sum += vis[i].w ;
				cnt ++;
				book[vis[i].from][vis[i].to] = 1;//将加入最小生成树的点标记为1 
			}
			if(cnt == n-1)
				break;
		}
		//求出最小值为sum 
		flag = 0;
		for(i = 0;  i < m; i ++)
		{
			if(book[vis[i].from][vis[i].to])//如果该边属于最小生成树 
			{
				sum2 = Kruskal(vis[i].from,vis[i].to );//去掉这条边后求最小生成树的权值 
				if(sum2 == sum)
				{//如果去掉这条边后仍然存在相同的最小生成树的值,说明最小生成树不唯一 
					flag = 1;
					break;
				}
			}
		}
		if(flag)
			printf("Not Unique!\n");
		else
			printf("%d\n",sum);
	}
	return 0;
}








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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值