HDU - Pseudoforest
Description
Input
The last test case is followed by a line containing two zeros, which means the end of the input.
Output
Sample Input
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
5
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std; const int MX = 111111;
int road[MX];
int sign[MX];
int rec[MX];
int n, m; struct Node {
int a, b, c;
}node[MX]; bool comp(const Node& n1, const Node& n2) {
return n1.c > n2.c;//对价值进行排序,优先考虑放大的,贪心啦
} void ini() {
for (int i = 0; i < n; i++) {
road[i] = i;
sign[i] = 0;//注意这个是用来标记环的数量的
rec[i] = 1;//注意这个是用来标记集合中的元素个数的,因为我采用了新的合并方法,就是把小集合合并到大集合,当然你也不用在意这种细节啦,你可以继续使用自己的合并方式
}
} int FindRoot(int r) {//在使用路径压缩查找跟节点的时候我没有使用递归了,主要是不好进行各种标记
int root = r;
while (road[root] != root) root = road[root]; int t1 = r;
int t2 = r;
while (road[t1] != root) {
t2 = road[t1];
road[t1] = root;
sign[t1] = sign[root];
t1 = t2;
}
return root;
} int UnionRoot(int root1, int root2) {//基本的合并,一看就懂啦,看不懂就继续看- -
if (rec[root1] >= rec[root2]) {
road[root2] = root1;
rec[root1]++;
return root1;
} else {
road[root1] = root2;
rec[root2]++;
return root2;
}
} int main()
{
//freopen("input.txt", "r", stdin);
while (scanf("%d%d", &n, &m), n || m) {
ini();
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &node[i].a, &node[i].b, &node[i].c);
}
sort(node, node + m, comp);//基本的贪心思想
int ans = 0;
for (int i = 0; i < m; i++) {
int root1 = FindRoot(node[i].a);
int root2 = FindRoot(node[i].b);
if (root1 == root2 && sign[root1] == 0) {//当两个节点同属于一个集合的时候,看看这个集合已经形成环了没有,如果形成了,就不能加入了,反之则可以
ans += node[i].c;
sign[root1] = 1;
} else {
if (sign[root1] != 1 || sign[root2] != 1) {//两个节点分别属于不同的集合,因为合并的时候要考虑环的数量,所以当两个集合合并后新集合的环数量超过1也是不行的
ans += node[i].c;
int r = UnionRoot(root1, root2);
if (sign[root1] == 1 || sign[root2] == 1) {
sign[r] = 1;
}
}
}
}
printf("%d\n", ans);
}
return 0;
}
HDU - Pseudoforest的更多相关文章
- hdu 3367(Pseudoforest ) (最大生成树)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- 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 Pseudoforest
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(Kruskal)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- hdu 3367 Pseudoforest(并查集)
题意:有一种叫作Pseudoforest的结构,表示在无向图上,每一个块中选取至多包含一个环的边的集合,又称“伪森林”.问这个集合中的所有边权之和最大是多少? 分析:如果没有环,那么构造的就是最大生成 ...
- hdu 3367 Pseudoforest 最大生成树★
#include <cstdio> #include <cstring> #include <vector> #include <algorithm> ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
随机推荐
- MVC - 11(下)jquery.tmpl.js +ajax分页
继续 mvc-11(上).dto:http://www.cnblogs.com/tangge/p/3840060.html jquery.tmpl.js 下载:http://pan.baidu.com ...
- Microsoft SQL Server 博客目录
基础概念篇 SQL Server排序规则 SQL SERVER 统计信息概述(Statistics) SQL SERVER 索引之聚集索引和非聚集索引的描述 Sql Server 索引之唯一索引和筛选 ...
- Creating a Table View Programmatically
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/Cre ...
- 1-03 Sql Sever 的身份验证模式
身份验证分为: 1:Windows身份验证. 1:Sql Sever身分验证. 每种验证的具体方式: 1Windows的验证方式 点击下拉框,有这两种验证方式,Windows验证只需要启动服务即可. ...
- FP-Growth算法及演示程序
FP-Growth算法 FP-Growth(频繁模式增长)算法是韩家炜老师在2000年提出的关联分析算法,它采取如下分治策略:将提供频繁项集的数据库压缩到一棵频繁模式树(FP-Tree),但仍保留项集 ...
- HDU 5876 Sparse Graph BFS 最短路
Sparse Graph Problem Description In graph theory, the complement of a graph G is a graph H on the ...
- Linux内核system_call中断处理过程
“平安的祝福 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ” men ...
- BNUOJ1067生成函数入门
https://www.bnuoj.com/v3/problem_show.php?pid=1067
- 移动端_line-height问题
如果把line-height加1px,iPhone文字就会下移,由于我们app的ios用户居多,并且android机型太多,不同机型也会显示不同,所以只能退而求其次了. line-height的兼容问 ...
- 【转】ADO.NET中的五个主要对象
Connection 物件 Connection 对象主要是开启程序和数据库之间的连结.没有利用连结对象将数据库打开,是无法从数据库中取得数据的.这个物件在ADO.NET 的最底层,我们可以自己 ...