ICPC (Isles of Coral Park City) consist of several beautiful islands. The citizens requested construction of bridges between islands to resolve inconveniences of using boats between islands, and they demand that all the islands should be reachable from any other islands via one or more bridges. The city mayor selected a number of pairs of islands, and ordered a building company to estimate the costs to build bridges between the pairs. With this estimate, the mayor has to decide the set of bridges to build, minimizing the total construction cost. However, it is difficult for him to select the most cost-efficient set of bridges among those connecting all the islands. For example, three sets of bridges connect all the islands for the Sample Input 1. The bridges in each set are expressed by bold edges in Figure F.1.
Figure F.1. Three sets of bridges connecting all the islands for Sample Input 1
As the first step, he decided to build only those bridges which are contained in all the sets of bridges to connect all the islands and minimize the cost. We refer to such bridges as no alternative bridges. In Figure F.2, no alternative bridges are drawn as thick edges for the Sample Input 1, 2 and 3.
Figure F.2. No alternative bridges for Sample Input 1, 2 and 3
Write a program that advises the mayor which bridges are no alternative bridges for the given input.
Input
The input file contains several test cases, each of them has the following format.
N M S1 D1 C1 . . . SM DM CM
The first line contains two positive integers N and M. N represents the number of islands and each island is identified by an integer 1 through N. M represents the number of the pairs of islands between which a bridge may be built. Each line of the next M lines contains three integers Si, Di and Ci (1 ≤ i ≤ M) which represent that it will cost Ci to build the bridge between islands Si and Di. You may assume 3 ≤ N ≤ 500, N − 1 ≤ M ≤ min(50000,N(N − 1)/2), 1 ≤ Si < Di ≤ N, and 1 ≤ Ci ≤ 10000. No two bridges connect the same pair of two islands, that is, if i ̸= j and Si = Sj, then Di ̸= Dj. If all the candidate bridges are built, all the islands are reachable from any other islands via one or more bridges.
Output
For each test case, output two integers, which mean the number of no alternative bridges and the sum of their construction cost, separated by a space.
Sample Input
4 4 1 2 3 1 3 3 2 3 3 2 4 3 4 4 1 2 3 1 3 5 2 3 3 2 4 3 4 4 1 2 3 1 3 1 2 3 3 2 4 3 3 3 1 2 1 2 3 1 1 3 1
Sample Output
1 3 3 9 2 4 0 0

分析:

题意
给定一个N个点,M条边的简单连通无向图。
对于一个无向图来说,它的最小生成树可能不是唯一的。
问在它的所有的最小生成树中共有的边是哪几条,输出边数和权值之和。
3<=N<=500, N-1<=M<=min{50000, N(N-1)/2}
思路
首先跑一遍Kruskal,得到最小生成树的权值。
之后尝试删去图中的边,如果某一条边被删去后,最小生成树的值发生了变化(一定变大),那么说明这条边是在所有的最小生成树中都不可或缺的,那么就把这条边加入到答案中。
注意到第一次Kruskal得到的边已经包含了所有的答案,因此只要枚举这里的N-1条边即可。
边排序的复杂度被均摊了,并查集的复杂度可以忽略,因此总的复杂度是O(NM)

注意:重载运算符比cmp快一点

还有就是用一个数组存下第一次MST用到的边(开始没有存起来,直接标记,超时。。。。。。。。。)

code:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include<stdio.h>
  4. #include<algorithm>
  5. #include<cstring>
  6. #include<math.h>
  7. #include<memory>
  8. using namespace std;
  9. typedef long long LL;
  10. #define INF 0x3f3f3f3f
  11. #define max_v 50010
  12. #define max_n 510
  13. struct edge
  14. {
  15. int x,y,w;
  16. bool operator<(const edge& b) const
  17. {
  18. return w<b.w;
  19. }
  20. } e[max_v];
  21. int possEdge[max_n];//保存第一次MST用到的边 很重要
  22. int pa[max_n];
  23. int cnt,minv;
  24. int n,m;
  25. int c,wsum;
  26. void init()
  27. {
  28. for(int i=;i<=n;i++)
  29. pa[i]=i;
  30. }
  31. int find_set(int x)
  32. {
  33. if(x!=pa[x])
  34. pa[x]=find_set(pa[x]);
  35. return pa[x];
  36. }
  37. int union_set(int x,int y)
  38. {
  39. x=find_set(x);
  40. y=find_set(y);
  41. if(x==y)
  42. return ;
  43. pa[x]=y;
  44. return ;
  45. }
  46. void firstkrus()
  47. {
  48. cnt=;
  49. minv=;
  50. init();
  51. for(int i=; i<m; i++)
  52. {
  53. if(union_set(e[i].x,e[i].y))
  54. {
  55. minv+=e[i].w;
  56. possEdge[cnt++]=i;
  57. if(cnt==n-)
  58. break;
  59. }
  60. }
  61. }
  62. bool krusWithout(int ce)
  63. {
  64. init();
  65. int sum=,ct=;
  66. for(int i=; i<m; i++)
  67. {
  68. if(i==ce)
  69. continue;
  70. if(union_set(e[i].x,e[i].y))
  71. {
  72. ct++;
  73. sum+=e[i].w;
  74. if(sum>minv)
  75. return false;
  76. if(ct==cnt)
  77. return true;
  78. }
  79. }
  80. return false;
  81. }
  82. void trycut()
  83. {
  84. c=;
  85. wsum=;
  86. for(int i=; i<cnt; i++)
  87. {
  88. if(!krusWithout(possEdge[i]))
  89. {
  90. c++;
  91. wsum+=e[possEdge[i]].w;
  92. }
  93. }
  94. printf("%d %d\n",c,wsum);
  95. }
  96. int main()
  97. {
  98. while(~scanf("%d %d",&n,&m))
  99. {
  100. for(int i=; i<m; i++)
  101. scanf("%d %d %d",&e[i].x,&e[i].y,&e[i].w);
  102. sort(e,e+m);
  103. firstkrus();
  104. trycut();
  105. }
  106. return ;
  107. }

UVALive - 6837 Kruskal+一点性质(暴力枚举)的更多相关文章

  1. Gym 101194L / UVALive 7908 - World Cup - [三进制状压暴力枚举][2016 EC-Final Problem L]

    题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...

  2. P1217 [USACO1.5]回文质数 Prime Palindromes(技巧+暴力枚举+线性筛)

    技巧:就是偶数位的回文数字一定不是质数---------证明:奇数位之和sum1==偶数位之和sum2的数字可以被11整除.(11除外,这是一个坑点) 最高位,最低位必须是 1, 3, 7, 9 暴力 ...

  3. UVA.12716 GCD XOR (暴力枚举 数论GCD)

    UVA.12716 GCD XOR (暴力枚举 数论GCD) 题意分析 题意比较简单,求[1,n]范围内的整数队a,b(a<=b)的个数,使得 gcd(a,b) = a XOR b. 前置技能 ...

  4. Codeforces Round #266 (Div. 2)B(暴力枚举)

    很简单的暴力枚举,却卡了我那么长时间,可见我的基本功不够扎实. 两个数相乘等于一个数6*n,那么我枚举其中一个乘数就行了,而且枚举到sqrt(6*n)就行了,这个是暴力法解题中很常用的性质. 这道题找 ...

  5. CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)

    题意:求定 n 个数,求有多少对数满足,ai^bi = x. 析:暴力枚举就行,n的复杂度. 代码如下: #pragma comment(linker, "/STACK:1024000000 ...

  6. 2014牡丹江网络赛ZOJPretty Poem(暴力枚举)

    /* 将给定的一个字符串分解成ABABA 或者 ABABCAB的形式! 思路:暴力枚举A, B, C串! */ 1 #include<iostream> #include<cstri ...

  7. HNU 12886 Cracking the Safe(暴力枚举)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ...

  8. 51nod 1116 K进制下的大数 (暴力枚举)

    题目链接 题意:中文题. 题解:暴力枚举. #include <iostream> #include <cstring> using namespace std; ; ; ch ...

  9. Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举

    题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...

随机推荐

  1. ubuntu 配置dns访问外网

    我新建了一个虚拟机后采用的net模式 一 先配置虚拟机ip使其能在局域网内通信 1.vmware 编辑->虚拟网络编辑器 可以看到网关.掩码等信息 可以看到我们配置的ip应该在192.168.1 ...

  2. IDEA 搭建 springmvc maven 项目

    前言:将搭建 java springmvc maven 项目的过程及问题记录下来,以及配置文件.这次没有涉及到数据库,后续再写. 目录: 一.首先在 IDEA 中创建 springmvc maven ...

  3. Node.js IO处理输入和回显,以及当今web应用程序的发展史

    1.关于Node.js IO处理输入和回显 在Windows终端或者CD中输入   echo  'I must learn about Node.js' 结果将刚刚输入的   echo  'I mus ...

  4. html+css 百度首页练习

    这几天看完了<css权威指南>,写了个百度页面,不带js的纯静态,主要目的就是掌握页面布局,字体颜色之类的没有深究. 写完了觉得很简单,毕竟一开始觉得只要模仿的像就行,但是缩小了浏览器窗口 ...

  5. c# copy类中值到另外一个对象中

    贴图: 调用:

  6. 【JavaScript】闭包应用之数据独立

      在平常的开发中,总有一些方法我们在不同的地方都有用的,因此我们会把这些方法封装起来.当我们需要在开发一个功能的时候需要用到一个组合函数(多个函数之间有联系,即有一个或多个共同的全局变量)且这个组合 ...

  7. Python - Exceptions

    官方文档:https://docs.python.org/3/library/exceptions.html 1. 使用try...except... 2. 输出错误信息的方式为: try: curs ...

  8. JDK1.9怎么配置环境变量

  9. 如何使用chrome devtool调试Mobile网页?

    凡是做过mobile网页web app开发的朋友一定对开发效率的底下会有吐槽.现在chrome dev tool改变了程序员们的苦比. 0.登录google chrome 1. chrome://in ...

  10. Java笔记-IO流的运用

    --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3877386.html "谢谢-- 1.InputStream和System ...