题意:求一个无向图的最小生成树,如果有多个最优解,输出"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 最小生成树判重的更多相关文章

  1. (最小生成树 次小生成树)The Unique MST -- POJ -- 1679

    链接: http://poj.org/problem?id=1679 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#probl ...

  2. The Unique MST POJ - 1679 (次小生成树)

    Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...

  3. K - The Unique MST - poj 1679

    题目的意思已经说明了一切,次小生成树... ****************************************************************************** ...

  4. The Unique MST POJ - 1679 次小生成树prim

    求次小生成树思路: 先把最小生成树求出来  用一个Max[i][j] 数组把  i点到j 点的道路中 权值最大的那个记录下来 used数组记录该条边有没有被最小生成树使用过   把没有使用过的一条边加 ...

  5. Day5 - G - The Unique MST POJ - 1679

    Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...

  6. poj 1679 The Unique MST(唯一的最小生成树)

    http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  7. POJ 1679 The Unique MST(判断最小生成树是否唯一)

    题目链接: http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its min ...

  8. POJ1679 The Unique MST(Kruskal)(最小生成树的唯一性)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27141   Accepted: 9712 D ...

  9. POJ 2458 DFS+判重

    题意: 思路: 搜+判重 嗯搞定 (听说有好多人用7个for写得-.) //By SiriusRen #include <bitset> #include <cstdio>0 ...

随机推荐

  1. Nginx Session Sticky

    nginx的粘性session主要通过nginx-sticky-module实现 1 下载 nginx-sticky-module 下载地址:https://code.google.com/p/ngi ...

  2. 第四章 TCP粘包/拆包问题的解决之道---4.1---

    4.1 TCP粘包/拆包 TCP是一个“流”协议,所谓流,就是没有界限的一串数据.TCP底层并不了解上层业务数据的具体含义,它会根据TCP缓冲区的实际情况进行包的划分,所以在业务上认为,一个完整的包可 ...

  3. Hightchart 技巧

    http://blog.csdn.net/u014796515/article/details/24428131

  4. mysql相关攻击代码收集

    1.批处理文件内容 @echo off net user li /add net user li /active:yes net localgroup Administrators li /add 2 ...

  5. Java API方式调用Kafka各种协议

    众所周知,Kafka自己实现了一套二进制协议(binary protocol)用于各种功能的实现,比如发送消息,获取消息,提交位移以及创建topic等.具体协议规范参见:Kafka协议  这套协议的具 ...

  6. N76E003之定时器3

    定时器3是一个16位自动重装载,向上计数定时器.用户可以通过配置T3PS[2:0] (T3CON[2:0])选择预分频,并写入重载值到R3H 和R3L寄存器来决定它的溢出速率.用户可以设置TR3 (T ...

  7. Use Reentrant Functions for Safer Signal Handling(译:使用可重入函数进行更安全的信号处理)

    Use Reentrant Functions for Safer Signal Handling 使用可重入函数进行更安全的信号处理 How and when to employ reentranc ...

  8. 使用__FILE__和__LINE__定位错误

    #include <stdio.h> int main() { printf("this fake error is in %s on line %d\n", __FI ...

  9. 【Python】给程序加个进度条

    对于开发或者运维来说,使用 Python 去完成一些跑批任务,或者做一些监控事件是非常正常的情况.那么如何有效地监控任务的进度?除了在任务中加上 Log 外,还能不能有另一种方式来了解任务进展到哪一步 ...

  10. Win7/Win8/IIS7/IIS8配置ASP/ACCESS

    1.在IIS信息服务管理器配置好站点后,配置ASP属性: a.IIS启用ASP 1.打开控制面板>>程序和功能>>“打开或关闭windows功能”,见下图 2.稍等片刻,出现一 ...