zoukankan      html  css  js  c++  java
  • The Unique MST(次小生成树)

    The Unique MST

    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<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int maxn=10010;
    int t,n,m,father[110],b[110][110],c[110][110];
    bool flag[maxn],in[110][110];
    struct node
    {
        int x;
        int y;
        int w;
        bool operator < (node tmp)const
        {
            return w<tmp.w;
        }
    }a[maxn];
    int find(int x)
    {
        if(x!=father[x])
        father[x]=find(father[x]);
        return father[x];
    }
    int kruskal()
    {
        memset(flag,0,sizeof(flag));
        memset(in,0,sizeof(in));
        int sum=0,v=0;sort(a+1,a+m+1);
        for(int i=1;i<=n;i++)
        father[i]=i;
        for(int i=1;i<=m;i++)
        {
            int f1=find(a[i].x);
            int f2=find(a[i].y);
            if(f1!=f2)
            {
                father[f2]=f1;
                sum++;flag[i]=1;
                in[a[i].x][a[i].y]=in[a[i].y][a[i].x]=1;
                v+=a[i].w;
            }
            if(sum==n-1) break;
        }
        return v;
    }
    void dfs(int star,int now,int from,int maxx)
    {
        b[star][now]=maxx;
        for(int i=1;i<=n;i++)
        {
            if(!in[now][i]) continue;
            if(i==from) continue;
            dfs(star,i,now,max(maxx,c[now][i]));
        }
    }
    void prepare()
    {
        for(int i=1;i<=n;i++)
        dfs(i,i,i,0);
    }
    int main()
    {
        int x,y,z;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d",&n,&m);
            memset(a,0,sizeof(a));
            for(int i=1;i<=m;i++)
            {
                scanf("%d%d%d",&x,&y,&z);
                a[i].x=x,a[i].y=y,a[i].w=z;
                c[x][y]=c[y][x]=z;
            }
            int minn=kruskal(),ans=0x7fffffff;
            prepare();
            for(int i=1;i<=m;i++)
            {
                if(flag[i]) continue;
                int u=a[i].x,v=a[i].y;
                ans=min(ans,minn+a[i].w-b[u][v]);
            }
            if(ans==minn) printf("Not Unique!
    ");
            else printf("%d
    ",minn);
        }
        return 0;
    }
  • 相关阅读:
    Spring快速开启计划任务
    一张图告诉你什么是系统架构师
    一张图搞清楚Java异常机制
    Spring Cloud配置中心内容加密
    Spring Cloud配置中心高可用搭建
    Spring Cloud动态刷新配置信息
    Spring Boot实现热部署
    Spring Boot集成Mybatis双数据源
    (1)python tkinter-窗体
    (1)html基础
  • 原文地址:https://www.cnblogs.com/cax1165/p/6070866.html
Copyright © 2011-2022 走看看