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

    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 22335   Accepted: 7922

    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

    思路:求图的次小生成树,网上找的思路,先求mst,然后枚举删除mst中的每一条边,判断新得到的mst'是否等于mst
    /*times memy
     63ms   804k
     by orchid
     */
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <iostream>
    #define INF 1 << 25
    using namespace std;
    int n, m, ans;
    int p[120], r[5000];
    int u[5000], v[5000], w[5000];
    bool flag[5000];//标记最小生成树中的所有边
    int mst;//
    void init()
    {
        for(int i = 1; i <= n ; i++)
        p[i] = i;
        for(int i = 1; i <= m; ++i)
        r[i] = i;
    }
    int cmp(int i,int j)//按权值间接排序
    {
        return w[i] < w[j];
    }
    int find(int x)//并查集路径压缩
    {
        return p[x] == x? x:p[x] = find(p[x]);
    }
    void read_graph()
    {
        cin >> n >> m;
        for(int i = 1; i <= m; ++i)
        cin >> u[i] >> v[i] >> w[i];
    }
    void kruskal()//求mst
    {
        sort(r + 1,r + m + 1,cmp);
        for(int i = 1; i <= m; ++i)
        {
            int e = r[i];
            int x = find(u[e]);
            int y = find(v[e]);
            if(x != y){ ans += w[e];flag[e] = false; p[x] = y; }
        }
    }
    void kruskal2()//求次小生成树
    {
        sort(r + 1,r + m + 1,cmp);
        for(int i = 1 ; i <= n; ++i) p[i] = i; //记得恢复并查集中的p[]
        for(int i = 1; i <= m; ++i)
        {
            int e = r[i];
            int x = find(u[e]);
            int y = find(v[e]);
            if(x != y){ ans += w[e]; p[x] = y; }
        }
    }
    void solve()
    {
        int i;//初始化
        memset(flag,true,sizeof flag);
        read_graph();
        init();
             //求mst
        ans = mst  = 0;
        kruskal();
        mst = ans;
    
        int tp, tr;
        tp = tr = 0;
        for(i = 1; i <= m; ++i)
        if(!flag[i])//寻找mst中的边
        {
            ans = 0;
            w[tp] = tr;//恢复上一条去除的边
    
            tp = i;tr = w[i];
            w[i] = INF;//去除此条边
    
            kruskal2();
            if(ans == mst) break;
        }
         //cout << fst << ' ' << ans <<endl;
        if(i <= m) {cout<< "Not Unique!" <<endl; }
        else cout << mst <<endl;
    }
    int main()
    {
        ios::sync_with_stdio(0);
        int t;
        cin >> t;
        while(t--)
        solve();
        return 0;
    }
  • 相关阅读:
    对物联网的认识
    读书笔记
    Intel:从屌丝逆袭成业界大佬
    实模式:奇葩的存在
    depot_tools Google代码管理工具包
    std::out_of_range异常
    SensorMode选择
    shell脚本学习(2)查找
    shell脚本学习(1)入门
    输入子系统
  • 原文地址:https://www.cnblogs.com/orchidzjl/p/4438242.html
Copyright © 2011-2022 走看看