Pseudoforest

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1705    Accepted Submission(s): 653

Problem 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

题意:n个点,给你n条边,要求你连一些边,是边权总和最大(整个图可以不连通,对于每个连通子图最多只能有一个环)

::按照最大生成树的做法,只要在连接同一个连通子图的两个结点是判断该子图是否已有环,没有才可以连接。

在连接不同连通子图的两个结点是判断两个连通子图的环数之和是否小于2,满足条件就可连接。

  1. view code#include <iostream>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <cstdio>
  5. using namespace std;
  6. #define REP(i,n) for(int i=0; i<(n); i++)
  7. const int N = 10010;
  8. int n, m, fa[N], num[N];
  9.  
  10. struct edge
  11. {
  12. int u, v, w;
  13. bool operator < (const edge &o) const {
  14. return w>o.w;
  15. }
  16. }e[N*10];
  17.  
  18. int find(int x)
  19. {
  20. return x==fa[x]?x:(fa[x]=find(fa[x]));
  21. }
  22.  
  23. void solve()
  24. {
  25. REP(i,n) fa[i] = i, num[i] = 0;
  26. REP(i,m) scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
  27. sort(e, e+m);
  28. int ans = 0;
  29. REP(i, m)
  30. {
  31. int u = find(e[i].u), v =find(e[i].v);
  32. if(u==v && num[v]==0)
  33. ans += e[i].w, num[v]++;
  34. else if(u!=v && num[v]+num[u]<2)
  35. fa[u] = v, num[v]+= num[u], ans += e[i].w;
  36. }
  37. printf("%d\n", ans);
  38. }
  39.  
  40. int main()
  41. {
  42. // freopen("in.txt", "r", stdin);
  43. while(cin>>n>>m &&(n|m) ) solve();
  44. return 0;
  45. }

hdu 3367 Pseudoforest(最大生成树)的更多相关文章

  1. hdu 3367 Pseudoforest 最大生成树★

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

  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(与最大生成树无关。无关。无关。重要的事情说三遍+kruskal变形)

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

  5. HDU 3367 Pseudoforest(Kruskal)

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

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

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

  7. hdu 3367 Pseudoforest(并查集)

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

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

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

  9. HDU 3367 (伪森林,克鲁斯卡尔)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3367 Pseudoforest Time Limit: 10000/5000 MS (Java/Oth ...

随机推荐

  1. vs2015 Android SDK

    It was not possible to complete an automatic installation. This might be due to a problem with your ...

  2. vs2012 编译运行报 项目程序包Dsp.Bds.dll还原失败,找不到版本xxxxxx的程序包问题的解决方法

    原先vs2012程序运行项目是可以了,不会出现程序包Dsp.Bds.dll还原失败,找不到版本未2.0.1.0的程序包问题, 但是项目可以正常运行,每次调试看到下面一片红感觉不舒服 原因:可能当时我看 ...

  3. 重新想象 Windows 8 Store Apps (51) - 输入: 涂鸦板

    [源码下载] 重新想象 Windows 8 Store Apps (51) - 输入: 涂鸦板 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 涂鸦板 通过 Poin ...

  4. 与众不同 windows phone (36) - 8.0 新的瓷贴: FlipTile, CycleTile, IconicTile

    [源码下载] 与众不同 windows phone (36) - 8.0 新的瓷贴: FlipTile, CycleTile, IconicTile 作者:webabcd 介绍与众不同 windows ...

  5. csharp: json to csharp

    http://json2csharp.com/ http://jsonclassgenerator.codeplex.com/ http://jsonutils.com/ JSON生成类文件 http ...

  6. Studio for WPF:使用 C1TileView 创建图片库

    C1TileView 提供了数据交互浏览的功能.允许我们设置最大化和最小化浏览模板,我们可以通过最小化模板快速定位详细浏览选项. 下面我们分步分享实现方法: 1.添加 C1TileView 到窗体,并 ...

  7. sql 去重

    ;WITH CETAS (SELECT *, ROW_NUMBER() OVER (PARTITION BY SearchTask_PKID ORDER BY SearchTask_PKID) Row ...

  8. ES6的Class

    类的基本写法: constructor构造函数其实就相当于ES5中的构造函数,用于定义类的实例属性: 而在类中定义的其他方法像这里的toString方法就相当于ES5中定义在原型prototype上的 ...

  9. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q32-Q34)

    Question 32You create a custom Web Part.You need to ensure that a custom property is visible in Edit ...

  10. 【读书笔记】iOS-垃圾回收

    Objective-C的垃圾回收器是一种继承性的垃圾回收器.与那些已经存在了一段时间的对象相比,新创建的对象更可能被当成垃圾.垃圾回收器定期检查变量和对象以及它们之间的指针,当发现没有任何变量指向某个 ...