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 ...
随机推荐
- jquery对象和js对象
<ul id="ul1"> <li id="li_1">01</li> <li>02</li> ...
- oracle 常用函数汇总
一.字符函数字符函数是oracle中最常用的函数,我们来看看有哪些字符函数:lower(char):将字符串转化为小写的格式.upper(char):将字符串转化为大写的格式.length(char) ...
- IOS7 点击空白处隐藏键盘的几种方法
IOS7 点击空白处隐藏键盘的几种方法 iOS开发中经常要用到输入框,默认情况下点击输入框就会弹出键盘,但是必须要实现输入框return的委托方法才能取消键盘的显示,对于用户体验来说很不友好,我们 ...
- struts标签与jstl标签互换
近期在做struts切换spring mvc时发现代码中使用了大量的struts标签,对常用的struts标签做了总结,首先需要引入 <%@ taglib prefix="c" ...
- EOutOfResources字符异常
近日,用Delphi编程时,遇到一个莫名其妙的异常:EOutOfResources,这是一个可以重复再现的异常.开始以为是程序中创建的对象太多,导致占用了过多的资源,引起了这个异常.于是在代码中将许多 ...
- TComboBox组件重要属性和事件
TComboBox组件的重要属性 CharCase--------此属性用于设置编辑框内文字的大小写 DropDownCount---此属性用于设置当用户下拉组合框时不需要加滚动条就能显示的项的个数 ...
- ElasticSearch入门(3) —— head插件
#### 安装ES head插件 具体请参考github地址:https://github.com/mobz/elasticsearch-head 使用 安装Install # 在线安装head插件 ...
- Java并发/多线程系列——线程安全篇(1)
创建和启动Java线程 Java线程是个对象,和其他任何的Java对象一样.线程是类的实例java.lang.Thread,或该类的子类的实例.除了对象之外,java线程还可以执行代码. 创建和启动线 ...
- Drying poj3104(二分)
Drying Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7916 Accepted: 2006 Descriptio ...
- 1042 数字0-9的数量 1050 循环数组最大子段和 1062 序列中最大的数 1067 Bash游戏 V2 1092 回文字符串
1042 数字0-9的数量 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 给出一段区间a-b,统计这个区间内0-9出现的次数. 比如 10-19,1出现11次 ...