(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],如果我们能找到一条边满足 ...
随机推荐
- HW2.16
import java.util.Scanner; public class Solution { public static void main(String[] args) { final int ...
- 自己修改select的样式(修改select右边的小三角)
CSS就可以解决,原理是将浏览器默认的下拉框样式清除,然后应用上自己的,再附一张向右对齐小箭头的图片即可. select { /*Chrome和Firefox里面的边框是不一样的,所以复写了一下*/ ...
- java利用Google Zxing实现 二维码生成与解析
1.引入zxing 2.使用下面两个类:QRCodeUtil.java和BufferedImageLuminanceSource.java 3.新建单元测试类 复制下面测试代码即可. 1.pom文件中 ...
- 成品入库过账bapi
入库过账 FUNCTION ZPP_BAPI_PRODUCT_STOCK_IN. *"---------------------------------------------------- ...
- 跟我学SpringMVC目录汇总贴、PDF下载、源码下载
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- 每天进步一点达——MySQL——myisampack
一. 简单介绍 myisampack是一个压缩使用MyISAM引擎表的工具,通常会压缩40%~70%,当须要訪问数据.server会将所须要的信息读入到内存中.所以当訪问详细记录时,性能 ...
- win 8(win 7)批处理设置IP
适合所有经常更改IP的朋友,里面的内容可用可用根据自己的需要随意修改 @rem 根据自己的需要修改带 (@rem/注释)的地方,修改完毕后直接将本文件后缀名.txt改为.bat即可使用 @rem 运行 ...
- Android开发书籍推荐
当你看到这些文字时,那么恭喜你,你可能选择了一个无限可能的方向. Android,Google出品,信誉保证,你值得深入研究. 学习一样新事物或许有多种方式,报培训班,看视频,向高手请教等等,但一本好 ...
- MySQL对于datetime 源码分析
http://tsecer.blog.163.com/blog/static/150181720160117355684/ 一.时间比较的语法分析 在mysql中,通常时间是一个必不可少的类型,而 ...
- careercup-位操作5.1
5.1 写程序使整数N中第i位到第j位的值与整数M中的相同. 题目 给定两个32位的数,N和M,还有两个指示位的数,i和j. 写程序使得N中第i位到第j位的值与M中的相同(即:M变成N的子串且位于N的 ...