Pseudoforest

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

Total Submission(s): 2382    Accepted Submission(s): 933

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 求出一个最大的子图(子图的每个连通分量最多有一个环) 用kruskal算法求出最大生成树  不过要判断是否有环  2树合并时 :若2个子树都有环不能合并 只有一个有环可以合并 但合并后的树有环 若2个子树都没环直接合并
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <stdio.h> using namespace std;
#define MAX 10000
int n,m;
struct Node
{
int x;
int y;
int w;
}a[MAX*10+5];
int father[MAX+5];
bool tag[MAX+5];
int cmp(Node a,Node b)
{
return a.w>b.w;
}
int find(int x)
{
if(x!=father[x])
father[x]=find(father[x]);
return father[x];
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0)
break;
for(int i=0;i<m;i++)
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);
for(int i=0;i<n;i++)
{
father[i]=i;
tag[i]=false;
}
sort(a,a+m,cmp);
int ans=0;
for(int i=0;i<m;i++)
{
int xx=find(a[i].x);
int yy=find(a[i].y);
if(xx!=yy)
{
if(tag[xx]&&tag[yy])
continue;
ans+=a[i].w;
father[xx]=yy;
if(tag[xx]||tag[yy])
{tag[xx]=true;tag[yy]=true;}
}
else
{
if(tag[xx]||tag[yy])
continue;
ans+=a[i].w;
tag[xx]=true;tag[yy]=true;
}
}
printf("%d\n",ans);
}
return 0;
}

HDU 3367 Pseudoforest(Kruskal)的更多相关文章

  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)Tot ...

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

  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. zookeeper(五):Zookeeper中的Access Control(ACL)

    概述 传统的文件系统中,ACL分为两个维度,一个是属组,一个是权限,子目录/文件默认继承父目录的ACL.而在Zookeeper中,node的ACL是没有继承关系的,是独立控制的. Zookeeper的 ...

  2. atitit. it软件项目管理---自己的员工,雇佣军、援军,混合的员工 杂牌 人员管理架构

    atitit. it软件项目管理---自己的员工,雇佣军.援军,混合的员工 杂牌 人员管理架构 1. 企业的正规军,雇佣军,杂牌划分 1 1.1. 企业的员工基本是雇佣而来 1 1.2. 全职员工与兼 ...

  3. hdu1569 方格取数(2) 最大点权独立集=总权和-最小点权覆盖集 (最小点权覆盖集=最小割=最大流)

    /** 转自:http://blog.csdn.net/u011498819/article/details/20772147 题目:hdu1569 方格取数(2) 链接:https://vjudge ...

  4. MapReduce实战(三)分区的实现

    需求: 在实战(一)的基础 上,实现自定义分组机制.例如根据手机号的不同,分成不同的省份,然后在不同的reduce上面跑,最后生成的结果分别存在不同的文件中. 对流量原始日志进行流量统计,将不同省份的 ...

  5. nodejs 聊天室简单实现

    前言 博客园的样式真心不会用啊,看着大大们的博客各种好看,心里无奈啊,只能慢慢摸索了. 最近的项目nodejs+wcf+app,app直接从wcf服务获取数据,nodejs作为单独的服务器为app提供 ...

  6. Cmake实现样例

    多目录工程的CmakeLists.txt编写(自动添加多目录下的文件) http://www.cnblogs.com/chengxuyuancc/p/5347646.html 实现类似于vs中工程的C ...

  7. 请说出ArrayList,Vector, LinkedList的存储性能和特性

    请说出ArrayList,Vector, LinkedList的存储性能和特性 解答:ArrayList和Vector都是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都 ...

  8. 2018 ACM-ICPC 北京赛区小结 @ Reconquista

    Statistics TYPE: Onsite Contest NAME: 2018 - ICPC Regional - Asia EC - Beijing PLAT: Hihocoder TIME: ...

  9. mysql中RAND()随便查询记录效率问题和解决的方法分享

    在我们做开发的中效率一直是个问题,特别是对于非常多大数据量操作,今天我们碰到一个要随机查询数据,一開始我们可能想到最简单的order by rand() 来操作但效率不敢恭维啊 近期因为须要大概研究了 ...

  10. 卡夫卡(kafka)

    1.Kafka独特设计在什么地方?2.Kafka如何搭建及创建topic.发送消息.消费消息?3.如何书写Kafka程序?4.数据传输的事务定义有哪三种?5.Kafka判断一个节点是否活着有哪两个条件 ...