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. Storm系列(十一)架构分析之Supervisor-管理Worker进程的事件线程

    处理流程:   方法原型: (defn sync-processes [supervisor]) 函数说明: Supervisor是一个supervisor-data对象. 从local-state中 ...

  2. passwnger

    环境:ubuntu10.04 + nginx + passenger + ruby1.8.7 rails2.3.x #安装nginx(手动编译) $  mkdir -p /home/mouse/opt ...

  3. Codeforces Round #311 (Div. 2) D - Vitaly and Cycle(二分图染色应用)

    http://www.cnblogs.com/wenruo/p/4959509.html 给一个图(不一定是连通图,无重边和自环),求练成一个长度为奇数的环最小需要加几条边,和加最少边的方案数. 很容 ...

  4. 神奇的问题记录【SqlDataAdapter Fill DataSet】

    今天发现程序中有一张报表查询速度很慢[全条件要二分钟左右],查找相关原因,准备进行优化处理.注:报表调用存储过程,存储过程返回两个table就有以下神奇的故事: 直接将SQL语句在SSMS中执行发现全 ...

  5. SqlServer定时跑一段SQL语句

    1.请把这段SQL语句写成一个存储过程,然后需要在B上面开启 SQL Server Agent服务,如下图: 2.开启完之后,打开数据库管理工具,然后依下图所示,展开“SQL Server Agent ...

  6. hdoj 2803 The MAX【简单规律题】

    The MAX Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  7. 如何将中国知网CNKI中的文献导入EndNote X6

    如何将中国知网CNKI中的文献导入EndNote X6 下面给出具体步骤: 1.在CNKI中检索目标文献,如检索<基于Qt的三维可视化技术研究> 我喜欢在CNKI(http://www.c ...

  8. JBOSS实现RMI时注意的问题

    使用JBOSS部署EJB服务后通过RMI访问报错: javax.naming.CommunicationException: Could not obtain connection to any of ...

  9. opencv数据结构-MAT结构详解

    1.定义 OpenCV中的C结构体有 CvMat 和 CvMatND,但后续的应用中指出 CvMat 和 CvMatND 弃用了,在C++封装中用 Mat 代替,另外旧版还有一个 IplImage,同 ...

  10. 解析Systemtap

    SystemTap 的架构 让我们深入探索 SystemTap 的某些细节,理解它如何在运行的内核中提供动态探针.您还将看到 SystemTap 是如何工作的,从构建进程脚本到在运行的内核中激活脚本. ...