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 ...
随机推荐
- ch340是什么芯片
CH340 是一个USB 总线的转接芯片,实现USB 转串口.USB 转IrDA 红外或者USB 转打印口. 在串口方式下,CH340 提供常用的MODEM联络信号,用于为计算机扩展异步串口, ...
- bzoj3997组合数学(求最长反链的dp)
组合数学 给出一个网格图,其中某些格子有财宝,每次从左上角出发,只能向下或右走.问至少走多少次才能将财宝捡完.此对此问题变形,假设每个格子中有好多财宝,而每一次经过一个格子至多只能捡走一块财宝,至少走 ...
- Reliability diagrams
Reliability diagrams (Hartmann et al. 2002) are simply graphs of the Observed frequency of an event ...
- PHP中 HTTP_HOST 和 SERVER_NAME 的区别
最近在开发站群软件,用到了根据访问域名判断子站点的相关问题.PHP获取当前域名有两个变量 HTTP_HOST 和 SERVER_NAME,我想知道两者的区别以及哪个更加可靠. 首先我想说,百度上那些转 ...
- VirtualBox 磁盘容量调整
起因 此前用VBox,初始时动态或固定分配的磁盘容量用完了就再重新建一个磁盘,但是有个虚拟机的系统分区容量不够了,把整个磁盘都分成系统盘仍然不够,于是研究了下磁盘容量调整问题. 正文 1,从当前虚拟机 ...
- 邮件实现详解(二)------手工体验smtp和pop3协议
上篇博客我们简单介绍了电子邮件的发送和接收过程,对参与其中的邮件服务器,邮件客户端软件,邮件传输协议也有简单的介绍.我们知道电子邮件需要在邮件客户端和邮件服务器之间,以及两个邮件服务器之间进行传递必须 ...
- 初学者易上手的SSH-struts2 02Action获取表单数据-通配符
在上一章中,我们已经搭建好了struts2的一个开发环境,那么这一章就来做一个简单的登录功能,以及介绍和使用struts2里面一个重要的东西-通配符. 第一步,在WebContent下面新建一个log ...
- 苹果 AR 新专利马上登陆 Facetime|Facebook 要用 VR 玩直播
附上VR技术福利视频 链接: https://pan.baidu.com/s/1boGGVs7 密码: viy8 点击关注有更多VR技术资源哦 苹果 AR 新专利马上登陆 Facetime ,使用光场 ...
- PHP-FPM进程池探秘
PHP 支持多进程而不支持多线程:PHP-FPM 在进程池中运行多个子进程并发处理所有连接请求.通过 ps 查看PHP-FPM进程池(pm.start_servers = 2)状态如下: root@d ...
- 在不用Promise的情况下如何控制异步请求?
如何更好的控制异步请求?相信大家一定首选Promise对象.确实,使用Promise控制异步请求确实非常方便,直接使用then()方法就可以实现当一个异步请求完成后再处理另一个请求或操作.同时,这样的 ...