zoukankan      html  css  js  c++  java
  • Day5

    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!

    思路:找次小生成树,如果权值相等则不唯一,用kruskal实现次小生成树
    const int maxm = 105;
    const int maxn = 10005;
    
    struct edge {
        int u, v, w;
        edge(int _u=-1, int _v=-1, int _w=0):u(_u), v(_v), w(_w){}
        bool operator<(const edge &a) const {
            return w < a.w;
        }
    };
    vector<edge> Edge;
    
    int fa[maxm], T, N, M, tree[maxn], k;
    
    void init() {
        Edge.clear();
        for(int i = 1; i <= N; ++i)
            fa[i] = i;
        k = 0;
    }
    
    int Find(int x) {
        if(fa[x] == x)
            return x;
        return fa[x] = Find(fa[x]);
    }
    
    void Union(int x, int y) {
        x = Find(x), y = Find(y);
        if(x != y) fa[x] = y;
    }
    
    int main() {
        scanf("%d", &T);
        while(T--) {
            int t1, t2, t3, u, v;
            scanf("%d%d", &N, &M);
            init();
            int sum = 0;
            for(int i = 0; i < M; ++i) {
                scanf("%d%d%d", &t1, &t2, &t3);
                Edge.push_back(edge(t1, t2, t3));
            }
            sort(Edge.begin(), Edge.end());
            bool flag = true;
            for(int i = 0; i < M; ++i) {
                u = Edge[i].u, v = Edge[i].v;
                u = Find(u), v = Find(v);
                if(u != v) {
                    sum += Edge[i].w;
                    Union(u,v);
                    tree[k++] = i;
                }
            }
            for(int i = 0; i < k; ++i) {
                int cnt = 0, edgenum = 0;
                for(int t = 1; t <= N; ++t)
                    fa[t] = t;
                for(int j = 0; j < M; ++j) {
                    if(j == tree[i]) continue;
                    u = Edge[j].u, v = Edge[j].v;
                    u = Find(u), v = Find(v);
                    if(u != v) {
                        cnt += Edge[j].w;
                        edgenum++;
                        Union(u,v);
                    }
                }
                if(cnt == sum && edgenum == N - 1) {
                    flag = false;
                    break;
                }
            }
            if(flag)
                printf("%d
    ", sum);
            else printf("Not Unique!
    ");
        }
        return 0;
    }
    View Code

     次小生成树博客:https://www.cnblogs.com/bianjunting/p/10829212.html

    https://blog.csdn.net/niushuai666/article/details/6925258

    注:这里的Max数组是记录从i到j节点中边权最大值(不是和),从其父节点与新连接的边中比较

    
    
  • 相关阅读:
    Python使用inspect查看代码参数
    Python的hasattr() getattr() setattr()
    Module-GitBook使用手册
    Module-Hadoop技术文档
    Module-Git使用手册
    Module-Faker使用手册
    Module-Docker使用手册
    Module-DB2技术文档
    Module-Dask并行任务调度
    Module-Apache使用说明
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/12179482.html
Copyright © 2011-2022 走看看