POJ 1679 The Unique MST (次小生成树)题解
题意:构成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 (次小生成树)题解的更多相关文章
- POJ 1679 The Unique MST (次小生成树)
题目链接:http://poj.org/problem?id=1679 有t组数据,给你n个点,m条边,求是否存在相同权值的最小生成树(次小生成树的权值大小等于最小生成树). 先求出最小生成树的大小, ...
- POJ 1679 The Unique MST (次小生成树 判断最小生成树是否唯一)
题目链接 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. De ...
- POJ 1679 The Unique MST (次小生成树kruskal算法)
The Unique MST 时间限制: 10 Sec 内存限制: 128 MB提交: 25 解决: 10[提交][状态][讨论版] 题目描述 Given a connected undirect ...
- poj 1679 The Unique MST (次小生成树(sec_mst)【kruskal】)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 35999 Accepted: 13145 ...
- poj 1679 The Unique MST 【次小生成树】【模板】
题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...
- POJ 1679 The Unique MST 【最小生成树/次小生成树模板】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- POJ1679 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
题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...
- 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 ...
随机推荐
- Shuffle'm Up---poj3087
题目链接 题意:有两个字符串s1,s2:经过交叉问是否得到字符串s,不能输出-1,能就输出交叉的次数 每次重组的串都是s2开始,重新组合时,前面一半是s1,后一半s2: #include<std ...
- requests库的get请求(加上head,加上get参数请求)
#coding:utf-8 # 导入requests import requests # 构建url url = 'http://www.baidu.com' # 发送请求,获取响应 # respon ...
- mysql 权限管理 对所有库 所有表 授权 *.*
对miek这个账号localhost 授予了所有库,所表的select权限 mysql> grant select on *.* to 'mike'@'localhost'; Query OK, ...
- Linuxer-"Linux开发人员自己的媒体"第五月稿件和赠书名单
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/juS3Ve/article/details/78859630 Linuxer已经从一个单纯的读者服务 ...
- [django实践]投票app
code: https://github.com/lannyMa/toupiao polls app介绍 这个例子来源于django官网,恰好2.x版本有中文版. https://docs.djang ...
- [LeetCode] 796. Rotate String_Easy **KMP
We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...
- [LeetCode] 197. Rising Temperature_Easy tag: SQL
Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to ...
- win10 + cuda(v9.0) 安装TensorFlow-gpu版
之前在实习公司的电脑上装过TensorFlow-gpu,那时候很快就装好了.但在自己的笔记本上装时,却搞了很久... 一部分原因是因为用校园网下载cuda toolkit 和cudnn ,总是在最后时 ...
- 如何用softmax和sigmoid来做多分类和多标签分类
首先,说下多类分类和多标签分类的区别 多标签分类:一个样本可以属于多个类别(或标签),不同类之间是有关联的,比如一个文本被被划分成“人物”和“体育人物”两个标签.很显然这两个标签不是互斥的,而是有关联 ...
- Docker深入浅出3-容器管理
docker客户端非常简单,我们可以直接输入docker命令来查看Docker客户端所有的命令项 [root@admin-fxr ~]# docker Usage: docker COMMAND A ...