ICode9

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

POJ 1679-The Unique MST【最小生成树的唯一性】

2019-01-26 22:01:34  阅读:317  来源: 互联网

标签:int MST tree edge POJ 1679 connected spanning include


题目描述

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,对于树中的边,每次删除一个有重复权值的边,再跑一遍Kruskal,如果删除后得到的值与删除前相同,则最小生成树是不唯一的。

由于这道题的数据不大,我直接依次删除最小生成树的边(没有判断是否有重复的权),然后判断删除前后的值。

注意!!这道题由于权值有0的情况,所以如果删除前后的值相同,还需判断最小生成树的边是否等于n-1,若等于,则不唯一,若不等于,仍唯一。

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;

const int Maxe = 10005;
const int Maxn = 105;
struct edge{
    int from,to,val;
    bool canuse;
};
edge e[Maxe];
int par[Maxn];
int cnt;
int ct;
vector<int> v;

bool cmp(edge e1, edge e2)
{
    return e1.val<e2.val;
}
void Init()
{
    for(int i = 0; i < Maxn; i++)
        par[i] = i;
}
int Find(int x)
{
    if(x==par[x])
        return par[x];
    else
        return par[x] = Find(par[x]);
}
bool Union(int x, int y)
{
    x = Find(x);
    y = Find(y);
    if(x!=y)
    {
        par[y] = x;
        return true;
    }
    else
        return false;
}
int Kruskal(int x)
{
    int res = 0;
    Init();
    for(int i = 0; i < cnt; i++)
    {
        if(e[i].canuse&&Union(e[i].from, e[i].to))
        {
            ct++;
            res+=e[i].val;
            if(x==0)
                v.push_back(i);
        }
    }
    return res;
}


int main()
{
    int t,n,m,val;
    bool flag;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d %d", &n, &m);
        cnt = m;
        ct = 0;
        flag = true;
        v.clear();
        for(int i = 0; i < m; i++)
        {
            scanf("%d %d %d", &e[i].from, &e[i].to, &e[i].val);
            e[i].canuse = true;
        }
        sort(e,e+cnt,cmp);
        val = Kruskal(0);
        for(int i = 0; i < v.size(); i++)
        {
            ct = 0;
            e[v[i]].canuse = false;
            if(val==Kruskal(1)&&ct==n-1)
            {
                flag = false;
                break;
            }
            e[v[i]].canuse = true;
        }
        if(flag)
            printf("%d\n", val);
        else
            printf("Not Unique!\n");
    }
    return 0;
}

如有错误请指明~   ฅ●ω●ฅ

标签:int,MST,tree,edge,POJ,1679,connected,spanning,include
来源: https://blog.csdn.net/include_peng/article/details/86661553

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

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

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

ICode9版权所有