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 ...
随机推荐
- DevExpress v18.1新版亮点——ASP.NET篇(一)
用户界面套包DevExpress v18.1日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress ASP.NET v18.1 的新功能,快来下载试用新版本!点 ...
- <NET CLR via c# 第4版>笔记 第13章 接口
13.1 类和接口继承 13.2 定义接口 C#用 interface 关键字定义接口.接口中可定义方法,事件,无参属性和有参属性(C#的索引器),但不能定义任何构造器方法,也不能定义任何实例字段. ...
- 优化cocos2d/x程序的内存使用和程序大小
本站文章均为李华明Himi原创,转载务必在明显处注明:转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/iphone-cocos2d/1043.html ☞ ...
- 《利用Python进行数据分析》笔记---第6章数据加载、存储与文件格式
写在前面的话: 实例中的所有数据都是在GitHub上下载的,打包下载即可. 地址是:http://github.com/pydata/pydata-book 还有一定要说明的: 我使用的是Python ...
- HDU 1588 Gauss Fibonacci(矩阵快速幂)
Gauss Fibonacci Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Java面试通关要点汇总整理【终极版】
简历篇 请自我介绍 请介绍项目 基础篇 基本功 面向对象的特征 final, finally, finalize 的区别 int 和 Integer 有什么区别 重载和重写的区别 抽象类和接口有什么区 ...
- magento的常用调用
1,CMS调用网站的Url <a href="{{store direct_url="about-us"}}">About Us</a> ...
- chapter02 回归模型在''美国波士顿房价预测''问题中实践
#coding=utf8 # 从sklearn.datasets导入波士顿房价数据读取器. from sklearn.datasets import load_boston # 从sklearn.mo ...
- LG2024 [NOI2001]食物链
拆点法 用并查集维护每种动物的同类.天敌.食物群 #include<cstdio> int fa[300005]; int n,k,ans; inline int read() { int ...
- Dataframe 取列名
1.[column for column in df] 2.df.columns.values 返回 array 3.list(df) 4.df.columns 返回Index,可以通过 tolist ...