zoukankan      html  css  js  c++  java
  • 判断最小生成树是否唯一

    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);
        }
    }
    


  • 相关阅读:
    Java 泛型的使用
    IDEA 2017.2.2 环境下使用JUnit
    Tomcat的下载安装及使用
    JavaScript基础知识(二)
    JavaScript基础知识(一)
    CSS3基础(4)——CSS3 渲染属性
    CSS3基础(3)——CSS3 布局属性全接触
    CSS3基础(2)—— 文字与字体相关样式、盒子类型、背景与边框相关样式、变形处理、动画功能
    CSS3 基础(1)——选择器详解
    CSS基础学习笔记
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348179.html
Copyright © 2011-2022 走看看