题目链接:

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. Python基础的练习

    ---恢复内容开始--- 简单输入输出交互. >>> name='Jame' >>> print('Hi,%s.'%name) Hi,Jame. >>& ...

  2. Android记录10--android.os.NetworkOnMainThreadException异常解决办法

    2013年11月1日小光棍节 有一段时间没有发表新的博客了,最近一直在忙着开发新浪微博客户端遇到很多问题比较头痛,比如说本篇博客要讲的NetworkOnMainThreadException这个异常, ...

  3. ORM操作mysql

    创建表和添加数据 import sqlalchemyfrom sqlalchemy import create_enginefrom sqlalchemy.ext.declarative import ...

  4. ASP.NET Web API 框架研究 Controller实例的销毁

    我们知道项目中创建的Controller,如ProductController都继承自ApiController抽象类,其又实现了接口IDisposable,所以,框架中自动调用Dispose方法来释 ...

  5. SQL Server--疑难杂症之坑爹的Windows故障转移群集

    --============================================================== 估计是春节前最后一次写博客,也估计是本年值班最后一次踩雷,感叹下成也S ...

  6. vs2017使用rdlc实现批量打印

    接着上一篇:上一篇写了安装,这篇直接搞定批量打印,A4纸横版竖版页面设计,正式开始.(我的表达不怎么好,我尽量发图片都是程序员一点就通) 一.界面展示 忽略界面设计丑 查看预览界面,因为有数据就不截全 ...

  7. C#实时检测端口占用情况

    在TCP/IP协议中,服务端需要去监听客户端的端口,开始监听,我们需要检测使用的端口是否被占用,获取系统当前使用的所有端口号,用此端口进行匹配即可. 代码如下 internal static Bool ...

  8. GoLang学习控制语句之switch

    基本结构 相比较 C 和 Java 等其它语言而言,Go 语言中的 switch 结构使用上更加灵活.它接受任意形式的表达式,例如: switch var1 { case val1: ... case ...

  9. Windows 网卡超过序列

    以前连接过别的网络,现在重新装网已经排到"网络连接9"了,连以前起过的wifi名称(ssid)都变成"*** 3"了,怎么把以前的清空呢? 这是解决办法: wi ...

  10. 设计模式《JAVA与模式》之命令模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述命令(Command)模式的: 命令模式属于对象的行为模式.命令模式又称为行动(Action)模式或交易(Transaction)模式. ...