题意:求一个无向图的最小生成树,如果有多个最优解,输出"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. nyoj 1239 引水project (河南省第八届acm程序设计大赛)

    题目1239 pid=1239" style="color:rgb(55,119,188)">题目信息 pid=1239" style="col ...

  2. Hibernate_day01讲义_使用Hibernate完成对CRM系统中客户管理的DAO中的CRUD的操作

  3. Dubbo -- 系统学习 笔记 -- 示例 -- 启动时检查

    示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 启动时检查 Dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成,以便上线时,能及早发 ...

  4. NetBpm 测试篇(3)

    http://www.netbpm.org/movie/holiday/holiday.html

  5. PHP代码审计笔记--代码执行漏洞

    漏洞形成原因:客户端提交的参数,未经任何过滤,传入可以执行代码的函数,造成代码执行漏洞. 常见代码注射函数: 如:eval.preg_replace+/e.assert.call_user_func. ...

  6. git切换分支(自记)

    git fetch git checkout feature/A4-page

  7. codeforces水题100道 第二十五题 Codeforces Round #197 A. Helpful Maths (Div. 2) (strings)

    题目链接:http://www.codeforces.com/problemset/problem/339/A题意:重新组合加法字符串,使得按照1,2,3的顺序进行排列.C++代码: #include ...

  8. 查看sql server日志

    如果是查询当前已经连接到服务器的用户 select loginame, * from master.dbo.sysprocesses 查看sql 的操作日志记录 SELECT * From ::fn_ ...

  9. nginx fastcgi.conf的参数

       编写FastCGI程序的时候有很多像php一样的参数可以获取到,并利用起来,下面就是FastCGI的一些参数.     fastcgi_param  SCRIPT_FILENAME    $do ...

  10. matplotlib包画基本的图

    画直线图 1.最简单的用法: import matplotlib.pyplot as plt import numpy as np x=np.linspace(-3,3,50) #在(-1,1)范围内 ...