zoukankan      html  css  js  c++  java
  • HDU 3367 Pseudoforest 最大生成树

    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
    ",ans);
    73     }
    74     
    75     return 0;
    76 }
    View Code
    Do Cool Things That Matter!
  • 相关阅读:
    清除浮动的方法
    手机端横竖屏切换,怎么做才能安卓浏览器及时改变字体大小
    css z-index之object flash修正
    解决Flash挡住层用z-index无效的问题
    css垂直居中
    自定义selsct
    基础导航条
    按钮下拉菜单
    pandas的使用
    数据分析、人工智能开篇
  • 原文地址:https://www.cnblogs.com/jcf94/p/3963197.html
Copyright © 2011-2022 走看看