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;
    }
  • 相关阅读:
    Context Menu on DataGrid
    DataGrid Column Group (合并表头)
    DataGrid loadData loadFilter
    sharepoint 2010 FieldLookup 的更新方法
    SharePoint 2010 列表查阅项栏的formfield控件对象取值
    sharepoint2010的几个类型字段赋值和取值的方法
    SharePoint 和 Windows Phone 7 开发人员培训资源
    如何提升SharePoint 2010的性能
    sharepoint国内网站一览表(转发)
    sharepoint2010列表的分页实现迅雷样式效果
  • 原文地址:https://www.cnblogs.com/zzyh/p/6830594.html
Copyright © 2011-2022 走看看