POJ 1679 The Unique MST(判断最小生成树是否唯一)
题目链接:
http://poj.org/problem?id=1679
Description
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
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
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!
Source
/*
问题
判断最小生成树是否唯一 解题思路
利用克鲁斯卡尔算法计算出最小花费和标记每一条边,每次删除一条标记边,再进行一次克鲁斯卡尔,如果能够生成最小生
成树而且最小代价相同,说明最小生成树不唯一,否则说明最小生成树是唯一的输出最小花费。
*/
#include<cstdio>
#include<algorithm> using namespace std; struct EDGE{
int u,v,w,f;
}edge[];
int n,m;
int fa[];
int cmp(struct EDGE a,struct EDGE b){
return a.w<b.w;
}
int kruskal1();
int kruskal2();
int merge(int u,int v);
int getf(int v);
int ok(int ans); int main()
{
int T,i;
scanf("%d",&T); while(T--){
scanf("%d%d",&n,&m);
for(i=;i<m;i++){
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
edge[i].f=;
} sort(edge,edge+m,cmp);
/*for(i=0;i<m;i++){
printf("%d %d %d %d\n",edge[i].u,edge[i].v,edge[i].w,edge[i].f);
}*/ int mina=kruskal1();
//printf("%d\n",mina); if(ok(mina))
printf("%d\n",mina);
else
printf("Not Unique!\n");
}
return ;
} int ok(int ans){
int temp,i;
for(i=;i<m;i++){
if(edge[i].f){
//printf("删去 %d 这条边\n",i);
edge[i].f=-;
temp=kruskal2();
if(temp == ans)//构成最小生成树并且最小代价相同
return ; edge[i].f=;
}
}
return ;
} int kruskal1()
{
int i;
for(i=;i<=n;i++)
fa[i]=i;
int c=,sum=; for(i=;i<m;i++){
if(merge(edge[i].u,edge[i].v)){
c++;
sum += edge[i].w;
edge[i].f=;
}
if(c == n-)
break;
}
return sum;
} int kruskal2()
{
int i;
for(i=;i<=n;i++)
fa[i]=i;
int c=,sum=; for(i=;i<m;i++){
if(edge[i].f >= && merge(edge[i].u,edge[i].v)){
//printf("使用 %d 这条边 %d %d %d\n",i,edge[i].u,edge[i].v,edge[i].w);
c++;
sum += edge[i].w;
}
if(c == n-)
break;
} if(c == n-)
return sum;
else
return -;
} int merge(int u,int v){
int t1=getf(u);
int t2=getf(v);
if(t1 != t2){
fa[t2]=t1;
return ;
}
return ;
} int getf(int v){
return fa[v] == v ? v : fa[v]=getf(fa[v]);
}
POJ 1679 The Unique MST(判断最小生成树是否唯一)的更多相关文章
- poj 1679 The Unique MST 判断最小生成树是否唯一(图论)
借用的是Kruskal的并查集,算法中的一点添加和改动. 通过判定其中有多少条可选的边,然后跟最小生成树所需边做比较,可选的边多于所选边,那么肯定方案不唯一. 如果不知道这个最小生成树的算法,还是先去 ...
- poj 1679 The Unique MST (判定最小生成树是否唯一)
题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total S ...
- POJ 1679 The Unique MST 推断最小生成树是否唯一
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22715 Accepted: 8055 D ...
- 【POJ 1679 The Unique MST】最小生成树
无向连通图(无重边),判断最小生成树是否唯一,若唯一求边权和. 分析生成树的生成过程,只有一个圈内出现权值相同的边才会出现权值和相等但“异构”的生成树.(并不一定是最小生成树) 分析贪心策略求最小生成 ...
- POJ 1679 The Unique MST(最小生成树)
Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...
- POJ 1679 The Unique MST (最小生成树)
The Unique MST 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/J Description Given a conn ...
- POJ 1679 The Unique MST 【最小生成树/次小生成树模板】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- (poj)1679 The Unique MST 求最小生成树是否唯一 (求次小生成树与最小生成树是否一样)
Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...
- poj 1679 The Unique MST 【次小生成树】【模板】
题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...
随机推荐
- [mysql]当mysql查询语句查询的结果为空时,返回query结果是什么类型的呢?
php > $con = mysql_connect('localhost' , 'hnb' , 'alyHnb2015'); php > print_r($con);Resource i ...
- poj3253哈夫曼树
Fence Repair Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java ...
- php支付宝手机网页支付类实例
<?php $alipayConfig = array( 'key' => 'xxxxx', //买卖安全校验码,用于签名的32位密钥 'transport' => 'https', ...
- 深入探索AngularJS
目录 深入探索AngularJS 作用域Scope是DOM和Directives交互的抽象 Scope是POJO对象 Scope是上下文 Scope继承树 Scope附加功能 正交功能 Element ...
- 用C#中的键值对遍历数组或字符串元素的次数
代码如下: string strs = "ad6la4ss42d6s3"; Dictionary<char, int> dic = new Dictionary< ...
- AngularJs创建一个带参数的自定义方法
学习这篇之前,先要从这篇<AngularJs创建自定义Service>http://www.cnblogs.com/insus/p/6773894.html 开始. 看看: app.con ...
- 【转】OAuth2.0的refresh token
转载自http://www.html-js.com/?p=1297 最近看人人网的OAuth认证,发现他是OAuth2.0,之前一直看的是新浪的OAuth,是OAuth1.0. 二者还是有很多不同的, ...
- Spring AOP 源码分析系列文章导读
1. 简介 前一段时间,我学习了 Spring IOC 容器方面的源码,并写了数篇文章对此进行讲解.在写完 Spring IOC 容器源码分析系列文章中的最后一篇后,没敢懈怠,趁热打铁,花了3天时间阅 ...
- DS博客作业03—栈和队列
1.本周学习总结 本周学习了栈和队列两种数据结构,分别对应后进先出,先进先出两种数据操作 学会栈的特殊类型-共享栈,队列的特殊类型-循环队列的一系列操作 学会熟练使用栈和队列的STL容器,使代码简洁 ...
- Linux基础命令-cd
cd 作用:切换路径 切换至家目录 $ cd $ cd~ 在上一个目录和当前目录来回切换 $ cd - 切换至某用户的家目录 # cd ~ # pwd /root # cd ~quail #pwd / ...