杭电专题四1008

来源:互联网 发布:纯文字游戏制作软件 编辑:程序博客网 时间:2024/05/29 12:30

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.<br><br>
 

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.<br>The last test case is followed by a line containing two zeros, which means the end of the input.<br>
 

Output
Output the sum of the value of the edges of the maximum pesudoforest.<br>
 

Sample Input

1、题目大意:

 

求一个图的最大联通子图,要求每个联通分量最多只有一个环,且所求的边的权值之和最大,

输入包括多组样例,每个样例第一行包含n/m两个整数,分别代表图中顶点的个数,边的个数。接下来的m行,每行有三个整数,分别表示一条边的起点和终点及权值,不存在圈不存在重复边,输入00结束

每组样例输出一个整形数,表示最大权值之和

2、思路:

每输入一条边,判断此边两端点是不是在同一颗树上,如果在同一颗树上,判断树是不是有环,如果有环,则不加入此边,如果没环,加入此边(合并);

如果两棵树都没有环,直接合并即可,

如果只有一棵树有环,可以合并,并标记,

如果都有环,显然不能合并

3、感想:

4代码:

#include<stdio.h>  
#include<algorithm>  
#include<string.h>  
using namespace std;  
int visit[10005];  
int set[10005];  
struct node  
{  
    int s;  
    int e;  
    int len;  
}a[100005];  
int cmp(node a,node b)  
{  
    return a.len>b.len;  
}  
int find(int x)  
{  
    int r=x;  
    while(r!=set[r])  
    r=set[r];  
    int i=x;  
    while(i!=r)  
    {  
        int j=set[i];  
        set[i]=r;  
        i=j;  
    }  
    return r;  
}  
int main()  
{  
    //freopen("E:\acm寒假集训\测试样例\测试数据.txt","r",stdin);  
    int n,m;  
    while(scanf("%d%d",&n,&m)!=EOF)  
    {  
        memset(a,0,sizeof(a));  
        int ans=0;  
        if(n==0&&m==0)  
        break;  
        for(int i=0;i<=n;i++)//初始化错了i=0;  
        {  
            set[i]=i;  
            visit[i]=0;  
        }  
        for(int i=1;i<=m;i++)  
              scanf("%d%d%d",&a[i].s,&a[i].e,&a[i].len);  
              sort(a+1,a+m+1,cmp);  
        for(int i=1;i<=m;i++)  
        {  
            int fx=find(a[i].s);  
            int fy=find(a[i].e);  
            if(fx==fy)//存在环  
            {  
                if(visit[fx]==1)  
                continue;  
                visit[fx]=1;//标记根节点,此树已存在环  
            }  
            else  
            {  
                if(visit[fx]==1&&visit[fy]==1)//如果2棵树都存在环,跳出  
                continue;  
                else if(visit[fx]==0)//如果只有一颗树有环,可以合并  
                    set[fx]=fy;  
                else//如果2棵树都没环,显然可以合并  
                set[fy]=fx;  
            }  
            ans+=a[i].len;  
        }  
        printf("%d\n",ans);  
    }  
    return 0;  




0 0
原创粉丝点击