hdu 3367(与最大生成树无关。无关。无关。重要的事情说三遍+kruskal变形)
Pseudoforest
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2351 Accepted Submission(s): 910
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 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.
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
5
,如果都无环,正常合并,如果都有环了,那就不合并了
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std; const int N = ;
const int M =;
int father[N];
int vis[N]; ///判断此树是否有环
int _find(int x){
if(x==father[x]) return x;
return father[x]=_find(father[x]);
}
struct Edge{
int u,v,c;
}edge[M];
int cmp(Edge a,Edge b){
return b.c < a.c;
} int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF,(n+m)){
memset(vis,,sizeof(vis));
for(int i=;i<n;i++){
father[i] = i;
}
for(int i=;i<m;i++){
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].c);
}
sort(edge,edge+m,cmp);///从大到小排序
long long ans = ;
for(int i=;i<m;i++){
int x = _find(edge[i].u);
int y = _find(edge[i].v);
if(x==y){ ///已经是同一棵树了,判断是否该树是否存在环,不存在则可以连接两点形成环
if(!vis[x]){
vis[x]=vis[y]=;
ans+=edge[i].c;
}
}else{ ///不是同一棵树
if(vis[x]&&vis[y]) continue; ///都存在环了不可能合并了
if(vis[x]||vis[y]) vis[x]=vis[y]=; ///只要两棵子树中有一棵有环,则新生成的树也是有环的
father[x]=y;
ans+=edge[i].c;
}
}
printf("%lld\n",ans);
}
return ;
}
hdu 3367(与最大生成树无关。无关。无关。重要的事情说三遍+kruskal变形)的更多相关文章
- hdu 3367 Pseudoforest(最大生成树)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- hdu 3367 Pseudoforest 最大生成树★
#include <cstdio> #include <cstring> #include <vector> #include <algorithm> ...
- 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 ...
- 【与软件无关】2013赤峰地区C1科目三考试攻略【绝对原创】
期待很久的科目三,终于在开考了.传说中的全部电子评判,让习惯给考官送礼的赤峰人民无所是从.据说前几天曾经有一个驾校,考了一整天,八十多个人一个没过的. 我这个攻略是今天通过考试后的一点心得,希望能有用 ...
- HDU 3367 (伪森林,克鲁斯卡尔)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3367 Pseudoforest Time Limit: 10000/5000 MS (Java/Oth ...
- HDU 4786 Fibonacci Tree(生成树,YY乱搞)
http://acm.hdu.edu.cn/showproblem.php? pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others ...
- hdu 3367 Pseudoforest
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- HDU 3367 Pseudoforest(Kruskal)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
随机推荐
- 直通BAT面试算法精讲课2
对于一个int数组,请编写一个冒泡排序算法,对数组元素排序. 给定一个int数组A及数组的大小n,请返回排序后的数组. 测试样例: [1,2,3,5,2,3],6 [1,2,2,3,3,5] clas ...
- 关于Mybatis的@Param注解 及 mybatis Mapper中各种传递参数的方法
原文:https://blog.csdn.net/mrqiang9001/article/details/79520436 关于Mybatis的@Param注解 Mybatis 作为一个轻量级的数 ...
- simpleDateFormat的 学习
http://blog.csdn.net/qq_27093465/article/details/53034427
- C++派生类继承的理解
#include<iostream> using namespace std; class Base{ public: Base() { a=; cout<<"Bas ...
- DFS搜索题素数环
素数环: 输入整数1,2,3,4,5,···,n组成一个环,使得相邻两个整数之和均为素数. 输出时从整数1开始逆时针排列.同一个环应恰好输出一次.n<=16. Sample: input: 6 ...
- HDU3046 最大流(最小割)
Pleasant sheep and big big wolf Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- mysql连接时权限问题 用户问题
启动工程时会连接mysql数据库,此时报错: ERROR 1044 (42000): Access denied for user 'pay'@'localhost' to database 'pay ...
- 洛谷 P2800 又上锁妖塔
https://www.luogu.org/problem/show?pid=2800 题目背景 小D在X星买完了想要的东西,在飞往下一个目的地的途中,正无聊的他转头看了看身边的小A,发现小A正在玩& ...
- flask-login源码梳理
- mysql 数据库备份与还原,用户的创建与删除,用户的密码修改
1.备份数据库 要退出mysql rimideiMac-23:~ rimi$ mysqldump -u root -p pro >pro.sql ls 查看路径 2.恢复数据库 2.1直接 ...