HDU 3367 Pseudoforest(Kruskal)
Pseudoforest
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2382 Accepted Submission(s): 933
larger than another if and only if the total value of the edges is greater than another one’s.
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 求出一个最大的子图(子图的每个连通分量最多有一个环) 用kruskal算法求出最大生成树 不过要判断是否有环 2树合并时 :若2个子树都有环不能合并 只有一个有环可以合并 但合并后的树有环 若2个子树都没环直接合并#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <stdio.h> using namespace std;
#define MAX 10000
int n,m;
struct Node
{
int x;
int y;
int w;
}a[MAX*10+5];
int father[MAX+5];
bool tag[MAX+5];
int cmp(Node a,Node b)
{
return a.w>b.w;
}
int find(int x)
{
if(x!=father[x])
father[x]=find(father[x]);
return father[x];
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0)
break;
for(int i=0;i<m;i++)
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);
for(int i=0;i<n;i++)
{
father[i]=i;
tag[i]=false;
}
sort(a,a+m,cmp);
int ans=0;
for(int i=0;i<m;i++)
{
int xx=find(a[i].x);
int yy=find(a[i].y);
if(xx!=yy)
{
if(tag[xx]&&tag[yy])
continue;
ans+=a[i].w;
father[xx]=yy;
if(tag[xx]||tag[yy])
{tag[xx]=true;tag[yy]=true;}
}
else
{
if(tag[xx]||tag[yy])
continue;
ans+=a[i].w;
tag[xx]=true;tag[yy]=true;
}
}
printf("%d\n",ans);
}
return 0;
}
HDU 3367 Pseudoforest(Kruskal)的更多相关文章
- 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)Tot ...
- 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的结构,表示在无向图上,每一个块中选取至多包含一个环的边的集合,又称“伪森林”.问这个集合中的所有边权之和最大是多少? 分析:如果没有环,那么构造的就是最大生成 ...
- 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 3367(与最大生成树无关。无关。无关。重要的事情说三遍+kruskal变形)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
随机推荐
- 机器学习之数据预处理,Pandas读取excel数据
Python读写excel的工具库很多,比如最耳熟能详的xlrd.xlwt,xlutils,openpyxl等.其中xlrd和xlwt库通常配合使用,一个用于读,一个用于写excel.xlutils结 ...
- app中h5交互的一些坑 记录笔记
1.ios开发镶嵌 h5页面 存在input 圆角问题(安卓直角) 解决办法 inpput{ -webkit-appearance: none; border-radius: 0px; } 2.ios ...
- python oop面向对象笔记
#coding:utf-8 class Person(object): def __init__(self,name,wage): self.name = name self.wage = wage ...
- Application Request Route实现IIS Server Farms集群负载详解(转)
http://www.cnblogs.com/knowledgesea/p/5099893.html http://www.cnblogs.com/smileberry/p/4300849.html
- 点滴积累【SQL Server】---使用Kettle实时同步DB2数据到SQLserver
效果: 描述: 此操作适用于单点登录的同步用户. 首先,使用kettle将DB2数据同步到SQL中,然后添加到windows的任务计划中.定时执行同步数据. 特殊说明:此工具涉及到公司版权,所以不方便 ...
- Android Training - 使用IntentService运行任务(Lesson 2 - 发送任务给IntentService)
写在http://hukai.me/blog/android-training-18-running-background-service-lesson-2/
- 分布式系统的CAP和BASE理论
1. 背景 网络分区:俗称“脑裂”.当网络发生异常情况,导致分布式系统中部分节点之间的网络延时不断变大,最终导致组成分布式系统的所有节点中,只有部分节点之间能够进行正常通信,而另一些节点则不能. 当网 ...
- 解决错误: ios property has a previous declaration
今天维护一个项目的时候,我新添加了一个库,运行的时候报错了: ios property has a previous declaration 上网查了一下没有找到思路,不过根据提示自己试了一下,原来这 ...
- regcomp/regexec/regfree--POSIX regex functions
语法 #include <sys/types.h> #include <regex.h> int regcomp(regex_t *preg, const char *rege ...
- nodejs对文件进行分页
//从文件中提取文件指从x行到y行的内容 //awk -v start=5 -v end=10 -F "\x01" '{if(NR>=start && NR& ...