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. GMap.Net开发之自定义Marker

    上一篇文章介绍了如何在WinForm和WPF中使用GMap控件,这篇介绍下GMap中Marker的使用. 自定义Marker,可以理解为在地图上自定义图标(Custom Marker),先看看GMap ...

  2. HYSBZ 2440 完全平方数(莫比乌斯反演)

    链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2440 若i为质数,n为i*i的倍数,则称n为含平方因子数. 求1~n的无平方因子数. F(x) ...

  3. DevExpress DXperience 的本地化(汉化)方法

    Devexpress的.net组件目前非常流行,在国内开发者中有非常高的热度,但是由于是国外控件,我们经常遇到的一个问题是汉化.目前Devexpress公司2011.2版以后使用了统一的本地化模式,针 ...

  4. Windows+Git+TortoiseGit+COPSSH 安装图文教程

    转自:http://blog.csdn.net/aaron_luchen/article/details/10498181 准备工作: 1. Git-1.8.1.2-preview20130201.e ...

  5. LeetCode——Single Number(找出数组中只出现一次的数)

    问题: Given an array of integers, every element appears twice except for one. Find that single one. No ...

  6. 在Xcode中想要清屏该怎么实现

    XCODE的控制台不会有清屏效果,并没有像终端一样可以clear.但在某些时候我们非得想要清屏该怎么办呢??你去打开可执行文件,就会有类似清屏的效果.实际上是它帮你自动换页了,xcode左边是可以看到 ...

  7. r0遍历系统进程方法总结

    方法1: ZwQuerySystemInformation 这个方法网上一搜一大堆,不举例了 方法2:暴力枚举PID枚举进程,代码: NTSTATUS DriverEntry(PDRIVER_OBJE ...

  8. Linux学习笔记(21) Linux日志管理

    1. 简介 (1) 日志服务 在CentOS 6.x中日志服务已经由rsyslogd取代了原先的syslogd服务.rsyslogd日志服务更加先进,功能更多.但是不论该服务的使用,还是日志文件的格式 ...

  9. 【设计模式】MVC模式

    MVC 模式代表 Model-View-Controller(模型-视图-控制器) 模式.这种模式用于应用程序的分层开发. Model(模型) - 模型代表一个存取数据的对象或 JAVA POJO.它 ...

  10. 系统剖析Android中的内存泄漏

    [转发]作为Android开发人员,我们或多或少都听说过内存泄漏.那么何为内存泄漏,Android中的内存泄漏又是什么样子的呢,本文将简单概括的进行一些总结. 关于内存泄露的定义,我可以理解成这样 没 ...