题意:构成MST是否唯一

思路:

问最小生成树是否唯一。我们可以先用Prim找到一棵最小生成树,然后保存好MST中任意两个点i到j的这条路径中的最大边的权值Max[i][j],如果我们能找到一条边满足:他不是最小生成树中的边,并且它的权值等于Max[i][j],那么他就可以代替MST中的这条边,所以MST不唯一。

次小生成树总结

代码:

#include<cmath>
#include<stack>
#include<queue>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
const int N = 100+5;
const int INF = 0x3f3f3f3f;
using namespace std;
int n,m;
int mp[N][N],dis[N],pre[N];
bool vis[N];
int Max[N][N]; //最大权值边
bool is[N][N]; //是否在MST中 void init(){
int x,y,w;
memset(mp,INF,sizeof(mp));
for(int i = 0;i < m;i++){
scanf("%d%d%d",&x,&y,&w);
mp[x][y] = mp[y][x] = w;
}
memset(vis,false,sizeof(vis));
vis[1] = true;
for(int i = 2;i <= n;i++){
dis[i] = mp[i][1];
pre[i] = 1;
}
}
void Prim(){
int mincost = 0;
memset(Max,0,sizeof(Max));
memset(is,false,sizeof(is));
for(int i = 1;i <= n - 1;i++){
int MIN = INF;
int point;
for(int j = 1;j <= n;j++){
if(!vis[j] && dis[j] < MIN){
MIN = dis[j];
point = j;
}
}
vis[point] = true;
mincost += MIN;
is[point][pre[point]] = is[pre[point]][point] = true; //在MST中
for(int j = 1;j <= n;j++){
if(vis[j] && j != point){ //在MST中
Max[j][point] = Max[point][j] = max(Max[pre[point]][j],dis[point]);
//更新j到point的最大权值边
}
if(!vis[j] && dis[j] > mp[j][point]){
pre[j] = point;
dis[j] = mp[j][point];
}
}
}
//判断MST是否唯一
for(int i = 1;i <= n;i++){
for(int j = 1;j < i;j++){
if(mp[i][j] != INF && !is[i][j]){
if(mp[i][j] == Max[i][j]){
printf("Not Unique!\n");
return;
}
}
}
}
printf("%d\n",mincost);
}
int main() {
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
init();
Prim();
}
return 0;
}

POJ 1679 The Unique MST (次小生成树)题解的更多相关文章

  1. POJ 1679 The Unique MST (次小生成树)

    题目链接:http://poj.org/problem?id=1679 有t组数据,给你n个点,m条边,求是否存在相同权值的最小生成树(次小生成树的权值大小等于最小生成树). 先求出最小生成树的大小, ...

  2. POJ 1679 The Unique MST (次小生成树 判断最小生成树是否唯一)

    题目链接 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. De ...

  3. POJ 1679 The Unique MST (次小生成树kruskal算法)

    The Unique MST 时间限制: 10 Sec  内存限制: 128 MB提交: 25  解决: 10[提交][状态][讨论版] 题目描述 Given a connected undirect ...

  4. poj 1679 The Unique MST (次小生成树(sec_mst)【kruskal】)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 35999   Accepted: 13145 ...

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

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

  6. POJ 1679 The Unique MST 【最小生成树/次小生成树模板】

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

  7. POJ1679 The Unique MST —— 次小生成树

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

  8. poj 1679 The Unique MST

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

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

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

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

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

随机推荐

  1. 借用HTML5 插入视频。音频

    HTML5 规定了一种通过 video 元素来包含视频的标准方法. 插入视频 <video width="320" height="240" contro ...

  2. os模块学习+open行数

    os模块的使用https://www.cnblogs.com/juandx/p/4962089.html 注意:新建和关闭文件,可以直接用,无需os模块 python中对文件.文件夹(文件操作函数)的 ...

  3. python 面向对象 issubclass

    判断是否 他的父类 class Foo(object): pass obj = Foo() class Boo(Foo): pass class Coo(Boo): pass obj = Boo() ...

  4. python接口自动化-token参数关联登录(二)

    原文地址https://www.cnblogs.com/yoyoketang/p/9098096.html 原文地址https://www.cnblogs.com/yoyoketang/p/68866 ...

  5. 搭建私有npm私库(使用verdaccio)

    搭建 npm 离线服务器 为什么要搭建npm 服务器 原因: 公司内部开发的私有包,统一管理,方便开发和使用 安全性,由于公司内部开发的模块和一些内容并不希望其他无关人员能够看到,但是又希望内部能方便 ...

  6. matlab 怎么建立结构体数组?

    https://zhidao.baidu.com/question/537198107.html 怎么定义一个结构体数组,使数组的每个元素是一个结构体变量.像这样:a=(1,2)a(1)=struct ...

  7. 关于 WebBrowser调用百度地图API 鼠标滚轮缩放地图级别失灵的解决办法

    在桌面程序下 百度地图API的鼠标缩放地图功能可能会失灵无效! 这个原因不是API的问题 小弟试了下在WEB上面是没有问题的 于是考虑可能是WebBrowser的获取焦点问题,于是在主窗体 添加了一个 ...

  8. VS2010/MFC编程入门之三十七(工具栏:工具栏的创建、停靠与使用)

    鸡啄米在上一节教程中讲了工具栏资源及CToolBar类,本节继续讲解工具栏的相关知识,主要内容包括工具栏的创建.停靠与使用. 工具栏的使用 上一节中鸡啄米提到过,一般情况下工具栏中的按钮在菜单栏中都有 ...

  9. POJ3608

    计算两个凸包之间的最小距离,旋转卡壳法详解在旋转卡壳的用法之计算两个凸 包上的最近距离 #include <iostream> #include<cstdio> #includ ...

  10. Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) Volume 5. Dynamic Programming

    10192 最长公共子序列 http://uva.onlinejudge.org/index.php?option=com_onlinejudge& Itemid=8&page=sho ...