题目链接:http://poj.org/problem?id=1679

题意:给你一组数据,让你判断是否是唯一的最小生成树。

题解:这里用的是kuangbin大佬的次小生成树的模板。直接判断一下次小生成树的最小花费和最小生成树的是否一样即可。

代码:

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
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 mp[maxn][maxn];
int n,m; 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] = mp[][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] > mp[p][j] ) ){
lowc[j] = mp[p][j];
pre[j] = p;
}
}
}
return ans;
}
int ans;
int smst(){
int minn = inf;
for(int i = ; i < n; i++)
for(int j = i+ ; j < n;j++)
if(mp[i][j] != inf && !used[i][j]){
minn = min(minn, ans + mp[i][j] - Max[i][j]);
}
if(minn == inf) return -;//不存在
return minn;
} int main(){
int T;
cin>>T;
while(T--){
cin>>n>>m;
int x,y,w;
memset(mp,inf,sizeof(mp));
for(int i = ; i < n ;i++){
mp[i][i] = ;
}
while(m--){
cin>>x>>y>>w;
x--,y--;
mp[x][y] = mp[y][x] = w;
}
ans = prim();
//cout<<smst()<<endl;
if(ans == -){
cout<<"Not Unique!"<<endl;
}
else if( ans == smst()){
cout<<"Not Unique!"<<endl;
}
else{
cout<<ans<<endl;
}
} return ;
}

【POJ】1679 The Unique MST的更多相关文章

  1. (poj)1679 The Unique MST 求最小生成树是否唯一 (求次小生成树与最小生成树是否一样)

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...

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

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

  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 Description Given a connected undirected graph, t ...

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

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

  6. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

  7. 【POJ】1704 Georgia and Bob(Staircase Nim)

    Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...

  8. 【POJ】1067 取石子游戏(博弈论)

    Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

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

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

随机推荐

  1. composer 配置镜像

    阿里云镜像:composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ 腾讯云镜像:composer ...

  2. .NET简介

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 微软跨语言运行的主要机制就如上图类似,其最重要的部分是CLR和MSIL:其中MSIL是微软中间语言,它的主要的作用是将不同 的语言,如:C ...

  3. C語言中資料結構(struct)的大小

    通常在PC上寫程式時,很少會去管struct會佔掉多少記憶體.當要使用到時,也不會想去用手算到底佔掉多少,大多是直接使用sizeof來做計算.然而sizeof計算出來的值往往不會如我們想的一樣.因為c ...

  4. c# 盖尔-沙普利算法的改进

    盖尔-沙普利算法 “盖尔-沙普利算法”(the Gale-Shapley algorithm),也被称为“延迟接受算法”(deferred-acceptance algorithm),简称“GS算法” ...

  5. CG-CTF CRYPTO部分wp

    1,easybase64解密得flag 2,keyboard键盘码,在键盘上画画得flag:areuhack 3,异性相吸根据提示,写脚本 with open('密文.txt')as a: a=a.r ...

  6. JS综合面试题1

    function foo(){ getName = function () { alert(1); }; return this; } Foo.getName = function(){ alert( ...

  7. 头文件 <sys/un.h>

    struct sockaddr_un server_sockaddr ; struct sockaddr_un cli_sockaddr ;

  8. 查看github热门项目

    访问 Trending 或者可以通过菜单 Explore -> 选择 "Trending" -- 默认是查看今天在github社区活跃的仓库 在 github 搜索框输入 s ...

  9. spark算子之Aggregate

    Aggregate函数 一.源码定义 /** * Aggregate the elements of each partition, and then the results for all the ...

  10. php获取微信openid

    使用微信接口,无论是自动登录还是微信支付我们首先需要获取的就是openid,获取openid的方式有两种,一种是在关注的时候进行获取,这种订阅号就可以获取的到,第二种是通过网页授权获取,这种获取需要的 ...