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. WPF动画 - Loading加载动画

    存在问题: 最近接手公司一个比较成熟的产品项目开发(WPF桌面端),其中,在登陆系统加载时,60张图片切换,实现loading闪烁加载,快有密集恐惧症了!!! 代码如下: private void L ...

  2. Laravel Excel安装及最简单使用

    官网:https://docs.laravel-excel.com/ 1.安装 1.1.安装要求: ​ PHP: ^7.0 ​ Laravel: ^5.5 ​ PhpSpreadsheet: ^1.6 ...

  3. velocity(vm)模板引擎基本语法

    for循环 #foreach($acc in $!{param.tools}) #set($count = $count + 1) <li custom-data="$!{acc.or ...

  4. 简谈Redis

    1.为什么使用redis 分析:博主觉得在项目中使用redis,主要是从两个角度去考虑:性能和并发.当然,redis还具备可以做分布式锁等其他功能,但是如果只是为了分布式锁这些其他功能,完全还有其他中 ...

  5. mysql cluster配置

    依赖包要求:cmake     gcc    gcc-c++     ncurses     Perl     ncurses-devel 在7.3以及更高的版本中, WITH_NDB_JAVA是默认 ...

  6. linux arp-显示和修改IP到MAC转换表

    博主推荐:更多网络测试相关命令关注 网络测试  收藏linux命令大全 arp命令用于操作主机的arp缓冲区,它可以显示arp缓冲区中的所有条目.删除指定的条目或者添加静态的ip地址与MAC地址对应关 ...

  7. Python Pandas库的学习(二)

    今天我们继续讲下Python中一款数据分析很好的库.Pandas的学习 接着上回讲到的,如果有人听不懂,麻烦去翻阅一下我前面讲到的Pandas学习(一) 如果我们在数据中,想去3,4,5这几行数据,那 ...

  8. Django——分页功能Paginator

    Django分页功能----Paginator Paginator所需参数: Paginator(object_list,per_page) Paginator常用属性: per_page: 每页显示 ...

  9. UVALive 6507 Passwords

    Passwords Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ...

  10. HASH的应用(负数下标用偏移量解决)

    Input 每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个处于区间[-500000,500000]的整数. Output 对每组测试数据按从大到小的 ...