The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 24034   Accepted: 8535

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! 今天看了一天的次小生成树,还是有几个细节不是太理解,今晚再跟舍友探讨一下
题意:判断是不是有唯一的最小生成树(即连接所有点的边的权值之和是最小且唯一的)如果是则输出最小生成树(mst)的权值,否则输出 Not Unique!
题解:先利用prime算法求出最小生成树,在prime求MST的过程中 用数组存储MST里面任意两点间的唯一的路中 权值最大的那条边的权值。
最后枚举不在MST里面的边<i,,j>,判断<i,j>的权值 是否 和 MST里面 i 到 j 的最大权值相等,只要有一条边满足就可以说明MST不唯一。
(因为我们可以用这条不在MST的边来 代替 在MST的边,这样MST肯定不唯一)
     MST更新:MST[ next ][ j ] = max(MST[ set[ next ] ][ j ], low[ next ])。( j 属于MST里面的点)
#include<stdio.h>
#include<string.h>
#define MAX 110
#define max(x,y)(x>y?x:y)
#define INF 0x3f3f3f3f
int map[MAX][MAX],vis[MAX],set[MAX];
int low[MAX];
int mst[MAX][MAX];//记录 mst中i到j的最大权值
int inmst[MAX][MAX];//判断边是否在mst中
int n,m;
void init()//初始化
{
int i,j;
for(i=0;i<=n;i++)
{
for(j=0;j<=n;j++)
{
if(i==j)
map[i][j]=0;
else
map[i][j]=INF;
}
}
}
void getmap()//建图
{
int i,a,b,c;
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(map[a][b]>c)
map[a][b]=map[b][a]=c;
}
}
void prim()
{
int i,j,min,mindis=0,next;
memset(mst,0,sizeof(mst));
memset(inmst,0,sizeof(inmst));
for(i=1;i<=n;i++)
{
low[i]=map[1][i];
vis[i]=0;
set[i]=1;//所有点的前驱都是起点
}
vis[1]=1;
for(i=1;i<n;i++)
{
min=INF;
for(j=1;j<=n;j++)
{
if(!vis[j]&&min>low[j])
{
min=low[j];
next=j;
}
}
mindis+=min;
vis[next]=1;
int fa=set[next];//求出当前新起点的前驱
inmst[fa][next]=inmst[next][fa]=1;
for(j=1;j<=n;j++)
{
if(vis[j]&&j!=next)
mst[j][next]=mst[next][j]=max(mst[fa][j],low[next]);
if(!vis[j]&&low[j]>map[next][j])
{
low[j]=map[next][j];
set[j]=next;//以新起点为所有点的新前驱
}
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<i;j++)
{
if(map[i][j]!=INF&&!inmst[i][j])//不在最小生成树中且存在的边
{
if(map[i][j]==mst[i][j])//代表可以有边替代最小生成树中的边(最小生成树不唯一)
{
printf("Not Unique!\n");
return ;
}
}
}
}
printf("%d\n",mindis);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
getmap();
prim();
}
return 0;
}

  

poj 1679 The Unique MST【次小生成树】的更多相关文章

  1. POJ 1679 The Unique MST (次小生成树)

    题目链接:http://poj.org/problem?id=1679 有t组数据,给你n个点,m条边,求是否存在相同权值的最小生成树(次小生成树的权值大小等于最小生成树). 先求出最小生成树的大小, ...

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

    题目链接 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. De ...

  3. POJ 1679 The Unique MST (次小生成树kruskal算法)

    The Unique MST 时间限制: 10 Sec  内存限制: 128 MB提交: 25  解决: 10[提交][状态][讨论版] 题目描述 Given a connected undirect ...

  4. poj 1679 The Unique MST (次小生成树(sec_mst)【kruskal】)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 35999   Accepted: 13145 ...

  5. poj 1679 The Unique MST 【次小生成树】【模板】

    题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...

  6. POJ 1679 The Unique MST 【最小生成树/次小生成树模板】

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

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

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

  8. poj 1679 The Unique MST

    题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...

  9. poj 1679 The Unique MST(唯一的最小生成树)

    http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

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

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

随机推荐

  1. 自己学习过程中关于以后有可能用到的技术的备份,微信广告滑屏组件 iSlider

    转载: iSlider 是个非常平滑的滑块,支持移动端 WebApp,HTML5App 和混合型的 App. iSlider是移动端的滑动组件的最佳解决方案.他和普通的web 端的滑动插件有很大不同, ...

  2. HDU 3006 The Number of set(位运算 状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3006 题目大意:给定n个集合,每个集合都是由大于等于1小于等于m的数字组成,m最大为14.由给出的集合 ...

  3. v880 debug

    zte v880手机,ubuntu中配置真机调试, 1.开启手机调试模式2.增加/etc/udev/rules.d/51-android.rules. 内容如下:SUBSYSTEM=="us ...

  4. bootstrap轮播组件,大屏幕图片居中效果

    在慕课网学习bootstrap轮播组件的时候,了解到轮播的图片都放在了类名为item下的img中 视频中老师对图片自适应采用给图片img设置width=100%完成,然而这样自适应处理图片在不同屏幕中 ...

  5. DevExpress GridControl 中下拉框联动效果的实现(及支持文本框录入情况)

    先解释一下标题: grid中的某一列默认为文本框,根据需要动态的变更为下拉框,且支持动态变更数据源 需求是这样的: 有一些参数(A),这些参数又分别对应另外的参数(B),所以,先把A作为一列,B根据A ...

  6. 【随记】修复TortoiseGit文件夹和文件状态图标不显示问题

    一. 运行环境: 操作系统 Windows 10 64bit TortoiseGit (2.2.0.0) 64bit msysgit(2.9.2.1) 64bit 注意:请确保环境正确,软件的位数相匹 ...

  7. 浏览器阻止window.open的解决方案

    先分析一下浏览器为什么会阻止window.open吧:用户主动去触发事件的浏览器不会阻止,什么是用户主动触发的呢?就是当用户去点击的一瞬间就弹出这种浏览器是不会阻止的,如果是通过setTimeout定 ...

  8. Centos6.5最小化安装:配置网络和自启动服务

    参考http://www.111cn.net/sys/CentOS/56456.htm 1.开启网络连接,禁止IPV6启用 1.开启网络连接 vi  /etc/sysconfig/network-sc ...

  9. Xcode7之后常见问题整理-b

    一.Xcode7,iOS9之后传出来的什么Xcode有鬼,被植入代码片段什么的,可以看看,了解一下http://drops.wooyun.org/news/8864 二.bitcode问题--未正确设 ...

  10. Quartz源码阅读

    基于Quartz1.8.5的源码解读 首先看一个demo //简单的任务管理类 //QuartzManager.java package quartzPackage; import java.text ...