The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 20737   Accepted: 7281

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!
题意:判断最小生成树是否唯一;
分析:最小生成树的值和次小生成树的值相等则不唯一;
程序:
#include"stdio.h"
#include"string.h"
#define inf 100000000
#define M 1111
int G[M][M],maxd[M][M],use[M],dis[M],pre[M],vis[M][M];
int max(int a,int b)
{
return a>b?a:b;
}
int min(int a,int b)
{
return a<b?a:b;
}
int dij(int u,int n)
{
int ans=0,i,j;
memset(use,0,sizeof(use));
memset(maxd,0,sizeof(maxd));//记录不在任意两点在在生成树的路径中的最长边
memset(vis,0,sizeof(vis));//标记边是否在生成树里面
for(i=1;i<=n;i++)
{
dis[i]=G[u][i];
pre[i]=u;//记录父节点
}
dis[u]=0;
use[u]=1;
for(i=1;i<n;i++)
{
int mini=inf;
int tep=-1;
for(j=1;j<=n;j++)
{
if(!use[j]&&dis[j]<mini)
{
mini=dis[j];
tep=j;
}
}
if(tep==-1)break;
use[tep]=1;
vis[tep][pre[tep]]=vis[pre[tep]][tep]=1;
ans+=mini;
for(j=1;j<=n;j++)
{
if(!use[j]&&dis[j]>G[tep][j])
{
dis[j]=G[tep][j];
pre[j]=tep;
}
if(j!=tep)
maxd[tep][j]=maxd[j][tep]=max(mini,maxd[pre[tep]][j]);//更新
}
}
return ans;
}
int main()
{
int T,m,n,i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
G[i][j]=inf;
while(m--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(G[a][b]>c)
G[a][b]=G[b][a]=c;
}
int ans=dij(1,n);
int cnt=inf;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(G[i][j]<inf&&vis[i][j]==0)
{
cnt=min(cnt,ans+G[i][j]-maxd[i][j]);
}
}
}
if(ans==cnt)
printf("Not Unique!\n");
else
printf("%d\n",ans);
}
return 0;
}

次小生成树(poj1679)的更多相关文章

  1. 次小生成树(POJ1679/CDOJ1959)

    POJ1679 首先求出最小生成树,记录权值之和为MinST.然后枚举添加边(u,v),加上后必形成一个环,找到环上非(u,v)边的权值最大的边,把它删除,计算当前生成树的权值之和,取所有枚举加边后生 ...

  2. POJ1679 The Unique MST[次小生成树]

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28673   Accepted: 10239 ...

  3. POJ1679(次小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36692   Accepted: 13368 ...

  4. POJ1679(次小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24201   Accepted: 8596 D ...

  5. POJ1679 The Unique MST【次小生成树】

    题意: 判断最小生成树是否唯一. 思路: 首先求出最小生成树,记录现在这个最小生成树上所有的边,然后通过取消其中一条边,找到这两点上其他的边形成一棵新的生成树,求其权值,通过枚举所有可能,通过这些权值 ...

  6. POJ1679 The Unique MST 【次小生成树】

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20421   Accepted: 7183 D ...

  7. poj1679次小生成树入门题

    次小生成树求法:例如求最小生成树用到了 1.2.4这三条边,总共5条边,那循环3次的时候,每次分别不用1.2.4求得最小生成树的MST,最小的MST即为次小生成树 如下代码maxx即求最小生成树时求得 ...

  8. POJ1679 The Unique MST —— 次小生成树

    题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total S ...

  9. POJ-1679 The Unique MST,次小生成树模板题

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K       Description Given a connected undirec ...

随机推荐

  1. RTC终于tm的通了

     ITDS(1316336566) 2014-1-16 10:34:36我们板子上用的是pcf8563默认没使用这个,用图形界面选择下这个完以后,在配置下就这两步骤ITDS(1316336566) 2 ...

  2. jQuery数组处理详解(转)

    1. $.each(array, [callback]) 遍历[常用] 解释: 不同于例遍 jQuery 对象的 $.each() 方法,此方法可用于例遍任何对象(不仅仅是数组哦~). 回调函数拥有两 ...

  3. DLL文件的使用

    一. 动态链接库 什么是动态链接库?DLL三个字母对于你来说一定很熟悉吧,它是Dynamic Link Library 的缩写形式,动态链接库 (DLL) 是作为共享函数库的可执行文件.动态链接提供了 ...

  4. mysql数据库binary log中的事件到底是什么?

    需求描述: 最近看mysql备份恢复的时候,基于时间点恢复,提到了binary log中存的是"事件" 那么到底什么是事件呢 概念解释: binary log中存的是事件(even ...

  5. 静默安装oracle 11g,环境预检查时报错,SEVERE: [FATAL] PRVF-0002 : 无法检索本地节点名

    环境描述: 操作系统:Redhat 6.6_x64 oracle:11.2.0.4 x64 问题描述: 今天在安装oracle 11g的数据库,在进行预安装环境检查的时候,报下面的错误: [oracl ...

  6. [mysql] Install/Remove of the Service Denied

    在windos 的cmd下安装mysql 在mysql的bin目录下面执行: mysqld --install 报错: 信息如下: Install/Remove of the Service Deni ...

  7. Java精选笔记_JavaBean

    JavaBean组件 初始JavaBean JavaBean是Java开发语言中一个可以重复使用的软件组件,它本质上就是一个Java类. 一个标准的JavaBean组件需要遵循一定的编码规范,具体如下 ...

  8. POJ 1384 Piggy-Bank(完全背包)

    Description Before ACM can do anything, a budget must be prepared and the necessary financial suppor ...

  9. Python 爬虫知识点 - 淘宝商品检索结果抓包分析(续二)

    一.URL分析 通过对“Python机器学习”结果抓包分析,有两个无规律的参数:_ksTS和callback.通过构建如下URL可以获得目标关键词的检索结果,如下所示: https://s.taoba ...

  10. docker学习之-什么是docker

    docker是一个用来装应用的容器,就想杯子可以装水,笔筒可以装笔,书包可以放书一样,可以把网站放到docker里,可以把任何应用放到docker里.