我是靠谱客的博主 典雅雪碧,最近开发中收集的这篇文章主要介绍POJ1679 判断是否是唯一的最小树,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 34447 Accepted: 12567

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!

题意概括:

    给出可以构成一个树的边,问这些边构成的最小生成树是否是唯一的?如果是输出这个最小生成树,如果不是输出Not Unique!  。。

解题思路:

    先将最小生成树的值算出来,标记最小生成树用过的边,然后依次把这些边删除,再次计算最小生成树的值,如果所有的边都删除一遍计算的最小生成树的值(首先要保证删边之后能构成最小生成树)都和本来的最小生成树不一样,那么可以确定最小生成数是唯一的。

代码:


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct edge
{
int u,v,w;
}e[11000];
int n,m,book[10100];
bool cmp(edge a,edge b)
{
return a.w<b.w;
}
int f[1010];
int getf(int u)
{
if(f[u]==u)
return u;
else
{
f[u]=getf(f[u]);
return f[u];
}
}
int merge(int u,int v)
{
int t1=getf(u);
int t2=getf(v);
if(t1!=t2)
{
f[t2]=t1;
return 1;
}
return 0;
}
void Kruskal()
{
memset(book,0,sizeof(book));
int i,j,num,sum2,sum1;
for(i=1;i<=n;i++)
{
f[i]=i;
}
sum1=sum2=num=0;
for(i=0;i<m;i++)
{
if(merge(e[i].u,e[i].v)==1)
{
book[i]=1;
sum1+=e[i].w;
num++;
}
if(num==n-1)
break;
}
int a=0;
for(i=1;i<=n;i++)
{
if(f[i]==i)
a++;
}
if(a>1)
printf("0n");
else
{
//printf("sum1=%dn",sum1);
int flag=0;
for(i=0;i<m;i++)
{
if(book[i]==1)
{
sum2=0;
for(j=1;j<=n;j++)
{
f[j]=j;
}
for(j=0;j<m;j++)
{
if(j==i)
continue;
if(merge(e[j].u,e[j].v)==1)
{
sum2+=e[j].w;
num++;
}
if(num==n-1)
break;
}
int b=0;
for(j=1;j<=n;j++)
{
if(f[j]==j)
b++;
}
//	printf("sum2=%dn",sum2);
if(b==1&&sum1==sum2)
{
flag=1;
printf("Not Unique!n");
break;
}
}
else
{
continue;
}
}
if(flag==0)
printf("%dn",sum1);
}
}
int main()
{
int i,j,k,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=0;i<m;i++)
{
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
}
sort(e,e+m,cmp);
if(n==1)
printf("0n");
else if(m==0)
printf("0n");
else
Kruskal();
}
return 0;
}

最后

以上就是典雅雪碧为你收集整理的POJ1679 判断是否是唯一的最小树的全部内容,希望文章能够帮你解决POJ1679 判断是否是唯一的最小树所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(33)

评论列表共有 0 条评论

立即
投稿
返回
顶部