• 判断最小生成树是否唯一


    http://poj.org/problem?id=1679

    The Unique MST
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 20487   Accepted: 7207

    Description

    Given a connected undirected graph, tell if its minimum spanning tree is unique. 

    Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the following properties: 
    1. V' = V. 
    2. T is connected and acyclic. 

    Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E') of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E'. 

    Input

    The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.

    Output

    For each input, if the MST is unique, print the total cost of it, or otherwise print the string 'Not Unique!'.

    Sample Input

    2
    3 3
    1 2 1
    2 3 2
    3 1 3
    4 4
    1 2 2
    2 3 2
    3 4 2
    4 1 2
    

    Sample Output

    3
    Not Unique!
    程序:

    #include"stdio.h"
    #include"string.h"
    #include"stack"
    #include"math.h"
    #include"algorithm"
    #include"stdlib.h"
    #define M 222
    #define inf 100000000
    #define eps 1e-12
    using namespace std;
    struct node
    {
        int u,v,w;
    }e[M*M];
    int cmp(node a,node b)
    {
        return a.w<b.w;
    }
    int f[M];
    int finde(int x)
    {
        if(x!=f[x])
            f[x]=finde(f[x]);
        return f[x];
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            int n,m,i,j;
            scanf("%d%d",&n,&m);
            for(i=0;i<m;i++)
                scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
            sort(e,e+m,cmp);
            int num=0;
            int fuck=0;
            int sum=0;
            for(i=1;i<=n;i++)
                f[i]=i;
            for(i=0;i<m;)
            {
                j=i;
                while(e[i].w==e[j].w&&j<m)
                {
                    int x=finde(e[j].u);
                    int y=finde(e[j].v);
                    if(x!=y)
                        fuck++;
                    j++;
                }
                j=i;
                while(e[i].w==e[j].w&&j<m)
                {
                    int x=finde(e[j].u);
                    int y=finde(e[j].v);
                    if(x!=y)
                    {
                        f[x]=y;
                        sum+=e[i].w;
                        num++;
                    }
                    j++;
                }
                i=j;
                if(num==n-1)
                    break;
            }
            if(fuck>num)
                printf("Not Unique!
    ");
            else
                printf("%d
    ",sum);
            //printf("%d %d
    ",fuck,num);
        }
    }
    


  • 相关阅读:
    Jenkins自动化多项目编译和Tomcat部署懒人终极大招
    python 装饰器总结
    selenium3之-测试环境搭建
    centos7.4 安装ftp服务器并配置匿名用户权限
    selenium3之-运行原理
    flutter 打包apk
    Fluwx:微信SDK在Flutter上的实现
    flutter 购物车功能
    flutter sharesdk实现跨平台分享
    Web API接口设计经验总结
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348179.html
Copyright © 2020-2023  润新知