POJ 1679 The Unique MST (次小生成树)
题目链接:http://poj.org/problem?id=1679
有t组数据,给你n个点,m条边,求是否存在相同权值的最小生成树(次小生成树的权值大小等于最小生成树)。
先求出最小生成树的大小,把最小生成树的边存起来。然后分别枚举最小生成树上的每条边,除了这条边,其他边是否能生成最小生成树,若生成树的权值等于原来最小生成树的权值,则不唯一,否则输出最小生成树的权值。这里我用kruskal比较方便。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN = ;
const int INF = 1e9;
struct edge {
int u , v , cost;
}a[MAXN * MAXN];
typedef pair <int , int> P;
int par[MAXN] , f;
bool vis[MAXN][MAXN];
vector <P> G; void init(int n) {
for(int i = ; i <= n ; i++) {
par[i] = i;
}
memset(vis , false , sizeof(vis));
f = ;
G.clear();
} bool cmp(edge a , edge b) {
return a.cost < b.cost;
} int Find(int n) {
if(par[n] == n)
return n;
return (par[n] = Find(par[n]));
} int kruskal(int m , int n) {
int res = , cont = n;
for(int i = ; i <= m ; i++) {
if(cont == )
break;
else if(vis[a[i].u][a[i].v])
continue;
int u = Find(a[i].u) , v = Find(a[i].v);
if(u != v) {
if(!f) {
G.push_back(P(a[i].u , a[i].v)); // f等于0的时候是求最小生成树的时候,存边操作
}
par[u] = v;
res += a[i].cost;
cont--;
}
}
if(cont == )
return res;
return INF;
} int main()
{
int t , n , m , u , v , w;
scanf("%d" , &t);
while(t--) {
scanf("%d %d" , &n , &m);
init(n);
for(int i = ; i <= m ; i++) {
scanf("%d %d %d" , &u , &v , &w);
a[i].u = u , a[i].v = v , a[i].cost = w;
}
sort(a + , a + m + , cmp);
int res = kruskal(m , n) , check = ;
f++;
for(int i = ; i < G.size() ; i++) {
for(int j = ; j <= n ; j++) {
par[j] = j;
}
vis[G[i].first][G[i].second] = true;
if(res == kruskal(m , n)) {
check = ;
break;
}
vis[G[i].first][G[i].second] = false;
}
if(check) {
printf("Not Unique!\n");
}
else {
printf("%d\n" , res);
}
}
}
POJ 1679 The Unique MST (次小生成树)的更多相关文章
- POJ 1679 The Unique MST (次小生成树 判断最小生成树是否唯一)
题目链接 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. De ...
- POJ 1679 The Unique MST (次小生成树kruskal算法)
The Unique MST 时间限制: 10 Sec 内存限制: 128 MB提交: 25 解决: 10[提交][状态][讨论版] 题目描述 Given a connected undirect ...
- poj 1679 The Unique MST (次小生成树(sec_mst)【kruskal】)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 35999 Accepted: 13145 ...
- poj 1679 The Unique MST 【次小生成树】【模板】
题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...
- POJ 1679 The Unique MST 【最小生成树/次小生成树模板】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- POJ1679 The Unique MST —— 次小生成树
题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total S ...
- 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 Submis ...
- poj 1679 The Unique MST (判定最小生成树是否唯一)
题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total S ...
随机推荐
- th固定 td滚动的表格实现
为什么这样? 体验好 原理 通过两个表格,使其th td 对应,产生一种错觉. 代码 1.html <div class="content"> <div clas ...
- 函数buf_pool_init_instance
buff_pool_t 结构体 详见 /********************************************************************//** Initial ...
- Android 怎样使用API
本文针对Android开发如何使用API文档进行一些经验分享. 1.为什么需要掌握API的使用. 也许你需要完成一个功能时很多时候你在网上google一番,因为很可能找到有用的代码片段,甚至不用关心具 ...
- 发布到IIS的时候用户 'WWW-6743CC520E9\ASPNET' 登录失败
在 webConfig 数据连接那里 别用集成验证方式 使用用户名密码的方式连接
- Java [Leetcode 58]Length of Last Word
题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...
- Java [Leetcode 263]Ugly Number
题目描述: Write a program to check whether a given number is an ugly number. Ugly numbers are positive n ...
- Linux Shell编程(2): for while
; i < ; i++)) do echo "current number is $i" done SERVICES="80 22 25 110 8000 23 2 ...
- 【转】Android Studio简单设置
原文网址:http://ask.android-studio.org/?/article/14 Android Studio 简单设置 界面设置 默认的 Android Studio 为灰色界面,可以 ...
- Oracle 一次 锁表 处理小记
同事说测试库上的一张表被锁了. 不能执行DML 操作. 锁表的准确说法应该是阻塞.之前的一遍blog里有说明: 锁 死锁 阻塞Latch 等待 详解 http://blog.csdn.net/tian ...
- Java 单元测试(Junit)
在有些时候,我们需要对我们自己编写的代码进行单元测试(好处是,减少后期维护的精力和费用),这是一些最基本的模块测试.当然,在进行单元测试的同时也必然得清楚我们测试的代码的内部逻辑实现,这样在测试的时候 ...