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 求最小生成树是否唯一 (求次小生成树与最小生成树是否一样)的更多相关文章

  1. poj 1679 The Unique MST 【次小生成树】【模板】

    题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...

  2. poj 1679 The Unique MST (次小生成树模板题)

    Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...

  3. poj 1679 The Unique MST(唯一的最小生成树)

    http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  4. poj 1679 The Unique MST (判定最小生成树是否唯一)

    题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total S ...

  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

    题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...

  7. 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 ...

  8. poj 1679 The Unique MST【次小生成树】

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24034   Accepted: 8535 D ...

  9. [ An Ac a Day ^_^ ][kuangbin带你飞]专题八 生成树 POJ 1679 The Unique MST

    求最小生成树是否唯一 求一遍最小生成树再求一遍次小生成树 看看值是否相等就可以 #include<cstdio> #include<iostream> #include< ...

  10. POJ 1679 The Unique MST (次小生成树)题解

    题意:构成MST是否唯一 思路: 问最小生成树是否唯一.我们可以先用Prim找到一棵最小生成树,然后保存好MST中任意两个点i到j的这条路径中的最大边的权值Max[i][j],如果我们能找到一条边满足 ...

随机推荐

  1. tail -f logfile.log 一直监控某个文件,若该文件有改动,立即在屏幕上输出

    tail -f logfile.log 可以一直监控某个文件,只要文件有改动,就立即在屏幕上输出

  2. error C2589: “(”: “::”右边的非法标记 error C2059: 语法错误 : “::

    1. 错误输出 ./zlibrary/ui/src/win32/w32widgets/W32VBorderBox.cpp(114) : error C2589: “(”: “::”右边的非法标记    ...

  3. SQLite 入门教程(二)创建、修改、删除表 (转)

    转于 SQLite 入门教程(二)创建.修改.删除表 一.数据库定义语言 DDL 在关系型数据库中,数据库中的表 Table.视图 View.索引 Index.关系 Relationship 和触发器 ...

  4. 实战RPM包制作

    在开发中经常会用到一些rpm包,但是一直没有自己手动制作过.今天在制作的时候意外地还解决了自己以前一直困惑的问题,就是怎么制作rpm debuginfo包,类似CentOS官网那样的debuginfo ...

  5. Redis学习手册(String数据类型)

    一.概述: 字符串类型是Redis中最为基础的数据存储类型,它在Redis中是二进制安全的,这便意味着该类型可以接受任何格式的数据,如JPEG图像数据或Json对象描述信息等.在Redis中字符串类型 ...

  6. 数据绑定(八)使用Binding的RelativeSource

    当一个Binding有明白的数据来源时能够通过为Source或ElementName赋值的办法让Binding与之关联,有的时候因为不能确定Source的对象叫什么名字,但知道它与作为Binding目 ...

  7. 容器大小的改变以及容器操作可能使迭代器失效、vector对象的容量变化

    1 改变容器的大小 我们可以使用resize来增加或缩小容器,与往常一样,array不支持resize.如果当前大小大于所要求的大小,容器后面的元素会被删除:如果当前大小小于新大小,会将新元素添加到容 ...

  8. linux上安装memcached

    我的版本为Centos Release 5.3 (Final)使用这个命令可以知道你的Linux版本1.cat /etc/redhat-release首先要安装libevent库.cd /usr/lo ...

  9. [转] JAVA正则表达式:Pattern类与Matcher类详解(转)

    java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包.它包括两个类:Pattern和 Matcher Pattern 一个Pattern是一个正则表达式经编译后的 ...

  10. 自定义URL Scheme完全指南

    iPhone / iOS SDK 最酷的特性之一就是应用将其自身”绑定”到一个自定义 URL scheme 上,该 scheme 用于从浏览器或其他应用中启动本应用. 注册自定义 URL Scheme ...