hdoj--3367--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
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
int n,m,minn,vis[10000+10],pre[10000+10];
struct node
{
int u,v,val;
}edge[100000+10];
void init()
{
for(int i=0;i<10010;i++)
pre[i]=i;
}
int cmp(node s1,node s2)
{
return s1.val>s2.val;
}
int find(int x)
{
return pre[x]==x?x:pre[x]=find(pre[x]);
}
int main()
{
while(scanf("%d%d",&n,&m),n||m)
{
init();
for(int i=0;i<m;i++)
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].val);
sort(edge,edge+m,cmp);
minn=0;
memset(vis,0,sizeof(vis));
for(int i=0;i<m;i++)
{
int fx=find(edge[i].u);
int fy=find(edge[i].v);
if(fx!=fy)
{
if(!vis[fx]&&!vis[fy])
{
minn+=edge[i].val;
pre[fx]=fy;
}
else if(!vis[fx]||!vis[fy])
{
minn+=edge[i].val;
pre[fx]=fy;
vis[fx]=vis[fy]=1;
}
}
else
{
if(!vis[fx]&&!vis[fy])
{
minn+=edge[i].val;
vis[fx]=vis[fy]=1;
}
}
}
printf("%d\n",minn);
}
return 0;
}
hdoj--3367--Pseudoforest(伪森林&&最大生成树)的更多相关文章
- HDU 3367 (伪森林,克鲁斯卡尔)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3367 Pseudoforest Time Limit: 10000/5000 MS (Java/Oth ...
- 【hdu3367】Pseudoforest(伪森林)
http://acm.hdu.edu.cn/showproblem.php?pid=3367 题目大意 伪森林就是一个无向图,这个无向图有多个连通块且每个连通块只有一个简单环. 给你一个无向图,让你找 ...
- hdu 3367 Pseudoforest (最大生成树 最多存在一个环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3367 Pseudoforest Time Limit: 10000/5000 MS (Java/Oth ...
- hdu3367最大伪森林(并查集)
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/3367/ 题目要求一个连通图的最大伪森林,伪森林是一个最多有一个回路的图.我们只要用Kruskal最大生成树的策略 ...
- hdu 3367 Pseudoforest (最小生成树)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- hdu 3367 Pseudoforest(并查集)
题意:有一种叫作Pseudoforest的结构,表示在无向图上,每一个块中选取至多包含一个环的边的集合,又称“伪森林”.问这个集合中的所有边权之和最大是多少? 分析:如果没有环,那么构造的就是最大生成 ...
- Pseudoforest(伪最大生成树)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- hdu 3367 Pseudoforest(最大生成树)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- 【HDOJ】3367 Pseudoforest
并查集. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 10005 #d ...
随机推荐
- [Offer收割]编程练习赛32
气泡图 两两判断关系,dfs. #include<stdio.h> #include<string.h> #include<stdlib.h> #include&l ...
- Django学习案例一(blog):一. 创建project、app
1.创建project 方法1:使用命令行创建项目.在E盘cmd执行如下命令: django-admin.py startproject myblog 方法2:使用pycharm创建项目.放置位置为D ...
- Android Studio复制项目 两个App之间不覆盖安装操作步骤
步骤一:修改包名 第五步注意:不能以数字等作为包名的开头. 步骤二:修改清单文件里面的包名 第八步注意:如果报红,从新引入新的包名下的Mainactivity类. 步骤三:修改Gradle Scrip ...
- 【Oracle】权限
1. 授予权限: GRANT privilege[, privilege...] TO user [, user| role, PUBLIC...]; ①DBA授予用户系统权限 GRANT creat ...
- WebService 服务接口
天气预报Web服务,数据来源于中国气象局Endpoint :http://www.webxml.com.cn/WebServices/WeatherWebService.asmxDisco :http ...
- Nginx下修改php.ini后重新加载配置文件命令
修改php.ini后 如,我的 php.ini 文件是放在 /etc/php.ini php 所在目录是 /www/Linux/php-5.2.17 修改 php.ini 后要用 php-fpm 来进 ...
- 杭电 1012 u Calculate e【算阶乘】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1012 解题思路:对阶乘递归求和 反思:前面3个的输出格式需要注意,可以自己单独打印出来,也可以在for ...
- 后代children() find()的区别
$(document).ready(function(){ $("div").children(); });只拿到div下面的子标签元素 $(document).ready(fun ...
- css常用代码大全以及css兼容(转载)
css常见的快捷开发代码汇总(长期更新),包括CSS3代码,有一些css效果很顽固,经常会一时找不出解决方案,网络上也有很多的工具和高手提供了具体的浏览器兼容代码,这个页面我今后会经常整理,希望能够帮 ...
- 动态规划——Buyer
题目链接 题目描述 哆啦A梦班级举办个party,当然吃的东西必不可少,哆啦A梦负责采购任务,他得到了一份清单,上面注明不同食品的受欢迎程度,哆啦A梦需要用一定的价钱尽可能达到的更大的受欢迎程度!例如 ...