The Unique MST POJ - 1679 最小生成树判重
题意:求一个无向图的最小生成树,如果有多个最优解,输出"Not Unique!"
题解:
考虑kruskal碰到权值相同的边:
假设点3通过边(1,3)连入当前所维护的并查集s。
然后有一条边(下图蓝色的边)满足:
1.长度等于(1,3)
2.一端连到3,一端连入S。
那么该边可以替换掉(1,3)。产生另一颗最小生成树。
关于如何判断该边一端连3,一端连入S,
用set来记录S中的点,find判断点是否在集合内。(发现kruskal可以用set写啊)
- #define _CRT_SECURE_NO_WARNINGS
- #include<stdio.h>
- #include<queue>
- #include<algorithm>
- #include<iostream>
- #include<vector>
- #include<string.h>
- #include<set>
- using namespace std;
- const int maxn = 3e4;;set<int> s;
- struct edge {
- int to, from, w;
- edge(int to=, int from=, int w=) :to(to), from(from), w(w) {}
- }e[maxn];
- bool cmp(edge a, edge b) {
- return a.w < b.w;
- }
- int f[maxn];
- int find(int x) {
- return f[x] == x ? x : f[x] = find(f[x]);
- }
- void un(int x, int y) {
- int u = find(x), v= find(y);
- f[u] = v;
- }
- bool same(int x, int y) {
- return find(x) == find(y);
- }
- int main() {
- int t;
- cin >> t;
- while (t--)
- {
- int n, m; cin >> n >> m;
- int num = ;
- for (int i = ; i < m; i++) {
- int x, y, z; cin >> x >> y >> z;
- e[num++] = edge(x, y, z);
- }
- for (int i = ; i <= n; i++)f[i] = i;
- sort(e, e + m,cmp);
- int lastw = -, lastto = -, lastfrom = -,lastv = -;
- int res=, flag=;
- for (int i = ; i < m; i++) {
- if (same(e[i].to, e[i].from)) {
- if (e[i].w == lastw) {
- if (e[i].to == lastv && (s.find(e[i].from) != s.end())) { flag = ; break; }
- if (e[i].from == lastv && (s.find(e[i].to) != s.end())) { flag = ; break; }
- }
- continue;
- }
- un(e[i].to, e[i].from);
res += e[i].w;- if (s.find(e[i].to) == s.end()) lastv = e[i].to;
- else lastv = e[i].from;
- s.insert(e[i].to);
- s.insert(e[i].from);
- }
- if (flag)cout << "Not Unique!";
- else cout << res;
- cout << endl;
- }
- }
队友的玄学代码(改)可以不断记录上一条被选择的边,每次选边时判断一下入度出度关系;
- #define _CRT_SECURE_NO_WARNINGS
- #include<stdio.h>
- #include<queue>
- #include<algorithm>
- #include<iostream>
- #include<vector>
- #include<string.h>
- using namespace std;
- const int maxn = 3e4;;
- char s[maxn], str[maxn];
- int len1, len2, p[maxn], ans;
- struct edge {
- int to, from, w;
- edge(int to=, int from=, int w=) :to(to), from(from), w(w) {}
- }e[maxn];
- bool cmp(edge a, edge b) {
- return a.w < b.w;
- }
- int f[maxn];
- int find(int x) {
- return f[x] == x ? x : f[x] = find(f[x]);
- }
- void un(int x, int y) {
- int u = find(x), v= find(y);
- f[u] = v;
- }
- bool same(int x, int y) {
- return find(x) == find(y);
- }
- int main() {
- int t;
- cin >> t;
- while (t--)
- {
- int n, m; cin >> n >> m;
- int num = ;
- for (int i = ; i < m; i++) {
- int x, y, z; cin >> x >> y >> z;
- e[num++] = edge(x, y, z);
- e[num++] = edge(y, x, z);
- }
- for (int i = ; i <= n; i++)f[i] = i;
- sort(e, e + *m,cmp);
- int lastw=-, lastto=-, lastfrom=-;
- int res=, flag=;
- for (int i = ; i < m*; i++) {
- if (same(e[i].to, e[i].from)) {
- if (e[i].w == lastw) {
- if ((e[i].from == lastto)&&(e[i].to!=lastfrom)) { flag = ; break; }
- if ((e[i].to == lastfrom) && (e[i].from != lastto)) { flag = ; break; }
- }
- continue;
- }
- un(e[i].to, e[i].from);
- res += e[i].w;
- lastto = e[i].to;
- lastw = e[i].w;
- lastfrom = e[i].from;
- }
- if (flag)cout << "Not Unique!";
- else cout << res;
- cout << endl;
- }
- }
The Unique MST POJ - 1679 最小生成树判重的更多相关文章
- (最小生成树 次小生成树)The Unique MST -- POJ -- 1679
链接: http://poj.org/problem?id=1679 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#probl ...
- The Unique MST POJ - 1679 (次小生成树)
Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...
- K - The Unique MST - poj 1679
题目的意思已经说明了一切,次小生成树... ****************************************************************************** ...
- The Unique MST POJ - 1679 次小生成树prim
求次小生成树思路: 先把最小生成树求出来 用一个Max[i][j] 数组把 i点到j 点的道路中 权值最大的那个记录下来 used数组记录该条边有没有被最小生成树使用过 把没有使用过的一条边加 ...
- Day5 - G - The Unique MST POJ - 1679
Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...
- 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 Description Given a connected undirected graph, tell if its min ...
- POJ1679 The Unique MST(Kruskal)(最小生成树的唯一性)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27141 Accepted: 9712 D ...
- POJ 2458 DFS+判重
题意: 思路: 搜+判重 嗯搞定 (听说有好多人用7个for写得-.) //By SiriusRen #include <bitset> #include <cstdio>0 ...
随机推荐
- AngularJS------Error: Cannot find module '@angular-devkit/core'
如图: 解决方法: 进入项目目录下执行以下代码 npm i --save-dev @angular-devkit/core
- C#------Aspose.cells使用方法
转载: http://www.cnblogs.com/muer/p/yaxle.html 代码: public ActionResult ImportData(HttpPostedFileBase f ...
- SpringMVC由浅入深day01_1springmvc框架介绍
springmvc 第一天 springmvc的基础知识 课程安排: 第一天:springmvc的基础知识 什么是springmvc? springmvc框架原理(掌握) 前端控制器.处理器映射器.处 ...
- PHP计算两个绝对路径的相对路径
用PHP计算两个绝对路径的相对路径,该如何求呢? 先根据分隔符切割,然后查找相同 异同点,然后开始有相同点,从相同点结束为止开始拼接剩余部分,没有的话,到达根路径拼接整体. 截图如下: 代码如下: & ...
- 接口测试之JMeter初探
1.JMeter安装配置 )登录 http://jmeter.apache.org/download_jmeter.cgi ,下载与自己的平台相对应文件: )安装JDK(.6以上),配置环境变量JAV ...
- redis 列表
Redis 列表(List) Redis列表是简单的字符串列表,按照插入顺序排序.你可以添加一个元素导列表的头部(左边)或者尾部(右边) 一个列表最多可以包含 232 - 1 个元素 (4294967 ...
- GC--垃圾收集器
把周末的文章放在现在才来写,是自己太忙了?还是堕落了? 好吧直接进入主题吧,简单干脆的理解会让自己记忆深刻: 首先说明:GC垃圾收集器关注两件事情: 第一件:查找所有存活对象. 第二件:抛弃死对象(不 ...
- 【代码审计】iZhanCMS_v2.1 前台存储型XSS漏洞分析
0x00 环境准备 iZhanCMS官网:http://www.izhancms.com 网站源码版本:爱站CMS(zend6.0) V2.1 程序源码下载:http://www.izhancms ...
- 关于RabbitMQ交换机的理解
RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性.扩展性.高可用性等方面表现不俗.消息中间件主要用于组件之间的解耦,消 ...
- struts1的配置文件详解
要想使用Struts,至少要依靠两个配置文件:web.xml和struts-config.xml.其中web.xml用来安装Struts框架.而struts-config.xml用来配置在Struts ...