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. ENVI/IDL中.sav和.pro文件用法的不同

    软件envi5.0 sp3 .sav是IDL binaryfile。 安装方法:把ENVIProgramGenerator.sav文件拷贝到…\Exelis\ENVI50\extensions\文件夹 ...

  2. 用JS给浏览器的关闭按钮添加事件

    以下是指在js中实现,而非 <body onunload="close()"> 这种方法! 因为这样是在unload掉body的时候触发,而无论任何浏览器,都会在关闭的 ...

  3. parsing html in asp.net

    http://stackoverflow.com/questions/5307374/parsing-html-page-in-asp-net http://htmlagilitypack.codep ...

  4. 有关hadoop分布式配置详解

    linux配置ssh无密码登录 配置ssh无密码登录,先要安装openssh,如下: yum install openssh-clients 准备两台linux服务器或虚拟机,设置两台linux的ho ...

  5. iOS工程中的info.plist文件

    我们建立一个工程后,会在Supporting files下面看到一个"工程名-Info.plist"的文件,这个是对工程做一些运行期配置的文件,很重要,不能删除. 如果你在网上下载 ...

  6. GPS导航仪常见术语解释

    摘自百度百科: 坐标(coordinate) 有2维.3维两种坐标表示,当GPS能够收到4颗及以上卫星的信号时,它能计算出本地的3维坐标:经度.纬度.高度,若只能收到3颗卫星的信号,它只能计算出2维坐 ...

  7. log4j示例

    示例代码:此示例从控制台输入日志,设置了输出等级. # # Log4J Settings for log4j 1.2.x (via jakarta-commons-logging) # # The f ...

  8. winform保存登录cookie

       在web程序中,我们通常使会使用cookie来保存一些用户状态,或者权限或者你想保存的东西,但是在CS程序中,如果要使用cookie就必须要做些功课了... 最好注意以下几点:   1.使用成员 ...

  9. PAT-乙级-1019. 数字黑洞 (20)

    1019. 数字黑洞 (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定任一个各位数字不完全相同的4位 ...

  10. -webkit-text-size-adjust: none;该如何处理

    -webkit-text-size-adjust: none; 在中文版Chrome里面,网页CSS里所有小于12px的字体设置都无效,最终将显示12px.这样弄的本意可能 是好的,因为中文一旦小于1 ...