HDU 3367 Pseudoforest 最小生成树
来源:互联网 发布:ios抓取网页数据 编辑:程序博客网 时间:2024/06/05 05:43
Pseudoforest
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1790 Accepted Submission(s): 687
Problem Description
In 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
The 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.
The last test case is followed by a line containing two zeros, which means the end of the input.
Output
Output the sum of the value of the edges of the maximum pesudoforest.
Sample Input
3 30 1 11 2 12 0 14 50 1 11 2 12 3 13 0 10 2 20 0
Sample Output
35
/*HODJ 3367 最小生成树K,判断有无环的出现 */#include<iostream>#include<stdio.h>#include<algorithm>using namespace std;struct node{ int x,y,dis;};node e[100001];int pre[10001],circle[10001];int cmp(node a,node b){ return a.dis>b.dis;}int find(int x){ if(x!=pre[x]) return pre[x]=find(pre[x]); return pre[x];}int main(){ int n,m,i,j,sum,x,y,a,b,v; // freopen("test.txt","r",stdin); while(scanf("%d%d",&n,&m)) { if(n==0&&m==0) break; for(i=0;i<n;i++)// 注意题目是从0开始的 pre[i]=i; memset(circle,0,sizeof(circle)); for(i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&v); e[i].x=a; e[i].y=b; e[i].dis=v; } sort(e,e+m,cmp); sum=0; for(i=0;i<m;i++) { x=find(e[i].x); y=find(e[i].y); if(x!=y) { if(!circle[x]&&!circle[y])//两个都不是环 { sum+=e[i].dis; pre[x]=y; } else if(!circle[x]||!circle[y])//其中有一个是环,合并后两个都是环 { pre[x]=y; sum+=e[i].dis; circle[x]=circle[y]=1; } } else// x==y表示两个是一个集合里的,有相同的根 //如果x还没构成环,现在又多条路径,构成环 //因为e[i].x,e[i].y是一条路径,现在又有相同的根x,构成环 { if(!circle[x])//!circle[y]一样,x==y { circle[x]=1; sum+=e[i].dis; } } } printf("%d\n",sum); } return 0;}
0 0
- HDU 3367 Pseudoforest 最小生成树
- HDU 3367 Pseudoforest 最小生成树、并查集
- HDU 3367 Pseudoforest (最小生成树,并查集)
- 3367Pseudoforest 伪森林 最小生成树
- hdu 3367 Pseudoforest(最大生成树)
- HDU 3367 Pseudoforest 最大生成树
- hdu 3367 Pseudoforest 最大生成树
- (step6.1.8)hdu 3367(Pseudoforest——最小生成树的逆应用)
- HDU 3367 Pseudoforest(最大生成树+并查集)
- HDU 3367 Pseudoforest(kruskal最大生成树变形)
- hdu 3367 Pseudoforest 变形最大生成树 解题报告
- hdu 3367 Pseudoforest
- HDU 3367 Pseudoforest
- hdu 3367 Pseudoforest
- hdu 3367 Pseudoforest
- hdu 3367 Pseudoforest
- HDU 3367 Pseudoforest
- HDU 3367 Pseudoforest(Kruskal)
- 项目引入Solr时应该考虑的一些问题
- Oracle WorkFlow 工作流 上篇
- UVa-10763-Foreign Exchange
- PHP下载CSS文件中的图片
- update值与原值相同时,SQL Server会真的去update还是忽略呢?
- HDU 3367 Pseudoforest 最小生成树
- qtp运行时,缺少对象Wscript的解决方法
- LINK : fatal error LNK1104: 无法打开文件“C:\Qt\4.8.0\lib\QtGuid4.lib”
- Oracle WorkFlow 工作流 中篇
- harbor服务,cloudfoundry的dea和warden实现UDP的端口映射
- 黑马程序员_iOS开发C语言基础基本语法易错点
- 数据挖掘的十种分析方法
- ios7 ios8 Notification
- [ACM] poj 1236 Network of Schools (有向强连通分量)