【POJ】1679 The Unique MST
题目链接: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的更多相关文章
- (poj)1679 The Unique MST 求最小生成树是否唯一 (求次小生成树与最小生成树是否一样)
Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...
- poj 1679 The Unique MST 【次小生成树】【模板】
题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...
- 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 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 S ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
- 【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, ...
- 【POJ】1067 取石子游戏(博弈论)
Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...
- POJ 1679 The Unique MST 【最小生成树/次小生成树模板】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
随机推荐
- Django框架(二十二)—— Django rest_framework-频率组件
目录 频率组件 一.作用 二.自定义频率类 三.内置的访问频率控制类 四.全局.局部使用 1.全局使用 2.局部使用 3.局部禁用 五.源码分析 1.as_view -----> view -- ...
- dubbo接口未更新,清maven缓存问题
有时候idea maven reimport 并未更新到最新的版本 此时可以到./m2去进行手动清楚缓存再更新 cd .m2/repository/cn/dface/biz/couponcenter ...
- windows server 2016 支持多用户远程登录
服务器设置多用户同时远程桌面,可以提高访问效率,避免人多抢登服务器. 1. 首先需要先安装远程桌面服务 配置组策略,运行框输入gpedit.msc,打开计算机配置–>管理模板—>wind ...
- 【读书笔记】【数据库】SQL必知必会
第1课 了解SQL 简单介绍了sql,和dbms,无重点. 第2课 检索数据 重点:select语句,distinct,limit,注释 1. select 语句如果没有明确排序查询结果,那么返回的数 ...
- 【读书笔记】剑指offer
导语 所有的编程练习都在牛客网OJ提交,链接: https://www.nowcoder.com/ta/coding-interviews 九章算法的 lintcode 也有这本书的题目.https: ...
- koa 中间件 koa-art-template 的使用
例子 const Koa = require('koa'); const render =require('koa-art-template'); const path= require('path' ...
- js 实现 间隙滚动效果
代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...
- Map集合类(二.其他map集合jdk1.8)
java集合笔记一 java集合笔记二 java集合笔记三 1.hashtable(线程安全) 1.存储数据为数组+链表2.存储键值对或获取时通过hash值取模数组长度确定节点在数组中的下标位置 in ...
- php常用函数总结2
文件系统函数 函数名 描述 实例 输入 输出 操作 fopen() 打开文件或者 URL $handle = fopen("ftp://user:password@example.com/s ...
- 关于if else 和 三目运算符的效率问题-java
1.从类型转换上看,因为三目运算符在做判断的时候需要考虑到类型转换的问题,而if else 不需要考虑类型转换. 所以 if else 效率高一点. 2.从总体上看 A:需要考虑到循环自身所占用的时间 ...