poj1679 The Unique MST(次小生成树)

题意给你一个n个点m条边的无向图,问你该图的最小生成树是否唯一?如果唯一输出,树的权值,否则输出'Not Unique!'.

思路 本题求该无向图的次小生成树的权值是否等于最小生成树的权值,一个图的次小生成树的权值<=最小生成树的权值,至少有一条边与最小生成树不同,所以可以枚举最小生成树上的边,删除之后用剩下的边重新求最小生成树,求出新生成的最小生成树中权值最小就是为次小生成树的权值

Trick:删除一条边之后原图可能不连通,返回-1,注意考虑清楚


#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 110
const int maxm = 100*100+10;
#define LL long long
int cas=1,T;
struct Edge
{
	int u,v,dist;
	int id;           //边的编号
	Edge(){}
	Edge(int u,int v,int dist,int id):u(u),v(v),dist(dist),id(id){}
	bool operator < (const Edge&rhs)const
	{
		return dist < rhs.dist;
	}
};
int n,m;
Edge edges[maxm];
int pre[maxn];
vector <int> E;            //保存最小生成树的边的编号
int Find(int x)
{
	return pre[x]==-1?x:pre[x]=Find(pre[x]);
}
void init()
{
	m=0;
	memset(pre,-1,sizeof(pre));
}
void AddEdge(int u,int v,int dist,int id)
{
	edges[m++]=Edge(u,v,dist,id);
}
int Kruskal(int ID)
{
	memset(pre,-1,sizeof(pre));
	E.clear();
	int sum = 0;
	int cnt = 0;
	sort(edges,edges+m);
	for (int i = 0;i<m;i++)
	{
		if (edges[i].id == ID)
			continue;
		int u = edges[i].u;
		int v = edges[i].v;
		if (Find(u)!=Find(v))
		{
			E.push_back(edges[i].id);
			pre[Find(u)]=Find(v);
			sum+=edges[i].dist;
			if (++cnt >=n-1)
				break;
		}
	}
	if (cnt < n-1)
		return -1;
	return sum;
}
int main()
{
	//freopen("in","r",stdin);
	scanf("%d",&T);
	while (T--)
	{
		int mm;
		scanf("%d%d",&n,&mm);
		init();
		for (int i = 0;i<mm;i++)
		{
			int u,v,d;
			scanf("%d%d%d",&u,&v,&d);
			AddEdge(u,v,d,i);
		}
		int ans1 = Kruskal(-1);
		int ans2 = 1<<20;
        vector <int>EE(E);
		for (int i = 0;i<EE.size();i++)
		{
			int temp = Kruskal(EE[i]);
			if (temp == -1)
				continue;
			ans2 = min(ans2,temp);
			if (ans2 == ans1)
				break;
		}
		if (ans1 == ans2)
			puts("Not Unique!");
		else
			printf("%d\n",ans1);
	}
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 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!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值