判断MST是不是唯一的 如果是唯一的 就输出最小的权值和 如果不是唯一的 就输出Not Unique!

次小生成树就是第二小生成树  如果次小生成树的权值和MST相等  那么MST就不是唯一的

法一:

先求出最小的权值和 然后一条边一条边的删
先标记MST中所使用的边 删边就是屏蔽这条边后 再对剩下的边(不管这些边是否被标记)求MST 如果最后的权值和 与开始算出的最小的那个 相等 就说明不是唯一的

Sample Input

2 //T
3 3 //n m
1 2 1// u v w
2 3 2
3 1 3
4 4
1 2 2
2 3 2
3 4 2
4 1 2
Sample Output

3
Not Unique!

Kruskal

 # include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# define LL long long
using namespace std ; int n ;
const int MAXN=;//最大点数
const int MAXM=;//最大边数
int F[MAXN];//并查集使用
int del[MAXM] ;
struct Edge
{
int u,v,w;
int tag ;
}edge[MAXM];//存储边的信息,包括起点/终点/权值 int tol;//边数,加边前赋值为0
void addedge(int u,int v,int w)
{ edge[tol].u=u;
edge[tol].v=v;
edge[tol].tag = ;
edge[tol++].w=w;
}
bool cmp(Edge a,Edge b)
{//排序函数,讲边按照权值从小到大排序
return a.w<b.w;
}
int find(int x)
{
if(F[x]==-)return x;
else return F[x]=find(F[x]);
}
int Kruskal(int d)//传入点数,返回最小生成树的权值,如果不连通返回-1
{
memset(F,-,sizeof(F)); int cnt=;//计算加入的边数
int ans=;
for(int i=;i<tol;i++)
{
if (i == d) //屏蔽id为d的这一条边
continue ;
int u=edge[i].u;
int v=edge[i].v;
int w=edge[i].w;
int t1=find(u);
int t2=find(v);
if(t1!=t2)
{
ans+=w;
F[t1]=t2;
cnt++;
edge[i].tag = ;
}
if(cnt==n-)break;
}
if(cnt<n-)return -;//不连通
else return ans;
} int main()
{ // freopen("in.txt","r",stdin) ;
int m ;
int T ;
scanf("%d" , &T) ;
while(T--)
{
scanf("%d %d" , &n , &m) ;
int i ;
int u , v , w ;
tol = ;
while(m--)
{
scanf("%d %d %d" , &u , &v , &w) ;
addedge(u , v , w) ;
} sort(edge,edge+tol,cmp);
int ans = Kruskal(-) ; int k = ;
for (i = ; i < tol ; i++)
{
if (edge[i].tag == )
{
del[k] = i ;
k++ ;
}
}
bool flag = ;
int t_ans ;
for (i = ; i < k ; i++)
{
t_ans = Kruskal(del[i]) ;
if (t_ans == ans)
{
flag = ;
break ;
}
}
if (flag)
printf("Not Unique!\n") ;
else
printf("%d\n" , ans) ; }
return ;
}

法二 : Prim

 /*
* 次小生成树
* 求最小生成树时,用数组Max[i][j]来表示MST中i到j最大边权
* 求完后,直接枚举所有不在MST中的边,替换掉最大边权的边,更新答案
* 点的编号从0开始
*/
# include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# define LL long long
using namespace std ; int n ;
const int MAXN=;
const int INF=0x3f3f3f3f;
bool vis[MAXN];
int lowc[MAXN];
int pre[MAXN];
int Max[MAXN][MAXN];//Max[i][j]表示在最小生成树中从i到j的路径中的最大边权
bool used[MAXN][MAXN];
int cost[MAXN][MAXN];
int Prim()
{
int ans=;
memset(vis,false,sizeof(vis));
memset(Max,,sizeof(Max));
memset(used,false,sizeof(used));
vis[]=true;
pre[]=-;
for(int i=;i<n;i++)
{
lowc[i]=cost[][i];
pre[i]=;
}
lowc[]=;
for(int i=;i<n;i++)
{
int minc=INF;
int p=-;
for(int j=;j<n;j++)
if(!vis[j]&&minc>lowc[j])
{
minc=lowc[j];
p=j;
}
if(minc==INF)return -;
ans+=minc;
vis[p]=true;
used[p][pre[p]]=used[pre[p]][p]=true;
for(int j=;j<n;j++)
{
if(vis[j])Max[j][p]=Max[p][j]=max(Max[j][pre[p]],lowc[p]);
if(!vis[j]&&lowc[j]>cost[p][j])
{
lowc[j]=cost[p][j];
pre[j]=p;
}
}
}
return ans;
} int smst(int ans)
{
int Min=INF;
for(int i=;i<n;i++)
for(int j=i+;j<n;j++)
if(cost[i][j]!=INF && !used[i][j])
{
Min=min(Min,ans+cost[i][j]-Max[i][j]);
}
if(Min==INF)return -;//不存在
return Min;
} int main()
{
// freopen("in.txt","r",stdin) ;
int T;
int m;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
int u,v,w;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
{
if(i==j)cost[i][j]=;
else cost[i][j]=INF;
}
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
u--;v--;
cost[u][v]=cost[v][u]=w;
}
int ans=Prim();
if(ans==-)
{
printf("Not Unique!\n");
continue;
}
if(ans==smst(ans))printf("Not Unique!\n");
else printf("%d\n",ans);
}
return ;
}

poj 1679 判断MST是不是唯一的 (次小生成树)的更多相关文章

  1. poj 1679 判断最小生成树是否唯一

    /* 只需判断等效边和必选边的个数和n-1的关系即可 */ #include<stdio.h> #include<stdlib.h> #define N 110 struct ...

  2. POJ 1679 判断最小树是否唯一

    题意:       给你一个图,问你最小树是否唯一,唯一则输出最小数的权值,不唯一输出Not Unique! 思路:      题目问的是最小树是否唯一,其实也就是在问次小树是否等于最小树,如果等于则 ...

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

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

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

    题目链接: http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its min ...

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

    http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its minimum s ...

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

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

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

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

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

  9. POJ1679The Unique MST(次小生成树)

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

随机推荐

  1. HDU - 3980 Paint Chain(SG函数)

    https://vjudge.net/problem/HDU-3980 题意 一串长度为n的柱子,每个人只能给连续的珠子涂色,涂过的不能再涂,不能涂的人就输了,问最后谁获胜. 分析 第一个人先涂m个, ...

  2. windows Zookeeper本地服务化

    ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一致性服务的软件,提供的功 ...

  3. python学习笔记9-单元测试unittest

    Python中有一个自带的单元测试框架是unittest模块,用它来做单元测试,它里面封装好了一些校验返回的结果方法和一些用例执行前的初始化操作. 在说unittest之前,先说几个概念: TestC ...

  4. Ubuntu 16.04及以上 安装/卸载 Docker-CE

    前言 本文仅针对Ubuntu 18.10.18.04.16.04的x86_64的OS与架构下的Docker-CE的安装 卸载老板本 如果已安装,请卸载它们: sudo apt-get remove d ...

  5. 集大软件工程15级个人作业Week2

    集大软件工程15级个人作业Week2 快速通读教材<构建之法>,并参照提问模板,提出5个问题. 在每个问题后面,请说明哪一章节的什么内容引起了你的提问,提供一些上下文 列出一些事例或资料, ...

  6. javascript 回到顶部

    <script type="text/javascript"> window.onload = function(){ var timer = null; //用于判断 ...

  7. 【BZOJ4826】【HNOI2017】影魔(扫描线,单调栈)

    [BZOJ4826][HNOI2017]影魔(扫描线,单调栈) 题面 BZOJ 洛谷 Description 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万.千百年来,他 ...

  8. HDU1875 畅通工程再续【最小生成树】

    题意: 在这些小岛中建设最小花费的桥,但是一座桥的距离必须在10 -- 1000之间. 思路: 用最小生成树解决吧,就那两个算法. 代码: prim #include <iostream> ...

  9. 第18月第10天 iOS11 uicollectionview

    1. - (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollect ...

  10. 第16月第12天 CABasicAnimation 旋转加速

    1. ; double duration = 10.0f; ; i<count; i++) { //旋转动画 CABasicAnimation *anima3 = [CABasicAnimati ...