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 ...
随机推荐
- actor mysql 持久化之 specified actor
持久化到mysql,要求一次操作涉及到的多次读写的事务性.使用的 library 是 postgresql-async, akka 版本是 2.11. 1. 实现 per-user 逻辑,简单来讲,就 ...
- java命令行操作
一直使用eclipse操作java程序,但RMI程序需要命令行操作,故研究了下java的命令行操作. javac 用于编译.java文件,生成.class文件 假设文件夹dir下有pa.java和a. ...
- ios开发之--令UITableView滚动到指定位置
这个应用场景还是挺多的,代码如下: //获取到需要跳转位置的行数 NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow: inSect ...
- Selenium 节点交互
Selenium 可以驱动浏览器来执行一些操作,也就是说可以让浏览器模拟执行一些动作 常见方法:输入文字时用 send_keys() 方法,清空文字时用 clear() 方法,点击按钮时用 click ...
- [Command] lrzsz - 文件传输工具包
lrzsz 是一个支持 XMODEM.YMODEM.ZMODEM 文件传输协议的 Unix 程序包.它是 Omen Technologies 公司所有的 rzsz 程序包的公开发行增强版,遵守 GNU ...
- mybatis 之 parameterType="List" 2
<select id="queryGoodsByGoodsNoPcweb" parameterType="List" resultMap="si ...
- N76E003之WDT(看门狗定时器)
N76E003提供一个看门狗定时器(WDT),它可以配置成一个超时复位定时器用于复位整个设备.一旦由于外界干扰设备进入非正常状态或挂起,看门狗可以复位恢复系统.这有用于监测系统运行以提高系统可靠性.对 ...
- 《C++ Primer Plus》16.2 智能指针模板类
智能指针是行为类似于指针的类对象,单这种对象还有其他功能.本节介绍三个可帮助管理动态内存分配的智能指针类.先来看看需要哪些功能以及这些功能是如何实现的.请看下面的函数:void remodel(std ...
- Python进阶 学习笔记(一)
(笔记范围:第一章 课程介绍:第二章 函数式编程:第三章 模块) Python支持的函数式编程 不是纯函数式编程:允许有变量 支持高阶函数:函数也可以作为变量传入 支持闭包:有了闭包就能返回函数 有限 ...
- 【LeetCode OJ】Median of Two Sorted Arrays
题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ 题目:There are two sorted arrays nums1 ...