题意:求一个无向图的最小生成树,如果有多个最优解,输出"Not Unique!"

题解:

  考虑kruskal碰到权值相同的边:

  假设点3通过边(1,3)连入当前所维护的并查集s。

  然后有一条边(下图蓝色的边)满足:

      1.长度等于(1,3)

      2.一端连到3,一端连入S。

  那么该边可以替换掉(1,3)。产生另一颗最小生成树。

关于如何判断该边一端连3,一端连入S,

用set来记录S中的点,find判断点是否在集合内。(发现kruskal可以用set写啊)

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<stdio.h>
  3. #include<queue>
  4. #include<algorithm>
  5. #include<iostream>
  6. #include<vector>
  7. #include<string.h>
  8. #include<set>
  9. using namespace std;
  10. const int maxn = 3e4;;set<int> s;
  11. struct edge {
  12. int to, from, w;
  13. edge(int to=, int from=, int w=) :to(to), from(from), w(w) {}
  14. }e[maxn];
  15. bool cmp(edge a, edge b) {
  16. return a.w < b.w;
  17. }
  18. int f[maxn];
  19. int find(int x) {
  20. return f[x] == x ? x : f[x] = find(f[x]);
  21. }
  22. void un(int x, int y) {
  23. int u = find(x), v= find(y);
  24. f[u] = v;
  25. }
  26. bool same(int x, int y) {
  27. return find(x) == find(y);
  28. }
  29. int main() {
  30. int t;
  31. cin >> t;
  32. while (t--)
  33. {
  34. int n, m; cin >> n >> m;
  35. int num = ;
  36. for (int i = ; i < m; i++) {
  37. int x, y, z; cin >> x >> y >> z;
  38. e[num++] = edge(x, y, z);
  39. }
  40. for (int i = ; i <= n; i++)f[i] = i;
  41. sort(e, e + m,cmp);
  42. int lastw = -, lastto = -, lastfrom = -,lastv = -;
  43. int res=, flag=;
  44. for (int i = ; i < m; i++) {
  45.  
  46. if (same(e[i].to, e[i].from)) {
  47. if (e[i].w == lastw) {
  48. if (e[i].to == lastv && (s.find(e[i].from) != s.end())) { flag = ; break; }
  49. if (e[i].from == lastv && (s.find(e[i].to) != s.end())) { flag = ; break; }
  50. }
  51. continue;
  52. }
  53. un(e[i].to, e[i].from);
            res += e[i].w;
  54. if (s.find(e[i].to) == s.end()) lastv = e[i].to;
  55. else lastv = e[i].from;
  56. s.insert(e[i].to);
  57. s.insert(e[i].from);
  58. }
  59. if (flag)cout << "Not Unique!";
  60. else cout << res;
  61. cout << endl;
  62. }
  63. }

队友的玄学代码(改)可以不断记录上一条被选择的边,每次选边时判断一下入度出度关系;

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<stdio.h>
  3. #include<queue>
  4. #include<algorithm>
  5. #include<iostream>
  6. #include<vector>
  7. #include<string.h>
  8. using namespace std;
  9. const int maxn = 3e4;;
  10. char s[maxn], str[maxn];
  11. int len1, len2, p[maxn], ans;
  12. struct edge {
  13. int to, from, w;
  14. edge(int to=, int from=, int w=) :to(to), from(from), w(w) {}
  15. }e[maxn];
  16. bool cmp(edge a, edge b) {
  17. return a.w < b.w;
  18. }
  19. int f[maxn];
  20. int find(int x) {
  21. return f[x] == x ? x : f[x] = find(f[x]);
  22. }
  23. void un(int x, int y) {
  24. int u = find(x), v= find(y);
  25. f[u] = v;
  26. }
  27. bool same(int x, int y) {
  28. return find(x) == find(y);
  29. }
  30. int main() {
  31. int t;
  32. cin >> t;
  33. while (t--)
  34. {
  35. int n, m; cin >> n >> m;
  36. int num = ;
  37. for (int i = ; i < m; i++) {
  38. int x, y, z; cin >> x >> y >> z;
  39. e[num++] = edge(x, y, z);
  40. e[num++] = edge(y, x, z);
  41.  
  42. }
  43. for (int i = ; i <= n; i++)f[i] = i;
  44. sort(e, e + *m,cmp);
  45. int lastw=-, lastto=-, lastfrom=-;
  46. int res=, flag=;
  47. for (int i = ; i < m*; i++) {
  48.  
  49. if (same(e[i].to, e[i].from)) {
  50. if (e[i].w == lastw) {
  51. if ((e[i].from == lastto)&&(e[i].to!=lastfrom)) { flag = ; break; }
  52. if ((e[i].to == lastfrom) && (e[i].from != lastto)) { flag = ; break; }
  53. }
  54. continue;
  55. }
  56. un(e[i].to, e[i].from);
  57. res += e[i].w;
  58. lastto = e[i].to;
  59. lastw = e[i].w;
  60. lastfrom = e[i].from;
  61. }
  62. if (flag)cout << "Not Unique!";
  63. else cout << res;
  64. cout << endl;
  65. }
  66. }

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. AngularJS------Error: Cannot find module '@angular-devkit/core'

    如图: 解决方法: 进入项目目录下执行以下代码 npm i --save-dev @angular-devkit/core

  2. C#------Aspose.cells使用方法

    转载: http://www.cnblogs.com/muer/p/yaxle.html 代码: public ActionResult ImportData(HttpPostedFileBase f ...

  3. SpringMVC由浅入深day01_1springmvc框架介绍

    springmvc 第一天 springmvc的基础知识 课程安排: 第一天:springmvc的基础知识 什么是springmvc? springmvc框架原理(掌握) 前端控制器.处理器映射器.处 ...

  4. PHP计算两个绝对路径的相对路径

    用PHP计算两个绝对路径的相对路径,该如何求呢? 先根据分隔符切割,然后查找相同 异同点,然后开始有相同点,从相同点结束为止开始拼接剩余部分,没有的话,到达根路径拼接整体. 截图如下: 代码如下: & ...

  5. 接口测试之JMeter初探

    1.JMeter安装配置 )登录 http://jmeter.apache.org/download_jmeter.cgi ,下载与自己的平台相对应文件: )安装JDK(.6以上),配置环境变量JAV ...

  6. redis 列表

    Redis 列表(List) Redis列表是简单的字符串列表,按照插入顺序排序.你可以添加一个元素导列表的头部(左边)或者尾部(右边) 一个列表最多可以包含 232 - 1 个元素 (4294967 ...

  7. GC--垃圾收集器

    把周末的文章放在现在才来写,是自己太忙了?还是堕落了? 好吧直接进入主题吧,简单干脆的理解会让自己记忆深刻: 首先说明:GC垃圾收集器关注两件事情: 第一件:查找所有存活对象. 第二件:抛弃死对象(不 ...

  8. 【代码审计】iZhanCMS_v2.1 前台存储型XSS漏洞分析

      0x00 环境准备 iZhanCMS官网:http://www.izhancms.com 网站源码版本:爱站CMS(zend6.0) V2.1 程序源码下载:http://www.izhancms ...

  9. 关于RabbitMQ交换机的理解

    RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性.扩展性.高可用性等方面表现不俗.消息中间件主要用于组件之间的解耦,消 ...

  10. struts1的配置文件详解

    要想使用Struts,至少要依靠两个配置文件:web.xml和struts-config.xml.其中web.xml用来安装Struts框架.而struts-config.xml用来配置在Struts ...