题意:构成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. 2018/03/27 每日一个Linux命令 之 cron

    Cron 用于配置定时任务. -- 环境为 Ubuntu16-04 -- 先说说怎么配置一个简单的定时任务.直观的可以看到效果. 之前在网上查找资料,对Shell编程不熟悉的实在是很头疼,走了不少弯路 ...

  2. elasticsearch的重启

    没有重启的操作,只有关闭了再启动的操作. ps -ef | grep elastic e表示全部的进程,f表示展示进程间的相关关系,如父子进程. 然后找到你启动es的那个账号,不是root,一般是新建 ...

  3. Balanced Lineup---poj3264线段树基础

    题目链接 求对应区间最大值与最小值的差: #include<stdio.h> #include<string.h> #include<algorithm> #inc ...

  4. 如何bitbucket上删除项目

    老外网页操作习惯不同,删除项目的按钮,我花了半天,突然瞟到delete,如下图(真他妈的隐蔽,记住这2017/3/7):

  5. 共享访问在.NET中的编程实现

    转载:http://blog.csdn.net/zhzuo/article/details/1732937 共享访问在.NET中的编程实现 发布日期:2007-08-08 | 更新日期:2009-03 ...

  6. Android中 Application的使用

    Application全局唯一,如果需要放置全局的变量,需要用到Application,类似于OC中的单例类,获者OC中的AppDelegate 第一步:创建一个AppContext继承Applica ...

  7. [py]django url 参数/reverse和HttpResponseRedirect

    参考 需要完成以下任务 - 访问http://127.0.0.1:8000/ 返回"hello maotai"或home.html - 访问http://127.0.0.1:800 ...

  8. minus查找两张表的不同项

    minus关键字的使用: select * from A minus select * from B; 上面的SQL语句返回的是表A中存在,表B中不存在的数据: 注意:1.区分不同的规则是查询的所有字 ...

  9. (C#) SQLite数据库连接字符串

    最常用的:Data Source=filename;Version=3; 自增主键: Create  test1( [id] integer PRIMARY KEY AUTOINCREMENT ,[n ...

  10. A7架构

    以Cortex-A7 MPCore processor来进行说明,这是一款主打低功耗的多核处理器,采用ARMv7-A架构,最多支持4个core. 每个core都有L1级的Cache,分为instruc ...