zoukankan      html  css  js  c++  java
  • 次小生成树

    The Unique MST
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 30194   Accepted: 10809

    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!
    

    Source

    【解析】
    先生成最小生成树,再枚举删去每一条边,看看看生成的次小生成树与最小生成树是否相等。
    【代码】
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    #define N 101
    struct Edge
    {
        int x,y,z;
    }edge[2*N];
    int t,x,y,flag,ans,ans2,n,m,num2;
    int far[N];
    bool path[2*N];
    void set()
    {
        for(int i=1;i<=n;i++)
        far[i]=i;
    }
    void unionn(int x,int y)
    {
        far[x]=y;
    }
    int f(int x)
    {
        far[x]==x?x:far[x]=f(far[x]);
    }
    bool cmp(Edge a,Edge b)
    {
        return a.x<b.y;
    }
    void kruskal()
    {
        set();
        int num1=0;
        sort(edge+1,edge+m+1,cmp);
        for(int i=1;i<=m;i++)
        {
            int fx=f(edge[i].x),fy=f(edge[i].y);
            if(fx!=fy)
            {
                unionn(fx,fy);
                ans+=edge[i].z;
                path[++num1]=i;
            }
            if(num1==n-1)
            break;
        }
        
        for(int k=1;k<=num1;k++)
        {
        set();
            ans2=0;num2=0;
            for(int i=1;i<=m;i++)
            {
                if(i==path[i])
                continue;
                int fx=f(edge[i].x),fy=f(edge[i].y);
                if(fx!=fy)
                {
                    unionn(fx,fy);
                    ans2+=edge[i].z;
                    num2++;
                }
                if(num2==n-1&&ans2==ans)flag=1;
            }
        }
    }
    
    int main()
    {
        scanf("%d",&t);
        while(t--)
        {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++)
        scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].z);
        flag=0;
        ans=0;
        kruskal();
        if(flag)
        printf("Not Unique!
    ");
        else
        printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    Android系统源代码下载
    Windows Embedded Compact 7初体验
    windowsmobile 开发环境
    Windows X64汇编入门(1)
    汇编语言的Hello World
    如何构建Win32汇编的编程环境(ONEPROBLEM个人推荐)
    音频视频解决方案:GStreamer/ffmpeg/ffdshow/directshow/vfw
    汇编开发环境
    DirectX
    关于DirectShow SDK 和Windows SDK,及DirectX SDK
  • 原文地址:https://www.cnblogs.com/zzyh/p/6830594.html
Copyright © 2011-2022 走看看