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 ...
随机推荐
- 分分钟学会GCD
2014 什么是GCD Grand Central Dispatch (GCD)是异步运行任务的技术之中的一个.一般将应用程序中记述的线程管理用的代码在系统级中实现.因为线程管理是作为系统的一部分来实 ...
- Java Jdk1.8 HashMap源代码阅读笔记二
三.源代码阅读 3.元素包括containsKey(Object key) /** * Returns <tt>true</tt> if this map contains a ...
- 8 -- 深入使用Spring -- 7... Spring 整合 Struts 2
8.7 Spring 整合 Struts2 8.7.1 启动Spring 容器 8.7.2 MVC框架与Spring整合的思考 8.7.3 让Spring管理控制器 8.7.4 使用自动装配
- 【GIS】Cesium回到初始位置
var boundingSphere = new Cesium.BoundingSphere(Cesium.Cartesian3.fromDegrees(116.4, 39.9, 100), 15 ...
- 在oracle配置mysql数据库的dblink
本文介绍如何在oracle配置mysql数据库的dblink:虽然dblink使用很占资源:俗称“性能杀手”.但有些场景不得不使用它.例如公司使用数据库是oracle:可能其他部门或者CP合作公司使用 ...
- Spring transaction事务 roll back各种回滚
Spring的AOP事务管理默认是针对unchecked exception回滚. 也就是默认对RuntimeException()异常极其子类进行事务回滚. Exception作为基类,下面还分ch ...
- 清理和关闭多余的Windows 7系统服务
清理和关闭多余的Windows 7系统服务 现在已经有不少配置不是很高的电脑用户正式用上了Windows 7(以下简称Win 7),如何让低配置电脑可以更流畅的运行Win 7呢?虽然部分软件提供了傻瓜 ...
- 【前端安全】JavaScript防http劫持与XSS (转)
作为前端,一直以来都知道HTTP劫持与XSS跨站脚本(Cross-site scripting).CSRF跨站请求伪造(Cross-site request forgery).但是一直都没有深入研究过 ...
- laravel curl post json
<?php namespace App\BO; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; us ...
- Ubuntu 16.04 LAMP server tutorial with Apache 2.4, PHP 7 and MariaDB (instead of MySQL)
https://www.howtoforge.com/tutorial/install-apache-with-php-and-mysql-on-ubuntu-16-04-lamp/ This tut ...