(poj)1679 The Unique MST 求最小生成树是否唯一 (求次小生成树与最小生成树是否一样)
Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition (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:
. V' = V.
. T is connected and acyclic. Definition (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 ( <= t <= ), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m ( <= n <= ), 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 Sample Output Not Unique!
题意:问是否有一条唯一的最小生成树
方法:先求最小生成树,再求次小生成树,看是否相同,如相同则不唯一,否则唯一
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include <math.h>
#include<queue>
#define ll long long
#define INF 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof(a));
#define N 111
int Map[N][N],vis[N],used[N][N],f[N][N];
int s[N][N],es[N],dis[N];
int ans1,ans2,n;
using namespace std;
void init()
{
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
Map[i][j]=i==j?:INF;
}
met(vis,);///标记是否用过
met(s,);///标记两点是否连通
met(f,);
met(es,-);///你找的该点的上一个点,就是父节点
met(used,);///i,j这条边是最小生成树上的边
}
void prim()///查找最小生成树
{
ans1=;
for(int i=; i<=n; i++)
{
dis[i]=Map[][i];
if(dis[i]!=INF)
es[i]=;
}
vis[]=;
for(int i=; i<n; i++)
{
int an=INF,k=-;
for(int j=; j<=n; j++)
{
if(!vis[j] && an>dis[j])
an=dis[k=j];
}
if(k==-)
return ;
ans1+=an;
used[k][es[k]]=used[es[k]][k]=;
for(int j=; j<=n; j++)
{
if(vis[j])
{
f[j][k]=max(f[j][es[k]],Map[es[k]][k]);
f[k][j]=f[j][k];
} }
vis[k]=;
for(int j=; j<=n; j++)
{
if(!vis[j] && dis[j]>Map[k][j])
{
dis[j]=Map[k][j];
es[j]=k;
}
}
}
}
void sond()///从最小生成树上减去一条边加上另一条较小边,生成次小生成树
{
ans2=INF;
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
if(s[i][j] && !used[i][j] && ans1+Map[i][j]-f[i][j]<ans2)
ans2=ans1+Map[i][j]-f[i][j];
}
}
} int main()
{
int t,m,a,b,l;
scanf("%d",&t);
while(t--)
{
ans1=ans2=;
scanf("%d %d",&n,&m);
init();
for(int i=; i<m; i++)
{
scanf("%d %d %d",&a,&b,&l);
Map[a][b]=Map[b][a]=l;
s[a][b]=s[b][a]=;
}
prim();
sond();
if(ans1==ans2)
printf("Not Unique!\n");
else
printf("%d \n",ans1);
}
return ;
}
(poj)1679 The Unique MST 求最小生成树是否唯一 (求次小生成树与最小生成树是否一样)的更多相关文章
- poj 1679 The Unique MST 【次小生成树】【模板】
题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...
- poj 1679 The Unique MST (次小生成树模板题)
Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...
- poj 1679 The Unique MST(唯一的最小生成树)
http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
- 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(最小生成树)
Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...
- poj 1679 The Unique MST
题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...
- poj 1679 The Unique MST 【次小生成树+100的小数据量】
题目地址:http://poj.org/problem?id=1679 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 Outpu ...
- poj 1679 The Unique MST【次小生成树】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24034 Accepted: 8535 D ...
- [ An Ac a Day ^_^ ][kuangbin带你飞]专题八 生成树 POJ 1679 The Unique MST
求最小生成树是否唯一 求一遍最小生成树再求一遍次小生成树 看看值是否相等就可以 #include<cstdio> #include<iostream> #include< ...
- POJ 1679 The Unique MST (次小生成树)题解
题意:构成MST是否唯一 思路: 问最小生成树是否唯一.我们可以先用Prim找到一棵最小生成树,然后保存好MST中任意两个点i到j的这条路径中的最大边的权值Max[i][j],如果我们能找到一条边满足 ...
随机推荐
- Mysql常用操作记录
在linux平台中相关的MySql操作 打开Mysql mysql -uroot -p //-u后边为用户名,-p后边为密码 1:使用SHOW语句找出在服务器上当前存在什么数据库:mysql& ...
- Zookeeper集群安装详解
Zookeeper的角色 Zookeeper集群搭建 要求:服务器集群规模不小于3个节点,各服务器之间系统时间要保持一致! 安装步骤 1.在h1节点解压,目录改名. tar –zxvf zooke ...
- 代码修改mysql字符
为了不再显示的时候乱码,要修改数据库默认编码.以下以GBK编码页面为例进行说明: 1.修改MYSQL的配置文件:my.ini里面修改default-character-set=gbk2.代码运行时修改 ...
- 通过分析 JDK 源代码研究 Hash 存储机制
通过 HashMap.HashSet 的源代码分析其 Hash 存储机制 实际上,HashSet 和 HashMap 之间有很多相似之处,对于 HashSet 而言,系统采用 Hash 算法决定集合元 ...
- storm的特性
storm的特性 Storm 是一个开源的分布式实时计算系统,可以简单.可靠地处理大量的数据流. Storm支持水平扩展,具有高容错性,保证每个消息都会得到处理,而且处理速度很快(在一个小集群中,每个 ...
- zabbix邮件报警脚本
#!/usr/bin/python #coding:utf-8 import smtplib from email.mime.text import MIMEText import sys mail_ ...
- iOS 数组越界 Crash加工经验
我们先来看看有可能会出现的数组越界Crash的地方. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSInd ...
- Centos下的IO监控与分析
近期要在公司内部做个Linux IO方面的培训, 整理下手头的资料给大家分享下 各种IO监视工具在Linux IO 体系结构中的位置 源自 Linux Performance and Tuni ...
- 硬菜点播台 | MySQL阿里实践经典案例之参数调优最佳实践
http://mp.weixin.qq.com/s?__biz=MzA4NjI4MzM4MQ%3D%3D&mid=512708319&idx=1&sn=6af5f424d7cd ...
- Java为什么会超时
java程序,最终是转成c运行的,我们写的程序会转成c代码,并且由底层的c程序调用,可以理解成我们程序最后变成被调用的c函数 底层的C程序是java虚拟机,由它读取我们的.class文件,翻译成c ...