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 ...
随机推荐
- 转自知乎(JAVA后台开发可以纯粹用JAVA SE吗?)
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:巴多崽链接:http://www.zhihu.com/question/29663744/answer/45154839来源: ...
- 第一个asp.net MVC5+ExtJS6入门案例项目
最近在学习asp.net MVC,结合前段时间学习的ExtJS,做了一个入门示例.不过还有一个json日期显示的问题没有解决. [思路] 1.先搭建一个asp.net MVC项目. 2.将MVC项目的 ...
- ThinkPHP中:检查Session是否过期
1.创建Session public function index(){ $sess_time=time(); session('name','andy'); session('time_stamp' ...
- Linux学习——Shell基础
1 shell概述 Shell 是一个命令行解释器,它为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以用shell来启动,挂起,停止甚至编写一些程序. Shell 还是一 ...
- Codeforces 845 C. Two TVs 思路:简单贪心算法
题目: 题目原文链接:http://codeforces.com/contest/845/problem/C 题意:现在我们有一个电视清单,有两个电视,电视清单上有每一个节目的开始时间和结束时间. 电 ...
- PIC32MZ 通过USB在线升级 -- USB HID bootloader
了解 bootloader 的实现, 请加QQ: 1273623966(验证填bootloader); 欢迎咨询或定制bootloader; 我的博客主页 www.cnblogs.com/geekyg ...
- Java IO编程全解(一)——Java的I/O演进之路
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7419117.html JDK1.4之前的早期版本,Java对I/O的支持并不完善,开发人员在开发高性能I/O ...
- exp3.1实现顺序栈的各种操作
#include<iostream>using namespace std;#include<malloc.h>typedef char Elem;typedef struct ...
- Android 屏幕相关概念(1)
1. 术语和概念 术语 说明 备注 Screen size(屏幕尺寸) 指的是手机实际的物理尺寸,比如常用的2.8英寸,3.2英寸,3.5英寸,3.7英寸 摩托罗拉milestone手机是3. ...
- python虚拟环境的安装配置
安装 使用pip安装 pip install virtualenv 因为已经安装过了,所以显示这样 在这里我想在这里推荐大家以后再安装类库时可以用豆瓣源来安装,速度很快,因为在国内访问 官方p ...