【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 ...
随机推荐
- linux中errno及perror的应用
1 perror 定义在头文件<stdlib.h>中 void perror(const char *s);函数说明 perror ( )用 来 将 上 一 个 函 数 发 生 错 误 的 ...
- mvnw简介
- Mysq sql语句教程
mysql管理命令 show databases; 显示服务器上当前所有的数据库 use 数据库名称; 进入指定的数据库 show tables; 显示当前数据库中所有的数据表 d ...
- CF914D
CF914D 用线段树乱搞一下就行qwq #include<iostream> #include<cstring> #include<cstdio> #includ ...
- XStream的简单使用
XStream XStream是一个java对象和xml相互转换的工具 创建XStream对象:XStream stream = new XStream() Java对象转换成xml:stream . ...
- 服务bindService()方法启动服务
public class MainActivity extends Activity { private EditText studentno; private ServiceConnection c ...
- 《构建之法》需求分析 读书笔记 Week6
本周选读<构建之法>第8章——需求分析.由于有团队项目初期调研阶段做调查问卷的经历,这一章节中很多知识点我都比较有体会.对我而言,这一章节最有价值的内容就是厘清了关于需求分析的两个误解和近 ...
- Javascript权威指南——读书笔记
一.JavaScript核心语法 1.字符串中接受RegExp参数的方法 (1)text.search(pattern)返回首次匹配成功的位置 (2)text.match(pattern)返回匹配组成 ...
- termios, tcgetattr, tcsetattr, tcsendbreak, tcdrain, tcflush, tcflow, cfmakeraw, cfgetospeed, cfgetispeed, cfsetispeed, cfsetospeed - 获取和设置终端属性,行控制,获取和设置波特率
SYNOPSIS 总览 #include <termios.h> #include <unistd.h> int tcgetattr(int fd, struct termio ...
- android thread Runnable
原文链接: http://blog.csdn.net/boyupeng/article/details/6208072 这篇文章中有三点需要提前说明一下, 一: 在android中有两种实现线程thr ...