zoukankan      html  css  js  c++  java
  • K

    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:

    V’ = V.
    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 <algorithm>
    using namespace std;
    #define maxn 105
    int fa[maxn], n, t, m, ans;
    bool flag = 0;
    struct Edge
    {
        int x, y, z;
        bool flag;
    } edge[maxn*maxn];
    bool cmp(Edge a,Edge b)
    {
        return a.z < b.z;
    }
    int find(int x)
    {
        if(x==fa[x])
            return x;
        return fa[x] = find(fa[x]);
    }
    int Kruskal(int k)  //删去原树中某边后可能无法构成树
    {
        int sum = 0;
        int cnt = 1;
        for (int i = 1; i <= n;i++)
            fa[i] = i;
        for (int i = 1; i <= m;i++)
        {
            if(i==k)
                continue;
            int x = find(edge[i].x);
            int y = find(edge[i].y);
            if(x==y)
                continue;
            fa[x] = y;
            cnt++;
            sum += edge[i].z;
        }
        if(cnt!=n)  //不连通
            return -1;
        return sum;
    }
    int main()
    {
        cin >> t;
        while(t--)
        {
            flag = 0;
            cin >> n >> m;
            for (int i = 1; i <= m;i++)
            {
                cin >> edge[i].x >> edge[i].y >> edge[i].z;
                edge[i].flag = 0;
            }
            for (int i = 1; i <= n;i++)
                fa[i] = i;
            sort(edge + 1, edge + 1 + m, cmp);
            int cnt = 0;
            for (int i = 1; i <= m;i++)
            {
                int x = find(edge[i].x);
                int y = find(edge[i].y);
                if(x==y)
                    continue;
                fa[x] = y;
                edge[i].flag = 1;   //标记这条边加入了MST
                ans += edge[i].z;
            }
            for (int i = 1; i <= m;i++)
            {
                if(edge[i].flag==0) //跳过不在树中的边
                    continue;
                int cnt = Kruskal(i);
                if(cnt==ans)
                {
                    flag = 1;
                    break;
                } 
            }
            if(flag)
                cout << "Not Unique!" << endl;
            else
                cout << ans << endl;
            ans = 0;
        }
    }
    

      

  • 相关阅读:
    深入理解MyBatis中的一级缓存与二级缓存
    Spring-mvc文件的上传和下载
    Spring-mvc的拦截器和异常通知
    各种配置文件
    设计模式---代理模式
    dom4j读取xml和dtd的使用方式
    几种不同的路径
    常用正则表达式
    请求转发和重定向的对比
    跨浏览器检测某个节点是不是另一个节点的后代
  • 原文地址:https://www.cnblogs.com/zssst/p/11337840.html
Copyright © 2011-2022 走看看