【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框架(十六)—— cookie和session组件
目录 cookie和session组件 一.cookie 1.cookie的由来 2.什么是cookie 3.cookie的原理 4.cookie的覆盖 5.在浏览器中查看cookie 6.cooki ...
- proc伪文件系统 - 加载一个进程
内核模块的编译方法及注意事项 Ubuntu内核(2.6.32) 2.6内核中,模块的编译需要配置过的内核源码:编译.链接后生成的内核模块后缀为.ko:编译过程首先会到内核源码目录下读取顶层的Makef ...
- docker安装及使用
一.简介 说起docker,大多数人第一时间想起来的应该有虚拟化.云计算,这三者有什么区别呢? 对比: 我们所熟知的虚拟化有KVM.xen.Hyper-V等,这些虚拟化虚拟出来的服务器会产生一个新的操 ...
- spring 注入bean的两种方式
我们都知道,使用spring框架时,不用再使用new来实例化对象了,直接可以通过spring容器来注入即可. 而注入bean有两种方式: 一种是通过XML来配置的,分别有属性注入.构造函数注入和工厂方 ...
- 在MyEclipse中更换或修改svn的用户名和密码
1.通过删除SVN客户端的账号配置文件 (1)找到我们使用的客户端配置文件,Windows XP中的位置是在系统盘的Documents and Settings\Administrator\A ...
- oracle number 类型 只显示10位精度
,) show numwidth; 设置为15位 ; xa ------------------ 123456789012.12 或者 TO_CHAR(xa,'FM099999999999.09999 ...
- Codeforces Round #568 (Div. 2) G2. Playlist for Polycarp (hard version)
因为不会打公式,随意就先将就一下? #include<cstdio> #include<algorithm> #include<iostream> #include ...
- 【CSS】text-align:justify 的使用
工作需要写一个全是文本的网页,规范格式的时候发现很多css属性不是很熟悉,比如text-align:justify. 这个是两端对齐,css3中新增了text-justify属性 语法:text-ju ...
- 详解 MySQL int 类型的长度值问题
以下是每个整数类型的存储和范围 (来自 mysql 手册)
- pygame游戏框架
#_author:来童星#date:2019/12/22 import pygame import sys pygame.init() size=width,height=640,480 screen ...