Pseudoforest
Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status
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( < n <= ), m( <= m <= ), 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 ( < c <= ) 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. Output
Output the sum of the value of the edges of the maximum pesudoforest. Sample Input Sample Output /*伪森林*/
这个题目类似于最小生成树,只不过是找最大的权值,但是不能直接写最大生成树,有个地方要注意,就是每个联通块只能有一个环,要加一个环的判定,以环为根节点。
如果两个生成树都有环就不能再相连。
对于同一个根的两个点,如果这个根已经成环,这两点不能在连接,如果没成环,就连上,标记成环。
看懂题目后挺简单,可是我还是wa了三次。。
代码:
#include"iostream"
#include"cstdio"
#include"cstring"
#include"cmath"
#include"algorithm"
using namespace std;
const int MX=2e5+5;
struct nod {
int p; //根值
bool O; //是否联通成环
int sum;//总权值
} pe[MX]; struct node {
int s,e,q;//查询的两个点 s e 和 边的权值
} side[MX]; int find(int x) {
return pe[x].p==x?x:(pe[x].p=find(pe[x].p));
}
bool cmp(node a,node b) { //按照权值降序排序
return a.q>b.q;
} int main() {
int n,m,maxx,flag;
while(~scanf("%d%d",&n,&m)) {
if(!n&&!m)break; for(int i=0; i<=n; i++)pe[i].p=i,pe[i].O=0,pe[i].sum=0; //清空标记,清空环,清空总权值 for(int i=0; i<m; i++) {
scanf("%d%d%d",&side[i].s,&side[i].e,&side[i].q); //读入每一个点
}
sort(side,side+m,cmp);
maxx=0;
for(int i=0; i<m; i++) {
int rt1=find(side[i].s),rt2=find(side[i].e);
bool OK1=pe[rt1].O,OK2=pe[rt2].O;
if(rt1==rt2) {
if(OK1&&OK2)continue;//如果这个联通图内部已经构成环了,则不再连接这两点
pe[rt1].O=1;
pe[rt1].sum+=side[i].q;//找到两个根相同的节点,连起来就构成了最大环
}
if(rt1!=rt2) {
if(OK1&&OK2)continue; //如果两个联通图各自都已经有环了,则不再连接
if(!OK1&&OK2) {
pe[rt2].sum+=side[i].q+pe[rt1].sum; //以已经成环的根为根
pe[rt1].sum=0; //将未成环的根的总和清空
pe[rt1].p=rt2; //将未成环的根的根变为环
} else {
pe[rt1].sum+=side[i].q+pe[rt2].sum;
pe[rt2].sum=0;
pe[rt2].p=rt1;
}
}
}
for(int i=0; i<n; i++) {
maxx+=pe[i].sum;
}
printf("%d\n",maxx);
}
return 0;
}

  


ACM:Pseudoforest-并查集-最大生成树-解题报的更多相关文章

  1. ACM数据结构-并查集

    ACM数据结构-并查集   并查集,在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将属于同一组的元素所在的集合合并,其间要反复查找一个元素在哪个集合 ...

  2. ACM : Travel-并查集-最小生成树 + 离线-解题报告

    Travel Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u /*题意 给出n[节点 ...

  3. ACM: The Suspects-并查集-解题报告

    The Suspects Time Limit:1000MS Memory Limit:20000KB 64bit IO Format:%lld & %llu Description 严重急性 ...

  4. Codevs 3287 货车运输 2013年NOIP全国联赛提高组(带权LCA+并查集+最大生成树)

    3287 货车运输 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description A 国有 n 座 ...

  5. acm专题--并查集

    题目来源:http://hihocoder.com/problemset/problem/1066 #1066 : 无间道之并查集 时间限制:20000ms 单点时限:1000ms 内存限制:256M ...

  6. ACM: Ubiquitous Religions-并查集-解题报告

    Ubiquitous Religions Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Descript ...

  7. Codeforces 437D The Child and Zoo - 树分治 - 贪心 - 并查集 - 最大生成树

    Of course our child likes walking in a zoo. The zoo has n areas, that are numbered from 1 to n. The ...

  8. 小希的迷宫(HDU 1272 并查集判断生成树)

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  9. 洛谷P2661 信息传递 [NOIP2015] 并查集/乱搞 (待补充!

    感觉我好水啊,,,做个noip往年题目还天天只想做最简单的,,,实在太菜辽 然后最水的题目还不会正解整天想着乱搞,,,  虽然也搞出来辽233333 好滴不扯辽赶紧写完去做紫题QAQ 正解:并查集  ...

随机推荐

  1. Android中Service 使用详解(LocalService + RemoteService)

    Service 简介: Service分为本地服务(LocalService)和远程服务(RemoteService): 1.本地服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外L ...

  2. Delphi中exit、break、continue等跳出操作的区别

    Delphi中表示跳出的有break,continue,abort,exit,halt,runerror等 1.break 强制退出最近的一层循环(注意:只能放在循环里:而且是只能跳出最近的一层循环) ...

  3. Delphi集合的用法

    参考:http://www.cnblogs.com/doit8791/archive/2012/08/17/2644859.html 集合是Pascal特有的数据类型,在Visual Basic.C/ ...

  4. Pyqt 打包资源文件

    用打包工具将做好的Pyqt程序打包成exe后发现引用的资源图片都显示不了? 是否遇到了和我一样的问题呢.google之后找到了方法,一种方法是在程序中引用外部资源,另外一种方法是将资源文件转换为py文 ...

  5. 几年前做家教写的C教程(之一)

    C语言学习宝典 首先让我们认识什么是C语言. C语言是一种计算机开发语言,是一种非常基础的开发语言.能够用C语言做很多事情.C语言是顺序执行的程序. 程序应该包括数据描述,数据操作. C语言的数据类型 ...

  6. Python无类再理解--metaclass,type

    上次理解过一次,时间久了,就忘了.. 再学习一次.. http://blog.jobbole.com/21351/ ======================= 但是,Python中的类还远不止如此 ...

  7. cordova+angularJS+ionic

    1.创建项目 2.路由 angular.module("starter",['ionic']) // 依赖 ionic 提供的ui-router .config(function ...

  8. hdu 4070 福州赛区网络赛J 贪心 ***

    优先发路程最长的 #include<cstdio> #include<iostream> #include<algorithm> #include<cstri ...

  9. 几个国内速度最快的centos yum(更新源)

    转自:http://blog.itpub.net/15711267/viewspace-1068862/ 中国科技大学源 cd /etc/yum.repos.d mv CentOS-Base.repo ...

  10. Mysql日志操作

    Mysql日志 mysql的日志类型:错误日志: -log-err记录mysql服务的启动.运行.停止mysql服务时出现的问题查询日志: -log记录建立的客户端连接和执行的语句慢查询日志: -lo ...