题目链接: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. org-mode记录总结

    org-mode记录总结 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #839496;} cod ...

  2. [轉]Linux Data Structures

    Table of Contents, Show Frames, No Frames Chapter 15 Linux Data Structures This appendix lists the m ...

  3. ssh文件 ip锁定

    vi ~/.ssh/config Host web*hostname 115.29.242.1**user lian Host **hostname 192.168.1.**user dface

  4. Pytest参数传递

    import pytest@pytest.fixture()def login_r(open_browser):#调用login时,发现需要先打开浏览器,所以改成先打开浏览器,在登陆 print('输 ...

  5. Groovy学习:第四章 Groovy特性深入

    作者:chszs 1. 断言 Java开发者常常使用JUnit或TestNG做单元测试,所以对断言是很清楚的.断言是用于验证假设的条件是否为真.在Groovy的断言中,如果假设的条件不为真,那么就会抛 ...

  6. Codeforces A Mist of Florescence

    A Mist of Florescence 题目大意: 事先告诉你每种颜色分别有几个联通块,构造一个不超过 \(50*50\) 的矩形.用 \(A,B,C,D\) 四种颜色来对矩形进行涂色使它满足要求 ...

  7. cmd登录mysql、查所有的库、查所有的表、查表下的所有字段

    一.设置好mysql的环境变量,cmd之后输入mysql -u root  -p 输入password进入mysql 二.展示所有的库名show  batabases: 三.选择一个库名use dem ...

  8. SVM 详解

    https://cloud.tencent.com/developer/article/1411618 (关于hinge损失函数的定义) https://zhuanlan.zhihu.com/p/61 ...

  9. 关于_getattr_方法的一些理解

    在学习rest framework的过程中,rest framework的request是经过重构的,但是如果调用重构对象request中的属性,如果属性不存在会调用原request对象中的属性,它使 ...

  10. 读取Properties

    package com.infotech.common.util; import java.io.FileNotFoundException; import java.io.IOException; ...