HDU3367 Pseudoforest 【并查集】+【贪心】
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.
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.
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
3
5
题意:这题的题意折腾了好久才懂,開始以为是求最多有一个环的最大联通分量,然后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 【并查集】+【贪心】的更多相关文章
- HDU 1598 find the most comfortable road 并查集+贪心
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...
- [POJ2054]Color a Tree (并查集+贪心)
POJ终于修好啦 题意 和UVA1205是同一题,在洛谷上是紫题 有一棵树,需要给其所有节点染色,每个点染色所需的时间是一样的都是11.给每个点染色,还有一个开销“当前时间×ci×ci”,cici是每 ...
- POJ 1456 Supermarket 区间问题并查集||贪心
F - Supermarket Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- 利用并查集+贪心解决 Hdu1232
畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- POJ_1456 Supermarket 【并查集/贪心】
一.题面 POJ1456 二.分析 1.贪心策略:先保证从利润最大的开始判断,然后开一个标记时间是否能访问的数组,时间尽量从最大的时间开始选择,这样能够保证后面时间小的还能够卖. 2.并查集:并查集直 ...
- POJ1456:Supermarket(并查集+贪心)
Supermarket Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17634 Accepted: 7920 题目链接 ...
- HDU 2480 Steal the Treasure (并查集+贪心)
题意:给你n个点,m条边,包括有向边与无向边,每条边都有一个权值.在每个点上都有一个人,他可以走与这个点直接相连的所有边中任意一条边一次,并且得到这个权值,就不能走了,注意这条路也只能被一个人走.问最 ...
- UVA 1664 Conquer a New Region (并查集+贪心)
并查集的一道比较考想法的题 题意:给你n个点,接着给你n-1条边形成一颗生成树,每条边都有一个权值.求的是以一个点作为特殊点,并求出从此点出发到其他每个点的条件边权的总和最大,条件边权就是:起点到终点 ...
- 【luoguUVA1316】 Supermarket--普通并查集+贪心
题目描述 有一个商店有许多批货,每一批货又有N(0<=N<=10^4 )个商品,同时每一样商品都有收益P_iPi ,和过期时间D_iDi (1<=Pi,,Di <=10^4 ...
- 7.28 NOI模拟赛 H2O 笛卡尔树 并查集 贪心 长链剖分
LINK:H2O 这场比赛打的稀烂 爆蛋. 只会暴力.感觉暴力细节比较多不想写. 其实这道题的难点就在于 采取什么样的策略放海绵猫. 知道了这一点才能确定每次放完海绵猫后的答案. 暴力枚举是不行的.而 ...
随机推荐
- Java 斜杠 与 反斜杠
除号 /(数字键盘的斜杠)网址 /(数字键盘的斜杠)文件地址 \转义 \正则表达式 \
- g20学习笔记
BALProblem.h---------定义BALProblem类. BALProblem类保存我们的BA所需要的所有数据,包括相机与路标之间的联系,相机变量+路标变量的初始值.这些数据的原始信息都 ...
- Danfoss Motor - Automotive Motor: What Sensors Are There?
The Danfoss Motor states that the motor sensor control system is the heart of the entire autom ...
- ajax 分页点击数据缓存
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- pycharm激活2018
因为我的是Windows,所以这篇文章只针对Windows系统. 1.将“0.0.0.0 account.jetbrains.com”中的内容添加到hosts文件中,hosts路径为:C:\Windo ...
- Dash Speed
题目大意: 比特山是比特镇的飙车圣地.在比特山上一共有n 个广场,编号依次为1 到n,这些广场之间通过n - 1 条双向车道直接或间接地连接在一起,形成了一棵树的结构.因为每条车道的修建时间以及建筑材 ...
- (八)python3 迭代
迭代:如果给定一个 list 或 tuple,我们可以通过 for 循环来遍历这个 list 或tuple,这种遍历我们称为迭代(Iteration) 字典: >>> d = {'a ...
- Jquery EasyUI动态生成Tab
function addTab(title, url) { if ($('#tt').tabs('exists', title)) { $('#tt').tabs('select', title); ...
- 【ZJOI2017 Round1练习&BZOJ5354】D7T3 room(DP)
题意: 思路: 写了两种版本 考场版本 ..,..]of longint; t:..,..]of longint; n,m,i,j,k,oo,ans,d1:longint; function min( ...
- Redis 配置【十】
参考:http://www.runoob.com/redis/redis-conf.html Redis 的配置文件位于 Redis 安装目录下,文件名为 redis.conf. 你可以通过 CONF ...