题目链接:

http://poj.org/problem?id=1679

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!

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(判断最小生成树是否唯一)的更多相关文章

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

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

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

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

  3. POJ 1679 The Unique MST 推断最小生成树是否唯一

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

  4. 【POJ 1679 The Unique MST】最小生成树

    无向连通图(无重边),判断最小生成树是否唯一,若唯一求边权和. 分析生成树的生成过程,只有一个圈内出现权值相同的边才会出现权值和相等但“异构”的生成树.(并不一定是最小生成树) 分析贪心策略求最小生成 ...

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

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...

  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 【最小生成树/次小生成树模板】

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

  8. (poj)1679 The Unique MST 求最小生成树是否唯一 (求次小生成树与最小生成树是否一样)

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...

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

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

随机推荐

  1. Ajax登录用户名密码

    <script src="http://code.jquery.com/jquery-latest.js"></script>#引入jQuery#当点击函数 ...

  2. cxGrid控件过滤排序和TClientDataSet同步

    https://www.cnblogs.com/false/archive/2013/02/24/2924240.html procedure TReport10Form.cxGridViewData ...

  3. headpq

    从一个集合中获得最大或者最小的N个元素列表 http://python3-cookbook.readthedocs.io/zh_CN/latest/c01/p04_find_largest_or_sm ...

  4. Elasticsearch 系列2 --- 安装elasticsearch-head管理工具

    elasticsearch-head是elasticsearch的一个管理页面,它的官网是https://github.com/mobz/elasticsearch-head 通过官网我们得知,ES5 ...

  5. MySQL--REPALCE INTO操作

    REPLACE INTO语法是MySQL数据库独特的扩展语法,可以提供“不存在即插入,存在即更新”的操作,MySQL官方文档解析其算法为: 1.尝试进行INSER 操作 2.如果INSERT 失败,则 ...

  6. Python——面向对象(初级篇)

    1.如何创建类 class 类名: pass 2.如何创建方法 构造方法: __init__ obj = 类名('a1') 普通方法: obj = 类名('xxx') obj.普通方法名() 3.图解 ...

  7. 522. Longest Uncommon Subsequence II

    Given a list of strings, you need to find the longest uncommon subsequence among them. The longest u ...

  8. 友链&&日记

    上面友链,下面日记 友人链 最喜欢galgameの加藤聚聚 初三一本&&\(ACG\)姿势比我还丰厚的yx巨巨 更喜欢galgame的shadowice czx ZigZag胖胖 文文 ...

  9. mysql日期时间函数使用总结

     获取函数 mysql默认的时间格式: yyyy-MM-dd 或者 yyyy-MM-dd HH:mm:ss 1. Date() 返回日期部分, date('2018-02-14 17:03:04') ...

  10. MySQL utilities介绍&出现 No module named utilities

    目录 安装 mysqlreplicate mysqlrplcheck mysqlrplshow mysqlrpladmin mysqlfailover mysqldbcompare 详细介绍 mysq ...