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 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...
随机推荐
- F - Cookies Piles
Description The kids in my son's kindergarten made Christmas cookies with their teacher, and piled t ...
- Delphi 内进行音量控制及静音
unit UMute; interface uses MMSystem, Dialogs; Type TDeviceName = (Master, Microphone, WaveOut, Syn ...
- Delphi 的多线程使用已经很简单了
先看一个非多线程的例子, 代码执行时不能进行其它操作(譬如拖动窗体): {自定义方法: 在窗体上绘制...} procedure MyMethod; var i: Integer; begin ...
- 键'attachdbfilename'的值无效。
---恢复内容开始--- ---恢复内容结束---
- 【转】[MySQL复制异常]Cannot execute statement: impossible to write to binary log since statement is in row for
MySQL复制错误]Last_Errno: 1666 Last_Error: Error executing row event: 'Cannot execute statement: imposs ...
- linux安装mysql详细步骤
最近买了个腾讯云服务器,搭建环境. 该笔记用于系统上未装过mysql的干净系统第一次安装mysql.自己指定安装目录,指定数据文件目录. linux系统版本: CentOS 7.3 64位 安装源文件 ...
- UVA10829 L-Gap Substrings(后缀数组+ST表)
后缀数组+ST表. 代填的坑. \(Code\ Below:\) #include <bits/stdc++.h> #define ll long long using namespace ...
- Git-遇到的问题以及解决方法
1.将本地内容推送到远程仓库后,远程仓库里的文件夹不可点击 原因:在本地添加文件夹A时,又在A里使用了git init命令 解决:删除文件夹A,再重新添加过 2.其他人推送不了内容到远程仓库 原因:权 ...
- Python大法之告别脚本小子系列—各类URL采集器编写
本文作者:i春秋签约作家——阿甫哥哥 系列文章专辑:https://bbs.ichunqiu.com/forum.php?mod=collection&action=view&ctid ...
- Linux巩固记录(4) 运行hadoop 2.7.4自带demo程序验证环境
本节主要使用hadoop自带的程序运行demo来确认环境是否正常 1.首先创建一个input.txt文件,里面任意输入些单词,有部分重复单词 2.将input文件拷贝到hdfs 3.执行hadoop程 ...