Pseudoforest

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

Total Submission(s): 1729    Accepted Submission(s): 661
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
 
Source

题意:这题的题意折腾了好久才懂,開始以为是求最多有一个环的最大联通分量,然后WA了,后来才知道是求多个分量的最大和。每一个分量最多有一个环。

题解:边权从大到小排序,对于每条边的起点终点a、b有三种情况:

1、a、b分属两个环。此时a、b不能连接;

2、a、b当中一个属于环。此时将还有一个连过去或者两个都不在环中,此时随意连;

3、a、b在同一集合。但该集合无环。此时连接a、b并生成环。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define maxn 10002
#define maxm 100002
using std::sort; struct Node{
int u, v, cost;
} E[maxm];
int pre[maxn];
bool Ring[maxn]; bool cmp(Node a, Node b){
return a.cost > b.cost;
} int ufind(int k)
{
int a = k, b;
while(pre[k] != -1) k = pre[k];
while(a != k){
b = pre[a];
pre[a] = k;
a = b;
}
return k;
} int greedy(int n, int m)
{
int ans = 0, i, u, v;
for(i = 0; i < m; ++i){
u = E[i].u; v = E[i].v;
u = ufind(u); v = ufind(v);
if(Ring[u] && Ring[v]) continue;
if(u != v){
if(Ring[v]) pre[u] = v;
else pre[v] = u;
ans += E[i].cost;
}else if(Ring[v] == false){
Ring[v] = true;
ans += E[i].cost;
}
}
return ans;
} int main()
{
int n, m, a, b, c, i;
while(scanf("%d%d", &n, &m), n||m){
memset(pre, -1, sizeof(pre));
memset(Ring, 0, sizeof(Ring));
for(i = 0; i < m; ++i)
scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].cost);
sort(E, E + m, cmp);
printf("%d\n", greedy(n, m));
}
return 0;
}

HDU3367 Pseudoforest 【并查集】+【贪心】的更多相关文章

  1. HDU 1598 find the most comfortable road 并查集+贪心

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...

  2. [POJ2054]Color a Tree (并查集+贪心)

    POJ终于修好啦 题意 和UVA1205是同一题,在洛谷上是紫题 有一棵树,需要给其所有节点染色,每个点染色所需的时间是一样的都是11.给每个点染色,还有一个开销“当前时间×ci×ci”,cici是每 ...

  3. POJ 1456 Supermarket 区间问题并查集||贪心

    F - Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  4. 利用并查集+贪心解决 Hdu1232

    畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  5. POJ_1456 Supermarket 【并查集/贪心】

    一.题面 POJ1456 二.分析 1.贪心策略:先保证从利润最大的开始判断,然后开一个标记时间是否能访问的数组,时间尽量从最大的时间开始选择,这样能够保证后面时间小的还能够卖. 2.并查集:并查集直 ...

  6. POJ1456:Supermarket(并查集+贪心)

    Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17634   Accepted: 7920 题目链接 ...

  7. HDU 2480 Steal the Treasure (并查集+贪心)

    题意:给你n个点,m条边,包括有向边与无向边,每条边都有一个权值.在每个点上都有一个人,他可以走与这个点直接相连的所有边中任意一条边一次,并且得到这个权值,就不能走了,注意这条路也只能被一个人走.问最 ...

  8. UVA 1664 Conquer a New Region (并查集+贪心)

    并查集的一道比较考想法的题 题意:给你n个点,接着给你n-1条边形成一颗生成树,每条边都有一个权值.求的是以一个点作为特殊点,并求出从此点出发到其他每个点的条件边权的总和最大,条件边权就是:起点到终点 ...

  9. 【luoguUVA1316】 Supermarket--普通并查集+贪心

    题目描述 有一个商店有许多批货,每一批货又有N(0<=N<=10^4 )个商品,同时每一样商品都有收益P_iPi​ ,和过期时间D_iDi​ (1<=Pi,,Di <=10^4 ...

  10. 7.28 NOI模拟赛 H2O 笛卡尔树 并查集 贪心 长链剖分

    LINK:H2O 这场比赛打的稀烂 爆蛋. 只会暴力.感觉暴力细节比较多不想写. 其实这道题的难点就在于 采取什么样的策略放海绵猫. 知道了这一点才能确定每次放完海绵猫后的答案. 暴力枚举是不行的.而 ...

随机推荐

  1. convertquota - 把老的配额文件转换为新的格式

    总览 (SYNOPSIS) convertquota [ -ug ] filesystem 描述 (DESCRIPTION) convertquota 把老的配额文件 quota.user 和 quo ...

  2. PowerDesigner连接MySQL数据库

    详细步骤请点击下面的链接查看! 我在网上找了很多篇教程, 其中这一篇是最好的. 可以成功的帮助我们把PowerDesigner和MySQL数据库相连. PowerDesigner真的非常强大! 设计数 ...

  3. docker centos7 配置和宿主机同网段IP

    docker centos7 配置和宿主机同网段IP 1.安装brctl 命令 # yum -y install bridge-utils 2.编辑网卡配置文件 # vi ifcfg-eno16777 ...

  4. [Python3网络爬虫开发实战] 4.1-使用XPath

    XPath,全称XML Path Language,即XML路径语言,它是一门在XML文档中查找信息的语言.它最初是用来搜寻XML文档的,但是它同样适用于HTML文档的搜索. 所以在做爬虫时,我们完全 ...

  5. buf.swap32()

    buf.swap32() 返回:{Buffer} 将 Buffer 解释执行为一个32位的无符号整数数组并以字节顺序交换到位.如果 Buffer 的长度不是32位的倍数,则抛出一个 RangeErro ...

  6. [Android] java代码无错误,但跳转失败

    今天在调代码的时候,出现了这样的问题,我晕了半天,才找到解决办法. 查看日志发现:Initialize Binary Program Cache: Load Failed 从来没见过这种问题,Java ...

  7. 读书笔记:《人有人的用处》------N.维纳. (2016.12.28)

    读书笔记:<人有人的用处>------N.维纳 ·某些系统可以依其总能量而和其他系统区别开来. ·在某些情况下,一个系统如果保持足够长时间的运转,那它就会遍历一切与其能量相容的位置和动量的 ...

  8. bzoj1455左偏树裸题

    #include <stdio.h> bool vi[1000010]; int n,de[1000010],ls[1000010],rs[1000010],va[1000010],fa[ ...

  9. BNUOJ 26229 Red/Blue Spanning Tree

    Red/Blue Spanning Tree Time Limit: 2000ms Memory Limit: 131072KB This problem will be judged on HDU. ...

  10. [luoguP1033] 自由落体(模拟?)

    传送门 这不能算是数论题... 卡精度这事noip也做的出来.. 代码 #include <cmath> #include <cstdio> int n, ans; doubl ...