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(最大生成树)的更多相关文章

  1. hdu 3367 Pseudoforest 最大生成树★

    #include <cstdio> #include <cstring> #include <vector> #include <algorithm> ...

  2. hdu 3367 Pseudoforest (最大生成树 最多存在一个环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3367 Pseudoforest Time Limit: 10000/5000 MS (Java/Oth ...

  3. hdu 3367 Pseudoforest

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  4. hdu 3367(与最大生成树无关。无关。无关。重要的事情说三遍+kruskal变形)

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  5. HDU 3367 Pseudoforest(Kruskal)

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  6. hdu 3367 Pseudoforest (最小生成树)

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  7. hdu 3367 Pseudoforest(并查集)

    题意:有一种叫作Pseudoforest的结构,表示在无向图上,每一个块中选取至多包含一个环的边的集合,又称“伪森林”.问这个集合中的所有边权之和最大是多少? 分析:如果没有环,那么构造的就是最大生成 ...

  8. hdu 3367(Pseudoforest ) (最大生成树)

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  9. HDU 3367 (伪森林,克鲁斯卡尔)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3367 Pseudoforest Time Limit: 10000/5000 MS (Java/Oth ...

随机推荐

  1. 简单介绍.Net3.0 中跨线程访问控件

    这两天用WPF做一个项目的UI部分时,发现跨线程地访问了UI控件,自然地报异常了.当时找了半天也没在控件中找到InvokeRequired属性和Invoke方法,郁闷之极.....最后发现在.net3 ...

  2. C# 实现WinForm窗口最小化到系统托盘代码,并且判断左右鼠标的事件

    1.设置WinForm窗体属性showinTask=false 2.加notifyicon控件notifyIcon1,为控件notifyIcon1的属性Icon添加一个icon图标. 3.添加窗体最小 ...

  3. Studio for ASP.NET Wijmo:使用 C1Pager 对 DataList 控件分页

    Studio for ASP.NET Wijmo 控件值得称赞的一点是它不仅仅单独为 C1 控件而开发.还可以配合其他控件使用,也正体现了C1控件为提高开发人员工作效率而设计的宗旨.简单的举一个例子, ...

  4. u-boot移植总结(一)start.S分析

    本次移植u-boot-2010.09是基于S3C2440的FL440板子,板子自带NANDFLASH而没有NORFLASH,所以在U-BOOT启动的过程中必须实现从NANDFLASH到SDRAM的重定 ...

  5. 模拟---LCR

    HDU  2778 Description LCR is a simple game for three or more players. Each player starts with three ...

  6. 线段树——Ultra-QuickSort

    题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=109331#problem/A Description In this prob ...

  7. gulp入坑系列(4)——gulp的代码转换

    当然,gulp不仅仅能转换Sass,这里会提到如下转换: jsx转换成常规的JavaScript(说到jsx,玩过react的同学应该是知道的) es6转换为es5 Less,Sass转换为CSS 首 ...

  8. Java对象 json之间的转换(json-lib)

    在这里主要简单的介绍一下,如何使用json-lib这个工具包来完成Java对象(或集合)与json对象(或集合)之间的转换~ 1. Java对象转换成json(既创建json) 关键类:JSONObj ...

  9. js实现拖拽

    拖拽:最核心是三个事件,鼠标按下,鼠标移动,鼠标弹起.按下时激活拖拽,然后时刻根据鼠标的位置来更新物体的left和top值,达到跟随鼠标的效果,鼠标弹起则取消拖拽. 以下是代码: <!DOCTY ...

  10. Win10 FaceAPI小demo开发问题汇总

    Win10 FaceAPI小demo开发问题汇总 最近使用微软牛津计划做一个小demo,使用FaceAPI做一个小应用,实现刷脸的功能.开发的过程中用到几个问题,具体如下: Stream 与IRand ...