ACM:Pseudoforest-并查集-最大生成树-解题报
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-并查集-最大生成树-解题报的更多相关文章
- ACM数据结构-并查集
ACM数据结构-并查集 并查集,在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将属于同一组的元素所在的集合合并,其间要反复查找一个元素在哪个集合 ...
- ACM : Travel-并查集-最小生成树 + 离线-解题报告
Travel Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u /*题意 给出n[节点 ...
- ACM: The Suspects-并查集-解题报告
The Suspects Time Limit:1000MS Memory Limit:20000KB 64bit IO Format:%lld & %llu Description 严重急性 ...
- Codevs 3287 货车运输 2013年NOIP全国联赛提高组(带权LCA+并查集+最大生成树)
3287 货车运输 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description A 国有 n 座 ...
- acm专题--并查集
题目来源:http://hihocoder.com/problemset/problem/1066 #1066 : 无间道之并查集 时间限制:20000ms 单点时限:1000ms 内存限制:256M ...
- ACM: Ubiquitous Religions-并查集-解题报告
Ubiquitous Religions Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Descript ...
- 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 ...
- 小希的迷宫(HDU 1272 并查集判断生成树)
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- 洛谷P2661 信息传递 [NOIP2015] 并查集/乱搞 (待补充!
感觉我好水啊,,,做个noip往年题目还天天只想做最简单的,,,实在太菜辽 然后最水的题目还不会正解整天想着乱搞,,, 虽然也搞出来辽233333 好滴不扯辽赶紧写完去做紫题QAQ 正解:并查集 ...
随机推荐
- md5sum
[root@NB index]# ls index()().html index()().html index()().html index()().html index()().html [root ...
- Android bluetooth用户自定义数据
default mac: [btif_core.c] btif_fetch_local_bdaddr() default device name: [btif_dm.c] btif_get_defau ...
- 获取Windows下某进程监听的TCP/UDP端口
1.在Windows下用CMD netstat命令可以获得当前进程监听端口号的信息,如netstat -ano可以看到IP.port.状态和监听的PID. 那么可以执行CMD这个进程得到监听的端口号信 ...
- DevExpress DXperience 的本地化(汉化)方法
Devexpress的.net组件目前非常流行,在国内开发者中有非常高的热度,但是由于是国外控件,我们经常遇到的一个问题是汉化.目前Devexpress公司2011.2版以后使用了统一的本地化模式,针 ...
- 【leetcode】Remove Duplicates from Sorted Array
题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...
- Iphone [Tab Bar实现多view切换,Picker,DataPicter实现
用Tab Bar Controller处理IPhone多个view切换, 而且还附有创建空项目,picker和DataPicker的实现! 具体步骤: 1.创建一个空项目,选择User Interfa ...
- Android UI学习 - Tab的学习和使用(转)
本文是参考Android官方提供的sample里面的ApiDemos的学习总结. TabActivity 首先Android里面有个名为TabActivity来给我们方便使用.其中有以下可 ...
- Android studio导入eclipse项目且不改变目录结构
Android studio的安装与配置论坛当中已经有很多在此就不在细说了,现在开始说下如何在Android studio当中导入eclipse的项目且不改变其目录结构和配置,让使用eclipse的同 ...
- 62个Android Studio小技巧合集
1书签(Bookmarks) 描述:这是一个很有用的功能,让你可以在某处做个标记(书签),方便后面再跳转到此处. 调用:Menu → Navigate → Bookmarks 快捷键: 添加/移除书签 ...
- hbase伪分布式安装(转)
原文地址:http://blog.csdn.net/yonghutwo/article/details/24555103 本机环境: ubuntu 12.4 Hadoop 1.1.2 安装hbase版 ...