POJ 1679 The Unique MST (次小生成树kruskal算法)
The Unique MST
时间限制: 10 Sec 内存限制: 128 MB
提交: 25 解决: 10
[提交][状态][讨论版]
题目描述
Given a connected undirected graph, tell if its minimum spanning tree is unique.
Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the following properties:
1. V' = V.
2. T is connected and acyclic.
Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E') of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E'.
输入
输出
样例输入
2
3 3
1 2 1
2 3 2
3 1 3
4 4
1 2 2
2 3 2
3 4 2
4 1 2
样例输出
3
Not Unique!
题意:给一个无向图,判断这个图的最小生成树MST是否是唯一的。如果是唯一的,输出最小生成树的值,如果不是唯一的,输出“Not Unique!”
思路:kruskal的应用。详细思路与prim版相似,而且时间空间复杂度都得到了一些优化。
kruskal算法:把所有的边都排个序,从大到小取出,若取出的这条边的两个端点已经连通(用并查集),则换下一条边,n的顶点用n-1条边就可以相连,循环直到n-1条边。
本题:在找最小生成树(mst)的同时,把选中的边(是第几条)都存下来。再进行多次kruskal算法,每次模拟删除一条边,寻找一条新的边,得到边权和为mst2,判断mst==mst2?即可。
#include <iostream>
#include<string>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
int n, m;
int pre[];
int Rank[];
int mst_e[];
struct node
{
int u, v, w;
};
bool cmp(node x, node y)
{
return x.w < y.w;
}
void init()//初始化
{
int i;
for (i = ; i <= n; i++) pre[i] = i;
memset(Rank, , sizeof(Rank));
}
int find(int x)//找根
{
if (pre[x] == x) return x;
return pre[x] = find(pre[x]);
}
void unite(int x, int y)//压缩合并
{
if (Rank[x] < Rank[y]) pre[x] = y;
else
{
pre[y] = x;
if (Rank[x] == Rank[y]) Rank[x]++;
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
cin >> n >> m;
init();
node a[];
int i;
for (i = ; i <= m; i++)
{
cin >> a[i].u >> a[i].v >> a[i].w;
}
sort(a + , a + + m, cmp);
int mst = ;
int k = ;
for (i = ; i <= m; i++)//第一次kruskal算法
{
int x = find(a[i].u);
int y = find(a[i].v);
if (x != y)
{
unite(x, y);
mst = mst + a[i].w;
mst_e[k++] = i;// 记录下MST的边。
}
}
int edge_num = k-;
bool uni = ;//记录是不是唯一
int mst2, num;
for (k =; k <=edge_num; k++)
{//遍历每一条MST里的边,一次次模拟删除
init();//每进行一次kruskal算法,就初始化一次
mst2 = ;
num = ;
for (i = ; i <= m; i++)
{
if (i == mst_e[k]) continue;//模拟删除
int x = find(a[i].u);
int y = find(a[i].v);
if (x != y)
{
unite(x, y);
mst2 = mst2 + a[i].w;
num++;
}
if (num != edge_num) continue;//边数没达到就继续0
if (mst2 == mst)
{
uni = ;
break;
}
}
if (uni == ) break;
}
if (uni) cout << mst << endl;
else cout << "Not Unique!" << endl;
}
return ;
}
POJ 1679 The Unique MST (次小生成树kruskal算法)的更多相关文章
- POJ 1679 The Unique MST (次小生成树)
题目链接:http://poj.org/problem?id=1679 有t组数据,给你n个点,m条边,求是否存在相同权值的最小生成树(次小生成树的权值大小等于最小生成树). 先求出最小生成树的大小, ...
- 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 (次小生成树(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 ...
随机推荐
- koa 核心源码介绍
链接来源 Request,Context,Response 在代码运行之前就已经存在的 Request和Response自身的方法会委托到Context中. Context源码片段 var dele ...
- Tomcat学习之二:tomcat安装、配置及目录文件说明
我们看到tomcat目录/bin文件夹里有个tomcat6w.exe,顾名思义就是tomcat以window方式显示控制台.第1次点击打开它时候,可能会提示:tomcat指定的服务未安装,此时我们可以 ...
- SQL 基础--> NEW_VALUE 的使用
--=============================== -- SQL 基础--> NEW_VALUE 的使用 --=============================== 通常 ...
- Jsonp的实现
JSONP(JSON with Padding)是JSON的一种“使用模式”,可用于解决主流浏览器的跨域数据访问的问题. 由于同源策略,一般来说位于 server1.com 的网页无法与不是 serv ...
- HDU 5178:pairs(二分,lower_bound和upper_bound)
pairs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- JAVA解压文件
package com.chauvet.utils; import java.io.File; import java.io.FileOutputStream; import java.io.IOEx ...
- (11)shutil模块(文件处理模块)
shutil模块的格式 shutil.copyfileobj(文件1,文件2) #将文件1的数据覆盖copy给文件2 import shutil f1 = open("1.txt&quo ...
- Html页面Dom对象之Element
HTML DOM Element 对象 HTML DOM 节点 在 HTML DOM (文档对象模型)中,每个部分都是节点: 文档本身是文档节点 所有 HTML 元素是元素节点 所有 HTML 属性是 ...
- ES6必知必会 (三)—— 数组和对象的拓展
数组的扩展 1.拓展运算符('...'),它相当于rest参数的逆运算,用于将一个数组转换为用逗号分隔的参数序列: console.log(...[1, 2, 3]) // 1 2 3 console ...
- JS 网页快捷键设置
我们希望能用快捷键代替鼠标点击做一些事情,例如一个典型的应用就是论坛上常用的Ctrl + Enter 快捷发帖子.就以Ctrl+Enter快捷发帖子为例,实质上呢,就是通过JS脚本,捕获系统的onke ...