Pseudoforest

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1070    Accepted Submission(s): 417 
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
 

Source
 

Recommend
lcy
// 开始我想错了 我是先搞生成树,然后在每个分块加入一条最大边使其 构成环
// 这样是不对的 应该能成环的先让成环 两个成环的就算有边也不连,不然达不到最大的效果
// 这是求最大生成树问题
#include <iostream>
#include <algorithm>
#include <queue>
#include <math.h>
#include <stdio.h>
#include <string.h>
using namespace std;
struct node{
int u,v,c;
bool operator <(const node b) const{
return c>b.c;
}
}Eg[];
int f[];
bool flag[];
int Find(int x){
if(x!=f[x])
f[x]=Find(f[x]);
return f[x];
}
int main()
{
int n,m;
while(scanf("%d %d",&n,&m),n|m){
int i,j,k;
int u,v;
for(i=;i<m;i++)
scanf("%d %d %d",&Eg[i].u,&Eg[i].v,&Eg[i].c);
sort(Eg,Eg+m);
int sum=;
for(i=;i<n;i++) f[i]=i,flag[i]=false;
for(i=;i<m;i++){
u=Eg[i].u;
v=Eg[i].v;
u=Find(u);
v=Find(v);
if(u!=v){
if(!flag[u]&&!flag[v]){
f[u]=v;
sum+=Eg[i].c;
}
else if(!flag[u]||!flag[v]){
f[u]=v;
sum+=Eg[i].c;
flag[u]=flag[v]=true;
}
}
else{
if(!flag[u]){
flag[u]=true;
sum+=Eg[i].c;
}
}
}
printf("%d\n",sum);
} return ;
}

hdu 3367 Pseudoforest的更多相关文章

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

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

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

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

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

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

  4. HDU 3367 Pseudoforest(Kruskal)

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

  5. hdu 3367 Pseudoforest(并查集)

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

  6. hdu 3367 Pseudoforest 最大生成树★

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

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

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

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

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

  9. hdu 3367(与最大生成树无关。无关。无关。重要的事情说三遍+kruskal变形)

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

随机推荐

  1. 给UITextField左侧加一个小图标

    xcode 6.1 File-New-Project.. iOs-Application-Simple View Application 1.写一个自定义JSTextField类,继承UITextFi ...

  2. 【网络】 NAT

    NAT(Network Address Translation,网络地址转换)是1994年提出的.当在专用网内部的一些主机本来已经分配到了本地IP地址(即仅在本专用网内使用的专用地址),但现在又想和因 ...

  3. 2016 系统设计第一期 (档案一)MVC bootstrap model弹出窗

    局部代码: <!-- 按钮触发模态框 --> <div style=""> <button class="btn btn-primary&q ...

  4. ios登陆界面

    代码较老,仅供参考 主要涉及的功能点有: 1.密码输入框要隐藏输入字符,以黑点代替,有时候会在边上设置一个按钮,让用户选择是否需要密文输入 2.Login时会检查输入框,若输入不合法,弹窗提示用户 3 ...

  5. Linux下反斜杠号"\"引发的思考

    今天在检查home目录时发现有一个名为“\”的文件,觉得很奇怪,从来没见过,就准备用Vim打开看看,很自然地输入命令查看一下,结果居然打不开. ubuntu@ubuntu:~$ vi \> ub ...

  6. C# DataTable

    http://www.cnblogs.com/xun126/archive/2010/12/30/1921557.html http://msdn.microsoft.com/zh-cn/librar ...

  7. 1201: [HNOI2005]数三角形 - BZOJ

    Description Input 大三角形的所有短边可以看成由(n+1)*n/2个单位三角形的边界组成.如下图的灰色三角形所示.其中第1排有1个灰色三角形,第2排有2个灰色三角形,……,第n排有n个 ...

  8. Shell命令合集

    Ccat zdd 浏览文件zdd的内容cat zdd1 zdd2 浏览多个文件的内容cat -n zdd浏览文件zdd的内容并显示行号 cd 回到起始目录,也即刚登陆到系统的目录,cd后面无参数cd ...

  9. C++ 嵌套类使用(二)

    C++嵌套类 1.   嵌套类的名字只在外围类可见. 2.   类的私有成员只有类的成员和友元可以访问,因此外围类不可以访问嵌套类的私有成员.嵌套类可以访问外围类的成员(通过对象.指针或者引用). 3 ...

  10. c++实现文本中英文单词和汉字字符的统计

    源代码下载:http://download.csdn.net/detail/nuptboyzhb/4987141 1.统计文本中汉字的频数,为后续的文本分类做基础.对于汉字的统计,需要判断读取的是否为 ...