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. -A 解决数据库表太多,预读表时间很长

    Reading table information for completion of table and column names You can turn off this feature to ...

  2. 大话数据结构–1.基础知识+2.算法

      2.算法: 算法是解决特定问题求解步骤的描述,在计算机中表现为指令的有限序列,并且每个指令表现为一个或多个操作. 特性:输入.输出.有穷性.确定性.可行性.   2.9.1.算法时间复杂度: 语句 ...

  3. jQuery – 8.事件和事件参数

        事件 (*)JQuery中的事件绑定:$("#btn").bind("click",function(){}),每次都这么调用太麻烦,所以jQuery可 ...

  4. 重温WCF之WCF抛出异常的处理SOAP Fault(十二)

    1.(服务端)抛出和(客户端)捕获SOAP Fault 当我们需要客户端获取到WCF服务端的抛出的异常的时候,使用FaultException类 WCF类库在System.ServiceModel命名 ...

  5. 小鼠迷宫问题【sdut1157】【dfs,bfs综合题目】

    小鼠迷宫问题 Time Limit: 1500ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 小鼠a与小鼠b身处一个m×n的迷宫中,如图所示.每一个方格表示迷宫中 ...

  6. Message Flood

    Message Flood Time Limit: 1500MS Memory limit: 65536K 题目描述 Well, how do you feel about mobile phone? ...

  7. Java中的wait和sleep

    sleep()和wait() 首先,Java中的多线程是一种抢占式的机制,而不是分时机制.抢占式的机制是有多个线程处于可运行状态,但是只有一个线程在运行. 这种机制决定了,对于同一对象的多线程访问,必 ...

  8. Http 请求处理流程

    引言 我查阅过不少Asp.Net的书籍,发现大多数作者都是站在一个比较高的层次上讲解Asp.Net.他们耐心.细致地告诉你如何一步步拖放控件.设置控件属性.编写CodeBehind代码,以实现某个特定 ...

  9. Android中log使用方法

    private static final String ACTIVITY_TAG="MainActivity"; Log.v(MainActivity.ACTIVITY_TAG, ...

  10. JDBC之——一个单线程JDBC基类和一些注意事项

    下面的一个DBase基类提供了一些jdbc的基本操作,配置文件的配置部分并不准确,只是在自己机器上的mysql中测试过.. 数据库的增删改方法中都在开始调用了getConnection方法,末尾调用了 ...