ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Day5 - G - The Unique MST POJ - 1679

2020-01-11 11:52:57  阅读:239  来源: 互联网

标签:MST int Day5 tree cost POJ connected spanning total


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\n", sum);
        else printf("Not Unique!\n");
    }
    return 0;
}
View Code

 


标签:MST,int,Day5,tree,cost,POJ,connected,spanning,total
来源: https://www.cnblogs.com/GRedComeT/p/12179482.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有