hdu 3367 Pseudoforest(最大生成树)
Pseudoforest
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1705 Accepted Submission(s): 653
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
题意:n个点,给你n条边,要求你连一些边,是边权总和最大(整个图可以不连通,对于每个连通子图最多只能有一个环)
::按照最大生成树的做法,只要在连接同一个连通子图的两个结点是判断该子图是否已有环,没有才可以连接。
在连接不同连通子图的两个结点是判断两个连通子图的环数之和是否小于2,满足条件就可连接。

view code#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
#define REP(i,n) for(int i=0; i<(n); i++)
const int N = 10010;
int n, m, fa[N], num[N]; struct edge
{
int u, v, w;
bool operator < (const edge &o) const {
return w>o.w;
}
}e[N*10]; int find(int x)
{
return x==fa[x]?x:(fa[x]=find(fa[x]));
} void solve()
{
REP(i,n) fa[i] = i, num[i] = 0;
REP(i,m) scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
sort(e, e+m);
int ans = 0;
REP(i, m)
{
int u = find(e[i].u), v =find(e[i].v);
if(u==v && num[v]==0)
ans += e[i].w, num[v]++;
else if(u!=v && num[v]+num[u]<2)
fa[u] = v, num[v]+= num[u], ans += e[i].w;
}
printf("%d\n", ans);
} int main()
{
// freopen("in.txt", "r", stdin);
while(cin>>n>>m &&(n|m) ) solve();
return 0;
}
hdu 3367 Pseudoforest(最大生成树)的更多相关文章
- hdu 3367 Pseudoforest 最大生成树★
#include <cstdio> #include <cstring> #include <vector> #include <algorithm> ...
- 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 ...
- hdu 3367(与最大生成树无关。无关。无关。重要的事情说三遍+kruskal变形)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- HDU 3367 Pseudoforest(Kruskal)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- hdu 3367 Pseudoforest (最小生成树)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- hdu 3367 Pseudoforest(并查集)
题意:有一种叫作Pseudoforest的结构,表示在无向图上,每一个块中选取至多包含一个环的边的集合,又称“伪森林”.问这个集合中的所有边权之和最大是多少? 分析:如果没有环,那么构造的就是最大生成 ...
- hdu 3367(Pseudoforest ) (最大生成树)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- HDU 3367 (伪森林,克鲁斯卡尔)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3367 Pseudoforest Time Limit: 10000/5000 MS (Java/Oth ...
随机推荐
- Windows 8.1 去掉库的方法
Windows 8.1[这台电脑], 里面又多了[文件夹]分类,真是各种不习惯 删除方法: 打开注册表, 找到 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Wind ...
- 《Node.js+MongoDB+AngularJS Web开发》读书笔记及联想
总体介绍 <Node.js+MongoDB+AngularJS Web开发>,于2015年6月出版,是一本翻译过来的书,原书名为<Node.js,MongoDB and Angula ...
- Python基础:模块
一.概述 二.导入语句 1.基本语法 2.推荐风格 三.模块 1.模块名 2.模块属性 3.可导出的公有属性 4.直接执行 四.包 1.包名 2.包属性 3.可导出的公有属性 4.其他 五.导入原理 ...
- C#中ListView的简单使用方法
ListView是用于显示数据的,先在窗体中拉一个lisview控件,还有一些新增.修改.删除.查询按钮和文本框,控件名称为listview,按钮为btnInsert,btnUpate,btnDele ...
- C# winform调用浏览器打开页面方法分享,希望对大家有帮助
在很多客户端程序中我们都需要调用浏览器打开网页,这里分享一个可以在我winform程序调用浏览器的方法,测试通过了. 声明:这个方法是上万个用户测试通过的,不是我没有测试通过就拿出来分享,那个是自己搬 ...
- PHP的静态变量和引用函数
直接贴代码,结果的原因写在备注了 <?php /** * Created by PhpStorm. * User: Administrator * Date: 16-8-25 * Time: 上 ...
- Java中处理异常中return关键字
Java中,执行try-catch-finally语句需要注意: 第一:return语句并不是函数的最终出口,如果有finally语句,这在return之后还会执行finally(return的值会暂 ...
- linux多线程-互斥&条件变量与同步
多线程代码问题描述 我们都知道,进程是操作系统对运行程序资源分配的基本单位,而线程是程序逻辑,调用的基本单位.在多线程的程序中,多个线程共享临界区资源,那么就会有问题: 比如 #include < ...
- Linux_Centos中搭建nexus私服
1.在Linux下搭建Nexus私服 1).下载并且解压 下载 nexus-2.11.2-03-bundle.zip unzip nexus-2.11.2-03-bundle.z ...
- HTML 块元素
分为3类 1. 结构块 只能包含块级元素.它们包含结构含义,但没有语义含义,也就是,不能说明内容是什么,只能说明其组织方式. <ol> <ul> <dl> < ...