HDU 3367 Pseudoforest 最大生成树

本文介绍了一种求解最大伪森林的问题,通过Kruskal算法的变种实现,确保每个连通分量最多只包含一个环。代码示例展示了如何进行边的排序、并查集操作及环的检测。

 

Pseudoforest

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

【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

【题意】

给出一张图,求最大生成树。要求每一个连通块上只能有一个环。

【分析】

对于Kruskal来说,最小/最大生成树只是改变一下排序顺序即可。这里需要另外注意添加的就是对环的判断了:

如果两个节点不在同一棵树内,且分别不成环,则可合并;

如果两个节点在同一棵树内,但是未成环,则加上这条边之后将成环;

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 
 6 using namespace std;
 7 
 8 typedef struct nod
 9 {
10     int x,y,c;
11 } node;
12 node a[100010];
13 
14 bool op(node a,node b)
15 {
16     return a.c>b.c;
17 }
18 
19 int father[10010];
20 bool flag[10010];
21 
22 void clean_father(int n)
23 {
24     for (int i=0;i<n;i++) father[i]=i;
25 } 
26 
27 int getfather(int x)
28 {
29     if (father[x]!=x) father[x]=getfather(father[x]);
30     return father[x];
31 }
32 
33 void link(int x,int y)
34 {
35     father[getfather(x)]=getfather(y);
36 } 
37 
38 int main()
39 {
40     int n,m;
41     while (scanf("%d%d",&n,&m))
42     {
43         if (n==0&&m==0) break;
44         
45         for (int i=1;i<=m;i++) scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].c);
46         sort(&a[1],&a[m+1],op);
47         
48         clean_father(n);
49         memset(flag,0,sizeof(flag));
50         int ans=0;
51         for (int i=1;i<=m;i++)
52         if (getfather(a[i].x)!=getfather(a[i].y))
53         {
54             if (!(flag[getfather(a[i].x)]&&flag[getfather(a[i].y)]))
55             {
56                 
57                 if (flag[getfather(a[i].x)]||flag[getfather(a[i].y)])
58                 {
59                     flag[getfather(a[i].x)]=true;
60                     flag[getfather(a[i].y)]=true;
61                 }
62                 link(a[i].x,a[i].y);
63                 ans+=a[i].c;
64             }
65         } else 
66         if (!flag[getfather(a[i].x)])
67         {
68             ans+=a[i].c;
69             flag[getfather(a[i].x)]=true;
70         }
71         
72         printf("%d\n",ans);
73     }
74     
75     return 0;
76 }
View Code

 

转载于:https://www.cnblogs.com/jcf94/p/3963197.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值