(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],如果我们能找到一条边满足 ...
随机推荐
- poj 3281 Dining【拆点网络流】
Dining Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11828 Accepted: 5437 Descripti ...
- mongodb基础系列——数据库查询数据返回前台JSP(一)
经过一段时间停顿,终于提笔来重新整理mongodb基础系列博客了. 同时也很抱歉,由于各种原因,没有及时整理出,今天做了一个demo,来演示,mongodb数据库查询的数据在JSP显示问题. 做了一个 ...
- 浅谈js观察者模式
观察者模式又叫发布订阅模式,它可以让多个观察者对象同时监听某一个主题对象,即在一个事件发生时,不同的对象迅速对其进行相应.就比如当又人闯红灯,不同的人对这件事迅速发起响应,当然这个比喻不太恰当,不过在 ...
- Java开发中常见的危险信号(中)
本文来源于我在InfoQ中文站原创的文章,原文地址是:http://www.infoq.com/cn/news/2013/12/common-red-flags-in-java-1 Dustin Ma ...
- GridControl 选择列、复选框全选(上)
说明: GirdControl 中添加一列,这一列不是写在数据库中的,而是代码中添加的. 图示: 底层类代码: #region GridControl 全选 /// <summary> / ...
- $.post()请求 ation请求,jsp获取的处理结果
public void write(String content, String charset) { getHttpResponse().setCharacterEncoding(charset); ...
- socket编程——一个简单的样例
从一个简单的使用TCP样例開始socket编程,其基本过程例如以下: server client ++ ...
- 【剑指Offer学习】【面试题19 :二叉树的镜像】
题目:请完毕一个函数,输入一个二叉树,该函数输出它的镜像. 二叉树结点的定义: /** * 二叉树的树结点 */ public static class BinaryTreeNode { int va ...
- POJ 3616 DP
题意:给你N的时间,M的工作时间段,每个时间段有一个权重,还有一个R,每次完成一个工作需要休息R,问最后在时间N内,最大权重是多少. 思路:很简单的DP,首先对区间的右坐标进行排序,然后直接转移方程就 ...
- ARM GCC CodeSourcery 下载地址
Sourcery G++ Lite 2011.03-42: https://sourcery.mentor.com/GNUToolchain/package8737/public/arm-none-e ...