poj1679The Unique MST判断最小生成树是否唯一以及求次小生成树边权和的讲解

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 22346   Accepted: 7924

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!

Source

POJ Monthly--2004.06.27 [email protected]

次小生成树:除却当前已生成的最小生成树外,最小的生成树

判断最小生成树唯一:若当前的最小生成树的边权和不等于次小生成树的边权和则此图中最小生成树唯一

计算次小生成树的边权和:

首先生成一棵最小生成树并维护两个数组

pre[i]:树中,i的父亲

mx[i][j]:i到j的树链上最大的边权是多少

方便描述,设一个数组cost[i][j]:i跟j之间的边的权值

然后,枚举任意两点i,j,满足,在树中i,j没有连边,

若最小生成树的边权和sum,存在sum-mx[i][j]+cost[i][j]==sum则说明最小生成树不唯一

这个过程就是所谓的删掉i到j的树链中的最大边,再加上树外的边使得i,j再次连通,

形成一棵新的树,若新树的边权和==sum,则说明最小生成树不唯一

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
bool used[110],fb[110][110];
int pre[110],dis[110],mx[110][110],cost[110][110];
int work(int n)
{
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		dis[i]=cost[1][i];
		pre[i]=1;
	}
	memset(fb,0,sizeof(fb));
	memset(used,0,sizeof(used));
	used[1]=1;
	int N=n;
	while(--N)
	{
		int from,mn=INT_MAX;
		for(int i=1;i<=n;i++)
			if(!used[i]&&mn>dis[i])
			{
				from=i;
				mn=dis[i];
			}
		ans+=mn;
		used[from]=1;
		fb[from][pre[from]]=fb[pre[from]][from]=1;
		for(int i=1;i<=n;i++)
			if(used[i])
				mx[i][from]=mx[from][i]=max(mx[i][pre[from]],dis[from]);
			else if(!used[i]&&dis[i]>cost[from][i])
			{
				dis[i]=cost[from][i];
				pre[i]=from;
			}
	}
	return ans;
}
int ok(int n)
{
	int ans=work(n);
	int mn=INT_MAX;
	for(int i=1;i<=n;i++)
		for(int j=i+1;j<=n;j++)
			if(!fb[i][j]&&cost[i][j]!=INT_MAX)
				mn=min(mn,ans-mx[i][j]+cost[i][j]);
	return mn==ans?-1:ans;
}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		int n,m;
		cin>>n>>m;
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				cost[i][j]=INT_MAX;
		while(m--)
		{
			int s,e,w;
			cin>>s>>e>>w;
			cost[s][e]=cost[e][s]=w;
		}
		int ans=ok(n);
		if(ans==-1)
			cout<<"Not Unique!"<<endl;
		else
			cout<<ans<<endl;
	}
}
时间: 2024-08-25 18:24:04

poj1679The Unique MST判断最小生成树是否唯一以及求次小生成树边权和的讲解的相关文章

poj 1679 The Unique MST (判断最小生成树是否唯一)

链接:poj 1679 题意:判断最小生成树是否唯一, 若唯一,输出最小权值和,否则,输出  Not Unique! 判断最小生成树是否唯一的思路: 1.对图中的每一条边,扫描其他边,如果存在相同权值的边,则对该边做标记 2.然后用Kruskal算法或Prim算法求MST 3.求得MST后,如果该MST中未包含做了标记的边,即可判断MST唯一: 如果包含做了标记的边,则依次去掉这些边的一条边,再求MST, 如果求得的MST权值和原来的MST的权值一样,即可判断MST不唯一. 个人思路是先求最小生

POJ 1679 The Unique MST 判断最小生成树是否唯一/次小生成树

题目链接: 1679 题意: 给出 M个点N条边 求它的的最小生成树 不唯一则输出:Not Unique! 题解: prim:判断"最小生成树是否唯一"可以理解为"最小生成树和次小生成树是否相等" 求次小生成树的步骤如下 1)先求出最小生成树T,在prim的同时,用一个矩阵maxx[u][v]记录在树中连接u-v的路径中权值最大的边. 2)枚举所有不在T中的边map[u][v],加入边u-v,删除权值为maxx[u][v]的边; 3)找到MST-maxx[u][v]

POJ-1679 The Unique MST (判断最小生成树的唯一性)

<题目链接> 题目大意: 给定一张无向图,判断其最小生成树是否唯一. 解题分析: 对图中每条边,扫描其它边,如果存在相同权值的边,则标记该边:用kruskal求出MST. 如果MST中无标记的边,则该MST唯一:否则,在MST中依次去掉标记的边,再求MST,若求得MST权值和原来的MST 权值相同,则MST不唯一. #include <cstdio> #include <iostream> #include <cstring> #include <al

poj 1679 The Unique MST (判定最小生成树是否唯一)

题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29408   Accepted: 10520 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spannin

POJ 1679 The Unique MST 推断最小生成树是否唯一

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22715   Accepted: 8055 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ--1679--The Unique MST【kruskal判断MST是否唯一】

链接:http://poj.org/problem?id=1679 题意:告诉你有n个点,m条边,以及m条边的信息(起点.终点.权值),判断最小生成树是否唯一 判断MST是否唯一的思路是这样:对于每条边如果有和他相等权值的边,则做一个标记,然后进行一遍kruskal或prim找出最小生成树权值,然后对于每个使用过并且有相等边标记的边,把它从图中删去,再进行一遍kruskal或prim,如果此时最小生成树权值和第一次一样,则说明最小生成树不唯一,否则最小生成树唯一. #include<cstrin

poj 1679 The Unique MST (判断最小生成树是否唯一)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20679   Accepted: 7255 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ 1679 The Unique MST(判断最小生成树是否唯一)

题目链接: http://poj.org/problem?id=1679 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

【POJ 1679 The Unique MST】最小生成树

无向连通图(无重边),判断最小生成树是否唯一,若唯一求边权和. 分析生成树的生成过程,只有一个圈内出现权值相同的边才会出现权值和相等但“异构”的生成树.(并不一定是最小生成树) 分析贪心策略求最小生成树的过程(贪心地选最短的边来扩充已加入生成树的顶点集合U),发现只有当出现“U中两个不同的点到V-U中同一点的距离同时为当前最短边”时,才会出现“异构”的最小生成树. 上图为kruscal和prim生成过程中可能遇到的相等边的情况,红色和蓝色的为权值相等的边. 可以看到kruscal由于事先将所有边