Pseudoforest

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

Total Submission(s): 2311    Accepted Submission(s): 892
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   |   We have carefully selected several similar problems for you:  3362 3368 3364 3365 3369 

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
int n,m,minn,vis[10000+10],pre[10000+10];
struct node
{
int u,v,val;
}edge[100000+10];
void init()
{
for(int i=0;i<10010;i++)
pre[i]=i;
}
int cmp(node s1,node s2)
{
return s1.val>s2.val;
}
int find(int x)
{
return pre[x]==x?x:pre[x]=find(pre[x]);
}
int main()
{
while(scanf("%d%d",&n,&m),n||m)
{
init();
for(int i=0;i<m;i++)
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].val);
sort(edge,edge+m,cmp);
minn=0;
memset(vis,0,sizeof(vis));
for(int i=0;i<m;i++)
{
int fx=find(edge[i].u);
int fy=find(edge[i].v);
if(fx!=fy)
{
if(!vis[fx]&&!vis[fy])
{
minn+=edge[i].val;
pre[fx]=fy;
}
else if(!vis[fx]||!vis[fy])
{
minn+=edge[i].val;
pre[fx]=fy;
vis[fx]=vis[fy]=1;
}
}
else
{
if(!vis[fx]&&!vis[fy])
{
minn+=edge[i].val;
vis[fx]=vis[fy]=1;
}
}
}
printf("%d\n",minn);
}
return 0;
}

hdoj--3367--Pseudoforest(伪森林&&最大生成树)的更多相关文章

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

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

  2. 【hdu3367】Pseudoforest(伪森林)

    http://acm.hdu.edu.cn/showproblem.php?pid=3367 题目大意 伪森林就是一个无向图,这个无向图有多个连通块且每个连通块只有一个简单环. 给你一个无向图,让你找 ...

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

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

  4. hdu3367最大伪森林(并查集)

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/3367/ 题目要求一个连通图的最大伪森林,伪森林是一个最多有一个回路的图.我们只要用Kruskal最大生成树的策略 ...

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

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

  6. hdu 3367 Pseudoforest(并查集)

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

  7. Pseudoforest(伪最大生成树)

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

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

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

  9. 【HDOJ】3367 Pseudoforest

    并查集. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 10005 #d ...

随机推荐

  1. Jquery 重置表单

    1.重置表单回初始状态 $('#fromid')[0].reset(); 此方法一步到位,不需要一个个的去赋值为空

  2. Choerodon 的微服务之路(二):Choerodon 的微服务网关

    链接地址:https://my.oschina.net/choerodon/blog/2254030

  3. zookeeper单机和奇数集群

    zookeeper单机和奇数集群 链接地址:https://www.cnblogs.com/lsdb/p/7297731.html

  4. 关于FastIo

    由于你的驱动将要绑定到文件系统驱动的上边,文件系统除了处理正常的IRP 之外,还要处理所谓的FastIo.FastIo是Cache Manager 调用所引发的一种没有irp 的请求.换句话说,除了正 ...

  5. MongoDB_"Error parsing YAML config file: yaml-cpp: error at line 3, column 9: illegal map value"解决方法

    在启动配置文件的时候,系统报错:Error parsing YAML config file: yaml-cpp: error at line 3, column 9: illegal map val ...

  6. 匈牙利&&EK算法(写给自己看)

    (写给自己看)匈牙利算法(最大匹配)和KM算法(最佳匹配) 匈牙利算法 思想 不断寻找增广路,每次寻得增广路,交换匹配边和非匹配边,则匹配点数+1 这里增广路含义:交错路,即从未匹配点出发经过未匹配边 ...

  7. 解决linux 升级高版本python3.7后yum不能使用的问题

    我们linux系统一般自带python2.7 版本,但是最近项目需求必须要上python3以上,对于用惯了python2的我来说,只能硬着头皮上了.下面是我的解决办法 which yum => ...

  8. sh脚本文件

    echo `date +%F`  打印当前日期  用于写备份脚本按照名字命名

  9. nyoj212-k尾相等数

    212-K尾相等数 内存限制:64MB时间限制:3000msSpecial Judge: No accepted:0submit:0 题目描述: 输入一个自然数K(K>1),如果存在自然数M和N ...

  10. nyoj312-20岁生日

    20岁生日 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描述 路过这的20岁生日就要到了,他当然很开心,可是他突然想到一个问题,是不是每个人从出生开始,到达20岁生日时所经 ...