Pseudoforest

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2351    Accepted Submission(s): 910

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.
 
Output
Output the sum of the value of the edges of the maximum pesudoforest.
 
Sample Input
3 3
0 1 1
1 2 1
2 0 1
4 5
0 1 1
1 2 1
2 3 1
3 0 1
0 2 2
0 0
 
Sample Output
3
5
很好的题目啊...当初错了n次
这个题目说的不是找到含有一个环的最大生成树,而是每个连通分量最多有一个环啊...所以求出最大生成树再添边的做法是错的
这个题正确的做法是每次选择合并的时候判断是否成环
先对边从大到小排序,然后进行贪心选择,如果根节点相同,则判断当前的树中是否有环,没有的话则连接两点。
如果根结点不同,如果有一棵子树有环一棵无环,则合并,然后新树标记成有环
,如果都无环,正常合并,如果都有环了,那就不合并了
 
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std; const int N = ;
const int M =;
int father[N];
int vis[N]; ///判断此树是否有环
int _find(int x){
if(x==father[x]) return x;
return father[x]=_find(father[x]);
}
struct Edge{
int u,v,c;
}edge[M];
int cmp(Edge a,Edge b){
return b.c < a.c;
} int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF,(n+m)){
memset(vis,,sizeof(vis));
for(int i=;i<n;i++){
father[i] = i;
}
for(int i=;i<m;i++){
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].c);
}
sort(edge,edge+m,cmp);///从大到小排序
long long ans = ;
for(int i=;i<m;i++){
int x = _find(edge[i].u);
int y = _find(edge[i].v);
if(x==y){ ///已经是同一棵树了,判断是否该树是否存在环,不存在则可以连接两点形成环
if(!vis[x]){
vis[x]=vis[y]=;
ans+=edge[i].c;
}
}else{ ///不是同一棵树
if(vis[x]&&vis[y]) continue; ///都存在环了不可能合并了
if(vis[x]||vis[y]) vis[x]=vis[y]=; ///只要两棵子树中有一棵有环,则新生成的树也是有环的
father[x]=y;
ans+=edge[i].c;
}
}
printf("%lld\n",ans);
}
return ;
}

hdu 3367(与最大生成树无关。无关。无关。重要的事情说三遍+kruskal变形)的更多相关文章

  1. hdu 3367 Pseudoforest(最大生成树)

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  2. hdu 3367 Pseudoforest 最大生成树★

    #include <cstdio> #include <cstring> #include <vector> #include <algorithm> ...

  3. hdu 3367(Pseudoforest ) (最大生成树)

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  4. hdu 3367 Pseudoforest (最大生成树 最多存在一个环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3367 Pseudoforest Time Limit: 10000/5000 MS (Java/Oth ...

  5. 【与软件无关】2013赤峰地区C1科目三考试攻略【绝对原创】

    期待很久的科目三,终于在开考了.传说中的全部电子评判,让习惯给考官送礼的赤峰人民无所是从.据说前几天曾经有一个驾校,考了一整天,八十多个人一个没过的. 我这个攻略是今天通过考试后的一点心得,希望能有用 ...

  6. HDU 3367 (伪森林,克鲁斯卡尔)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3367 Pseudoforest Time Limit: 10000/5000 MS (Java/Oth ...

  7. HDU 4786 Fibonacci Tree(生成树,YY乱搞)

    http://acm.hdu.edu.cn/showproblem.php? pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others ...

  8. hdu 3367 Pseudoforest

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  9. HDU 3367 Pseudoforest(Kruskal)

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

随机推荐

  1. 【DP】【P2734】游戏 A Game

    传送门 Description 有如下一个双人游戏:N个正整数的序列放在一个游戏平台上,游戏由玩家1开始,两人轮流从序列的任意一端取一个数,取数后该数字被去掉并累加到本玩家的得分中,当数取尽时,游戏结 ...

  2. JavaScript定义类的方式与其它OO语言有些差异

    JavaScript面向对象的程序编写与其它OO语言有一些出入,所以使用JavaScript的面向对象特性的时候,需要注意一些规范性的问题.下面就简单地谈一下,JavaScript如何定义一个类,在定 ...

  3. shell中的数值运算

    By francis_hao    Oct 2,2017   本文摘录自bash的man手册.   算数运算相关的形式 形式 含义 ((expression)) expression按照下面描述的算术 ...

  4. getQueryString----获取url中得参数

    /** * 获取url中得参数 * @param name * @returns {null} */ window.getQueryString = function (name) { var reg ...

  5. [LeetCode] 10. Regular Expression Matching ☆☆☆☆☆

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  6. mysql 替换

    最近贷后好烦,经常让我修改短信模板内容,以前一两个模板手动就直接改了.随着短信模板的增多,手动一个个改内容就不行了. 今天又让我把短信模板中所有的的电话号码修改一下:如:010-44444444改为0 ...

  7. bzoj2516 电梯

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2516 [题解] 状压dp. $f_{sta,i}$表示状态为sta,当前在第i层的最小花费时 ...

  8. UITableView---iOS-Apple苹果官方文档翻译

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址  //转载请注明出处--本文永久链接:http://www.cnblogs.com/C ...

  9. [Unity]游戏Inside中的Chromatic Aberration效果学习

    Chromatic Aberration效果指的是模拟摄像机的拍摄瑕疵导致rgb三个通道的颜色发生了偏移,如 传统的Chromatic Aberration实现往往是基于一个后处理,将rgb采样的坐标 ...

  10. bzoj 2733 平衡树启发式合并

    首先对于一个连通块中,询问我们可以直接用平衡树来求出排名,那么我们可以用并查集来维护各个块中的连通情况,对于合并两个平衡树,我们可以暴力的将size小的平衡树中的所有节点删掉,然后加入大的平衡树中,因 ...