您的位置:首页 > 产品设计 > UI/UE

POJ 1679 The Unique MST

2015-09-04 17:20 453 查看
The Unique MST

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 24586Accepted: 8743
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算法搞的,因为prim算法不能(或许我没有发现)判定最小生成树是否唯一。先说明几点,如果每条边的边权都不相同的话,那最小生成树肯定唯一,证明略,有兴趣可以上网查查,反证法可以解决。也就是说,如果一个图的最小生成树不唯一,那么肯定有边权相等的边。知道了这一点我们就在构建边表的时候加入一个变量equ,用来记录有没有和这条边相等的边,如果有值为1,否则0。这样我们去找另一个最小生成树的时候,就枚举删去equ为1的边再去构建最小生成树。如果返回的生成树的总边权和一开始的最小生成树的总边权相等,就说明最小生成树不唯一,这样枚举完equ为1的边,也就知道答案了。

直接贴出代码(相应解释有注释说明):

#include <iostream>
#include <algorithm>
#include <iterator>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <string>
#include <queue>
using namespace std;

const int max_n=105;//点的最大数目
const int max_e=6010;//边的最大数目

struct Edge
{
int u,v,w;//表示边的顶点和权值
bool used;//表示在第一次求MST中是否包含这条边
bool equ;//表示有没有和该边的相等的边
bool del;//表示删除的标记
}edge[max_e];//构建边表
int father[max_n];//为了构造并查集
int n,m;//表示点和边的数目
bool first;//记录是否是第一次构造最小生成树

bool cmp(Edge a,Edge b)
{
return a.w<b.w;
}

void Father()//根节点初始化
{
for(int i=0;i<n;i++)
father[i]=-1;

return ;
}

int Find(int i)//寻找根节点
{
if(father[i]<0)
return i;
else
return father[i]=Find(father[i]);
}

void Union(int u,int v)//合并两个集合
{
int find1,find2,tem;
find1=Find(u);
find2=Find(v);
tem=father[find1]+father[find2];

if(father[find1]<father[find2])//将元素少的集合合并到元素多的集合
{
father[find2]=find1;    father[find1]=tem;
}else
{
father[find1]=find2;    father[find2]=tem;
}

return ;
}

int kruskal()
{
Father();
int weight=0,num=0;

for(int i=0;i<m;i++)
{
if(edge[i].del)    continue;//如果这条边被删了就不用于构造最小生成树

int u,v;
u=edge[i].u;
v=edge[i].v;

if(Find(u)!=Find(v))
{
weight+=edge[i].w;
num++;
Union(u,v);
if(first)   edge[i].used=1;//记录第一次最小生成树用了哪些边
}

if(num>=n-1)    break;
}

if(num<n-1)//说明构建最小生成树失败
return -1;
return weight;
}

int main()
{
//    freopen("s","r",stdin);

int T;
scanf("%d",&T);
while(T--)
{
int u,v,w;
scanf("%d%d",&n,&m);

for(int i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
edge[i].u=u-1;edge[i].v=v-1;edge[i].w=w;
edge[i].equ=0;edge[i].used=0;edge[i].del=0;
}

for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(i==j)    continue;
if(edge[i].w==edge[j].w)//记录存在相等边的边
edge[i].equ=1;
}
}

sort(edge,edge+m,cmp);//对边由小到大排序

int weight1,weight2;
bool p=1;
first=true;
weight1=kruskal();//记录最小生成树的总边权
first=false;

for(int i=0;i<m;i++)
{
if(edge[i].used&&edge[i].equ)
{
edge[i].del=1;
weight2=kruskal();
if(weight2==weight1)
{
printf("Not Unique!\n");
p=0;
break;
}
edge[i].del=0;
}
}

if(p)
printf("%d\n",weight1);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: