hdu 3367 Pseudoforest (最小生成树)
Pseudoforest
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1526 Accepted Submission(s): 580
The last test case is followed by a line containing two zeros, which means the end of the input.
伪森林:
题意为有一个伪森林,即森林里的每棵树最多有一个环。
这题还算是不错的,最小生成树小变形,要用kruskal算法,边的排序要按值大到小排。
排完序后就可以按kruskal的做法做了,不过要多作一个circle[]数组来记录每棵树环的情况,如果该树有一个环了就不能再加环,并且如果一条边有两个点一个属于有环树,一个不属于有环树,那么并查集并的时候要讲第二个点并到第一个点中,这样可以避免多个环的情况。思路清晰后还是挺简单的。
//546MS 1464K 1096 B G++
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 10005
struct node{
int u,v,d;
}p[*N];
int set[N];
int circle[N];
int n,m;
int cmp(const void*a,const void*b)
{
return (*(node*)b).d-(*(node*)a).d;
}
int find(int x)
{
if(set[x]!=x) set[x]=find(set[x]);
return set[x];
}
int kruskal()
{
int ans=;
for(int i=;i<m;i++){
int tu=find(p[i].u);
int tv=find(p[i].v);
if(tu==tv){
if(!circle[tu]){
circle[tu]=;
ans+=p[i].d;
}
continue;
}
if(circle[tu] && circle[tv]) continue;
if(circle[tu]) set[tv]=tu;
else set[tu]=tv;
ans+=p[i].d;
}
return ans;
}
int main(void)
{
while(scanf("%d%d",&n,&m)!=EOF&&(n+m))
{
for(int i=;i<=n;i++) set[i]=i;
memset(circle,,sizeof(circle));
for(int i=;i<m;i++){
scanf("%d%d%d",&p[i].u,&p[i].v,&p[i].d);
}
qsort(p,m,sizeof(p[]),cmp);
printf("%d\n",kruskal());
}
}
hdu 3367 Pseudoforest (最小生成树)的更多相关文章
- 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(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> ...
- hdu 3367(Pseudoforest ) (最大生成树)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- HDU 3367 (伪森林,克鲁斯卡尔)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3367 Pseudoforest Time Limit: 10000/5000 MS (Java/Oth ...
- hdu Constructing Roads (最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 /************************************************* ...
随机推荐
- DevExpress TreeList用法总结
http://blog.itpub.net/29251214/viewspace-774395/ http://blog.csdn.net/czp_huster/article/details/501 ...
- BZOJ2659_算不出的算式_KEY
题目传送门 其实打表找一找规律可以得出: /************************************************************** Problem: 2659 U ...
- 【LG1975】[国家集训队]排队
[LG1975][国家集训队]排队 题面 洛谷 题解 又是一个偏序问题 显然\(CDQ\) 交换操作不好弄怎么办? 可以看成两次删除两次插入 排序问题要注意一下 代码 #include <ios ...
- 使用element-ui 的table 渲染数据遇到的问题
通常我们使用一个table 来渲染服务的返回来的数据时,数据结构一般都是按row 来返回的,并且表头也是固定的 但是如果接口返回的数据结构不是我们想要的,表头也不确定时,我们该如何解析数据,将数据进行 ...
- 一个例子说明substr(), mb_substr() 和 mb_strcut()之间的区别
例子来自PHP官方文档,我只是翻译下. http://www.php.net/manual/zh/function.mb-strcut.php header( 'Content-Type:text/h ...
- spl_autoload_register()函数
一.__autoload 这是一个自动加载函数,在PHP5中,当我们实例化一个未定义的类时,就会触发此函数.看下面例子: printit.class.php <?php class PRINTI ...
- Appium-原理、设置
Appium是支持跨平台的移动端自动化测试框架. 下面介绍下Appium的具体工作流程: 首先,Appium server 默认监听4723端口,监听客户端的命令. 客户端指的是 我们编写的自动化测试 ...
- JMeter随机上传附件
方法一: 1.添加一个前置Beanshell 2.输入代码: File folder = new File("/path/to/your/folder/with/audiofiles&quo ...
- Python 发邮件例子
Python 发邮件例子 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019-04-23 16:12:33 # @Autho ...
- jmeter关联三种常用方法
在LR中有自动关联跟手动关联,但在我看来手动关联更准确,在jmeter中,就只有手动关联 为什么要进行关联:对系统进行操作时,本次操作或下一次操作对服务器提交的请求,这参数里边有部分参数需要服务器返回 ...