HDU 3367 Pseudoforest 最小生成树

来源:互联网 发布:ios抓取网页数据 编辑:程序博客网 时间:2024/06/05 05:43

Pseudoforest

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


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 30 1 11 2 12 0 14 50 1 11 2 12 3 13 0 10 2 20 0
 

Sample Output
35
/*HODJ 3367 最小生成树K,判断有无环的出现 */#include<iostream>#include<stdio.h>#include<algorithm>using namespace std;struct node{        int x,y,dis;};node e[100001];int pre[10001],circle[10001];int cmp(node a,node b){    return a.dis>b.dis;}int find(int x){    if(x!=pre[x])        return pre[x]=find(pre[x]);    return pre[x];}int main(){    int n,m,i,j,sum,x,y,a,b,v;     //    freopen("test.txt","r",stdin);    while(scanf("%d%d",&n,&m))    {        if(n==0&&m==0) break;                 for(i=0;i<n;i++)// 注意题目是从0开始的             pre[i]=i;        memset(circle,0,sizeof(circle));                        for(i=0;i<m;i++)        {            scanf("%d%d%d",&a,&b,&v);            e[i].x=a;            e[i].y=b;            e[i].dis=v;                }            sort(e,e+m,cmp);                sum=0;        for(i=0;i<m;i++)        {            x=find(e[i].x);            y=find(e[i].y);            if(x!=y)            {                if(!circle[x]&&!circle[y])//两个都不是环                 {                    sum+=e[i].dis;                    pre[x]=y;                    }                else if(!circle[x]||!circle[y])//其中有一个是环,合并后两个都是环                 {                    pre[x]=y;                    sum+=e[i].dis;                    circle[x]=circle[y]=1;                }            }            else// x==y表示两个是一个集合里的,有相同的根                 //如果x还没构成环,现在又多条路径,构成环                 //因为e[i].x,e[i].y是一条路径,现在又有相同的根x,构成环             {                if(!circle[x])//!circle[y]一样,x==y                 {                    circle[x]=1;                    sum+=e[i].dis;                }            }        }        printf("%d\n",sum);    }    return 0;}



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 华科办临时饭卡怎么办 血浆乳酸测定高怎么办 olay大红瓶过敏怎么办 饥一顿饱一顿怎么办 朝鲜人向去韩国怎么办? 我是超级大方脸怎么办 被精神病砍断四根筋怎么办 40小时不睡觉怎么办 边防转业军人安置不好怎么办 转业军人孩子读书怎么办玉环 军官证丢了怎么办 飞行学员停飞了怎么办 丹东港债券违约怎么办 对村党支部贿选怎么办 马自达2油耗高怎么办 马自达1.5油耗高怎么办 马自达5停产配件怎么办 期货账户休眠了怎么办 低于二本线怎么办 听力测试不过关怎么办 安装的天正过期怎么办 天正t20v4过期了怎么办 鸡肉菊花一起吃怎么办 孕妇误食马兰头怎么办 孕29周喉咙痛怎么办 哺乳期吃了当归怎么办 哺乳期喝了茶水怎么办 大人感染eb病毒怎么办 下面一直有血怎么办 猫咪吐红色液体怎么办 宝宝拉屎有血水怎么办 屁眼火辣辣的疼怎么办 屁眼拉屎有血怎么办 转业干部退档案怎么办 学校遇到不公待遇怎么办 孕妇受到不公待遇怎么办 强制停机骗局该怎么办 号码被别人举报怎么办 农业银行卡有效期到了怎么办 电信欠费2000多怎么办 被移动强制停机怎么办