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

题目描述

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;
}

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

上一篇:[JLOI2010] 冠军调查 - 最小割


下一篇:Docker Zookeeper 单节点+集群部署