Pseudoforest(伪最大生成树)
Pseudoforest |
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) |
Total Submission(s): 389 Accepted Submission(s): 165 |
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 |
Sample Output
3 |
Source
“光庭杯”第五届华中北区程序设计邀请赛 暨 WHU第八届程序设计竞赛
|
Recommend
lcy
|
/*
初级想方法,最大生成树再加一条最长边
讲解:没看明白题意的傻逼想法,题目说不是最大生成树,森林也可以,但是最多只能有一个环 正解:将所有的边都加到树上,加的时候如果两点在同一个并查集:如果原来有环就不能加
如果不在同一个并查集:如果原来两个都有环不能加
*/
#include<bits/stdc++.h>
using namespace std;
struct node
{
int u,v,val;
node()
{}
node(int a,int b,int c)
{
u=a;
v=b;
val=c;
}
bool operator < (const node &a) const
{
return val>a.val;
}
};
vector<node>edge;
int bin[];
int n,m;
int x,y,val;
int h[];//表示当前集合有没有环
long long cur=;
void init()
{
for(int i=;i<=n;i++)
{
bin[i]=i;
h[i]=;
}
edge.clear();
cur=;
}
int findx(int x)
{
int temp=x;
while(x!=bin[x])
x=bin[x];
bin[temp]=x;
return x;
}
int main()
{
//freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
while(scanf("%d%d",&n,&m)!=EOF&&(n||m))
{
init();
for(int i=;i<m;i++)
{
scanf("%d%d%d",&x,&y,&val);
edge.push_back(node(x,y,val));
}
sort(edge.begin(),edge.end());
for(int i=;i<edge.size();i++)
{
int fx=findx(edge[i].u);
int fy=findx(edge[i].v);
if(fx==fy)//两个原来就是一个并查集的就可能产生环了
{
if(h[fx])//有环了
continue;
cur+=edge[i].val;
h[fx]=;
}
else
{
if(h[fx]&&h[fy])//两个集合都有环不可以
continue;
bin[fy]=fx;
cur+=edge[i].val;
if(h[fx]||h[fy])
h[fx]=;
}
}
printf("%lld\n",cur);
}
return ;
}
Pseudoforest(伪最大生成树)的更多相关文章
- hdu 3367(Pseudoforest ) (最大生成树)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- hdu 3367 Pseudoforest (最大生成树 最多存在一个环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3367 Pseudoforest Time Limit: 10000/5000 MS (Java/Oth ...
- hdu 3367 Pseudoforest(最大生成树)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- 【hdu3367】Pseudoforest(伪森林)
http://acm.hdu.edu.cn/showproblem.php?pid=3367 题目大意 伪森林就是一个无向图,这个无向图有多个连通块且每个连通块只有一个简单环. 给你一个无向图,让你找 ...
- hdoj--3367--Pseudoforest(伪森林&&最大生成树)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- hdu 3367 Pseudoforest 最大生成树★
#include <cstdio> #include <cstring> #include <vector> #include <algorithm> ...
- ACM:Pseudoforest-并查集-最大生成树-解题报
Pseudoforest Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status ...
- [HDOJ3367]Pseudoforest(并查集,贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3367 求一个无向图上权值最大的伪森林. 伪森林:一个图的连通子图,当且仅当这个子图有且仅有一个环. 既 ...
- hdu 3367 Pseudoforest
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
随机推荐
- CentOS7的一些初始化
默认最小化安装 [root@GVMCET001 ~]# nmtui 设置网络,主机名等 [root@GVMCET001 ~]# yum update 更新系统 SSH [root@GVMCET001 ...
- win7系统Myeclipse下切换SVN用户
Eclipse的SVN插件Subclipse做得很好,在svn操作方面提供了很强大丰富的功能.但到目前为止,该插件对svn用户的概念极为淡薄,不但不能方便地切换用户,而且一旦用户的帐号.密码保存之后 ...
- LInux ugo权限详解
Linux 中的用户和组是用来控制使用者或者进程可以或者不可以使用哪些资源和硬件,是Linux权限控制最基本的方式. 用户和组可以看一下上一章的部分,先来看一下权限. 一.权限概览 在Linux下,使 ...
- VC维含义的个人理解
有关于VC维可以在很多机器学习的理论中见到,它是一个重要的概念.在读<神经网络原理>的时候对一个实例不是很明白,通过这段时间观看斯坦福的机器学习公开课及相关补充材料,又参考了一些网络上的资 ...
- thinkphp增删改查
添加数据: (添加单行数据) // 添加成功返回1,失败返回0 (添加多行数据) // 返回添加数据的条数 删除数据: 修改数据: (修改单个字段) (修改多个字段) // 修改成功返回1,失败返回0 ...
- MyBatis 配置的一些小知识点
MyBatis别名配置——typeAliases 类型别名是为 Java 类型设置一个短的名字.它只和 XML 配置有关,存在的意义仅在于用来减少类完全限定名的冗余.说白了就是预先设置包名 api是这 ...
- Template7插入动态模板
要完成的效果如下图 其中下面添加出来的订单号和订单总价可以看作是接口请求的数据 实现步骤: 1 下载template7:https://github.com/nolimits4web/template ...
- vue2购物车ch1-(安装依赖、简单配置、 axios获取api的模拟数据)
0--项目说明 说明此项目源自某课网购物车教程,但是在开发过程中,发现在开发过程中用的还是 vue-resource(宣布不更新的类$.ajx()插件),为了以后的发展使用axios.js,详情参考 ...
- 实战之elasticsearch集群及filebeat server和logstash server
author:JevonWei 版权声明:原创作品 实战之elasticsearch集群及filebeat server和logstash server 环境 elasticsearch集群节点环境为 ...
- YYModel学习总结YYClassInfo(1)
OC的run-time 机制,简直像是网络上的猫! 我在开发中很少用到,但是作为iOS开发 人家肯定会问这个东西,所以深入的学习了下. 对于 run-time的入手,YYModel的学习,简直让人美滋 ...