The Unique MST                                                                       
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 24058   Accepted: 8546

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>
const int INF=0x3f3f3f3f;
const int MAXN=;
int vis[MAXN],map[MAXN][MAXN],low[MAXN];
int ans,N,answer;
void prime(){
memset(vis,,sizeof(vis));
int flot=,temp,k;
answer=;
ans=;
vis[]=;
for(int i=;i<=N;i++)low[i]=map[][i];
for(int i=;i<=N;i++){
temp=INF;
for(int j=;j<=N;j++)
if(!vis[j]&&temp>low[j])temp=low[k=j];
if(temp==INF){
if(flot!=N)ans=;//因为这题本来就是联通的,所以这句话可以省略;
// printf("flot=%d,N=%d\n",flot,N);
break;
}
int x=;
for(int j=;j<=N;j++){
if(vis[j]&&map[k][j]==temp)x++;
if(x>)break;
}//这个for循环的作用就是,当前已经找到距离这个图最小的点了,然后判断到这个最小点的距离为temp也就是最小值的点有几个,如果点数大于一则有多个最小生成树;
if(x>){
ans=;
break;
}
answer+=temp;
vis[k]=;
flot++;
for(int j=;j<=N;j++){
if(!vis[j]&&low[j]>map[k][j])low[j]=map[k][j];
}
}
}
int main(){
int T,M,a,b,c;
scanf("%d",&T);
while(T--){
memset(map,INF,sizeof(map));
scanf("%d%d",&N,&M);
while(M--){
scanf("%d%d%d",&a,&b,&c);
if(c<map[a][b])map[a][b]=map[b][a]=c;
}
prime();
if(ans)printf("%d\n",answer);
else printf("Not Unique!\n");
}
return ;
}

The Unique MST (判断是否存在多个最小生成树)的更多相关文章

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

    <题目链接> 题目大意: 给定一张无向图,判断其最小生成树是否唯一. 解题分析: 对图中每条边,扫描其它边,如果存在相同权值的边,则标记该边:用kruskal求出MST. 如果MST中无标 ...

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

    借用的是Kruskal的并查集,算法中的一点添加和改动. 通过判定其中有多少条可选的边,然后跟最小生成树所需边做比较,可选的边多于所选边,那么肯定方案不唯一. 如果不知道这个最小生成树的算法,还是先去 ...

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

    http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its minimum s ...

  4. [poj1679]The Unique MST(最小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28207   Accepted: 10073 ...

  5. poj 1679 The Unique MST

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

  6. POJ 1679 The Unique MST (最小生成树)

    The Unique MST 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/J Description Given a conn ...

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

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

  8. poj 1679 The Unique MST【次小生成树】

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

  9. POJ 1679 The Unique MST (次小生成树)题解

    题意:构成MST是否唯一 思路: 问最小生成树是否唯一.我们可以先用Prim找到一棵最小生成树,然后保存好MST中任意两个点i到j的这条路径中的最大边的权值Max[i][j],如果我们能找到一条边满足 ...

随机推荐

  1. 【Sqlserver清空数据库中所有表数据】

    脚本: CREATE PROCEDURE sp_DeleteAllData AS EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT AL ...

  2. JIRA官方:为什么要用JIRA?

    因为你有各种事务 工作中总是有各种事务要去处理,而这些事务不仅仅是代码中的Bug.这些事务充斥在你的收件箱中,各种想法散落在 Excel表格里,需求隐藏在原有的业务系统中.使用JIRA可以轻松捕捉和管 ...

  3. 查看实时公网ip

    icanhazip.com 使您在任何地方知道你的公网IP地址 icanhazip.com 使你在任何地方知道你的公网IP地址 icanhazip.com 使你在任何地方知道你的公网IP地址 ican ...

  4. #include <boost/regex.hpp>

    boost C++的正则表达式库boost.regex可以应用正则表达式于C++.正则表达式大大减轻了搜索特定模式字符串的负担,在很多语言中都是强大的功能. boost.regex库中两个最重要的类是 ...

  5. Connect the Cities(prime)

    Connect the Cities Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) ...

  6. GetBuffer与ReleaseBuffer的用法,CString剖析

    转载: http://blog.pfan.cn/xman/43212.html GetBuffer()主要作用是将字符串的缓冲区长度锁定,releaseBuffer则是解除锁定,使得CString对象 ...

  7. zendStudio安装Xdebug项目断点调试

    1,首先安装xdebug插件 传送门 2,配置php.ini文件如下: [XDebug] xdebug.profiler_append = xdebug.profiler_enable = xdebu ...

  8. MySQL安全问题

    使用MySQL,安全问题不能不注意.以下是MySQL提示的23个注意事项:1.如果客户端和服务器端的连接需要跨越并通过不可信任的网络,那么就需要使用SSH隧道来加密该连接的通信.2.用set pass ...

  9. javascript中的with

    with语句主要用来对一个对象操作多个属性,使代码简洁易读. 语法: with(object) statements object是新的默认对象,statements是一个或多个语句 例如: var ...

  10. smarty之缓存笔记

    smarty缓存技术 在smarty中,缓存分为:普通缓存,单模版都缓存,局部缓存. 缓存:1:首选打开缓存配置项:$smarty->caching=true; 2:缓存生命周期的配置选项:$s ...