Description

In graph theory, a pseudoforest is an undirected graph in which every connected component has at most one cycle. The maximal pseudoforests of G are the pseudoforest subgraphs of G that are not contained within any larger pseudoforest of G. A pesudoforest is larger than another if and only if the total value of the edges is greater than another one’s.

 

Input

The input consists of multiple test cases. The first line of each test case contains two integers, n(0 < n <= 10000), m(0 <= m <= 100000), which are the number of the vertexes and the number of the edges. The next m lines, each line consists of three integers, u, v, c, which means there is an edge with value c (0 < c <= 10000) between u and v. You can assume that there are no loop and no multiple edges. 
The last test case is followed by a line containing two zeros, which means the end of the input. 
 

Output

Output the sum of the value of the edges of the maximum pesudoforest. 
 

Sample Input

3 3
0 1 1
1 2 1
2 0 1
4 5
0 1 1
1 2 1
2 3 1
3 0 1
0 2 2
0 0
 

Sample Output

3
5
 
说说题目意思是必要的,毕竟小笼包都给我解释了好久,真是难以让人理解,题目意思就是合并出一个假森林出来,这个森林的环只能有一个,所谓环就是比如当0, 1, 2建立集合关系的时候
这个时候2已经链接到0了,也就是说0是可以到2的,这个时候如果系统在给你一组数据类似于2, 0的时候,继续合并,则0到1到2再到0,把它想成一圈,是不是就是一个环了,好了,理解这里应该就没有什么难度了,接下来的主要问题就是合并时的判断了,当两个节点同属于一个集合的时候,看看这个集合已经形成环了没有,如果形成了,就不能加入了,反之则可以,还有一种情况就是
两个节点分别属于不同的集合,因为合并的时候要考虑环的数量,所以当两个集合合并后新集合的环数量超过1也是不行的,还有一些问题我会在注释中注明:
 
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<queue>
  4. #include<cmath>
  5. #include<algorithm>
  6. #include<string>
  7. #include<iostream>
  8. using namespace std;
  9.  
  10. const int MX = 111111;
  11. int road[MX];
  12. int sign[MX];
  13. int rec[MX];
  14. int n, m;
  15.  
  16. struct Node {
  17. int a, b, c;
  18. }node[MX];
  19.  
  20. bool comp(const Node& n1, const Node& n2) {
  21. return n1.c > n2.c;//对价值进行排序,优先考虑放大的,贪心啦
  22. }
  23.  
  24. void ini() {
  25. for (int i = 0; i < n; i++) {
  26. road[i] = i;
  27. sign[i] = 0;//注意这个是用来标记环的数量的
  28. rec[i] = 1;//注意这个是用来标记集合中的元素个数的,因为我采用了新的合并方法,就是把小集合合并到大集合,当然你也不用在意这种细节啦,你可以继续使用自己的合并方式
  29. }
  30. }
  31.  
  32. int FindRoot(int r) {//在使用路径压缩查找跟节点的时候我没有使用递归了,主要是不好进行各种标记
  33. int root = r;
  34. while (road[root] != root) root = road[root];
  35.  
  36. int t1 = r;
  37. int t2 = r;
  38. while (road[t1] != root) {
  39. t2 = road[t1];
  40. road[t1] = root;
  41. sign[t1] = sign[root];
  42. t1 = t2;
  43. }
  44. return root;
  45. }
  46.  
  47. int UnionRoot(int root1, int root2) {//基本的合并,一看就懂啦,看不懂就继续看- -
  48. if (rec[root1] >= rec[root2]) {
  49. road[root2] = root1;
  50. rec[root1]++;
  51. return root1;
  52. } else {
  53. road[root1] = root2;
  54. rec[root2]++;
  55. return root2;
  56. }
  57. }
  58.  
  59. int main()
  60. {
  61. //freopen("input.txt", "r", stdin);
  62. while (scanf("%d%d", &n, &m), n || m) {
  63. ini();
  64. for (int i = 0; i < m; i++) {
  65. scanf("%d%d%d", &node[i].a, &node[i].b, &node[i].c);
  66. }
  67. sort(node, node + m, comp);//基本的贪心思想
  68. int ans = 0;
  69. for (int i = 0; i < m; i++) {
  70. int root1 = FindRoot(node[i].a);
  71. int root2 = FindRoot(node[i].b);
  72. if (root1 == root2 && sign[root1] == 0) {//当两个节点同属于一个集合的时候,看看这个集合已经形成环了没有,如果形成了,就不能加入了,反之则可以
  73. ans += node[i].c;
  74. sign[root1] = 1;
  75. } else {
  76. if (sign[root1] != 1 || sign[root2] != 1) {//两个节点分别属于不同的集合,因为合并的时候要考虑环的数量,所以当两个集合合并后新集合的环数量超过1也是不行的
  77. ans += node[i].c;
  78. int r = UnionRoot(root1, root2);
  79. if (sign[root1] == 1 || sign[root2] == 1) {
  80. sign[r] = 1;
  81. }
  82. }
  83. }
  84. }
  85. printf("%d\n", ans);
  86. }
  87. return 0;
  88. }
 
 
 
 
 

HDU - Pseudoforest的更多相关文章

  1. hdu 3367(Pseudoforest ) (最大生成树)

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  2. hdu 3367 Pseudoforest (最大生成树 最多存在一个环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3367 Pseudoforest Time Limit: 10000/5000 MS (Java/Oth ...

  3. hdu 3367 Pseudoforest(最大生成树)

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  4. hdu 3367 Pseudoforest

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  5. hdu 3367 Pseudoforest (最小生成树)

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  6. HDU 3367 Pseudoforest(Kruskal)

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  7. hdu 3367 Pseudoforest(并查集)

    题意:有一种叫作Pseudoforest的结构,表示在无向图上,每一个块中选取至多包含一个环的边的集合,又称“伪森林”.问这个集合中的所有边权之和最大是多少? 分析:如果没有环,那么构造的就是最大生成 ...

  8. hdu 3367 Pseudoforest 最大生成树★

    #include <cstdio> #include <cstring> #include <vector> #include <algorithm> ...

  9. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

随机推荐

  1. 【转载】 JQuery.Gantt(甘特图) 开发指南

    转载来自: http://www.cnblogs.com/liusuqi/archive/2013/06/09/3129293.html JQuery.Gantt是一个开源的基于JQuery库的用于实 ...

  2. ArcGIS中的三种查询

    ArcGIS runtime SDK for WPF/Silverlight中的三种常用的查询:QueryTask.FindTask.IdentifyTask都是继承自ESRI.ArcGIS.Clie ...

  3. Creating a Table View Programmatically

    https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/Cre ...

  4. PHPCMS V9 点击量排行调用方法

    首先调用的标签代码如下: {pc:content action=”sitehits” siteid=”4″ num=”10″ order=”views DESC” cache=”3600″} {loo ...

  5. WPF程序最小化到任务通知栏

    我们通常使用的桌面软件,都可以最小化到任务通知栏,并且可以从任务通知栏再打开当前软件,或者通过软件的快捷方式从任务通知栏呼出. 我们可以通过下面的方式把WPF程序最小化到任务栏.由于WPF并没有实现N ...

  6. 如何设计一个优秀的API(转)

    到目前为止,已经负责API接近两年了,这两年中发现现有的API存在的问题越来越多,但很多API一旦发布后就不再能修改了,即时升级和维护是必须的.一旦API发生变化,就可能对相关的调用者带来巨大的代价, ...

  7. 批量删除SharePoint 2010的List中的item

    第一种方式:循环遍历List中的所有item,然后根据条件去判断当前item是否应该被删除[注:要用 i-- 方式去遍历,这也是删除集合里面item的常用做法,如果用 i++ 的方式去遍历删除,会出错 ...

  8. System.Web.Caching.Cache类 缓存

    1.文件缓存依赖 public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender ...

  9. 【java 断点续传】

    模拟 断点续传 首先,先读取word文件的 一部分 package com.sxd.readLines; import java.io.File; import java.io.FileInputSt ...

  10. 【SQL 触发器】

    一.MySQL上触发器的使用 示例: CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW t ...