POJ1679-The Unique MST(最小生成树)

Description

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
//题意:给你一些结点和权值,然后求出最小生成树,问这一棵最小生成树是不是唯一的。
如果是就输出最小生成树的答案,否则就输出“ Not Unique!”。
解题思路:刚一开始就想到先用克鲁斯卡尔算法求出最小生成树,保存下其大小,并且在开一个结构体
保存这一个最小生成树的所有的结点。然后在枚举不选举其中的每一个的结点,在求一遍最小生成树。
如果有最小其中答案与其相同的则不是最小生成树。
写的时候还是有几个需要注意的地方。WA了好几次。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=110000;
struct Node
{
    int from;
    int to;
    int w;
    bool operator<(const Node &c)const
    {
        return w<c.w;
    }
} E[maxn];
struct Node2
{
    int x,y;
} k[maxn];
int p[maxn];
int n,m;
int cha(int x)
{
    if(p[x]==-1)
        return x;
    return p[x]=cha(p[x]);
}
void lxsb()
{
    int ans=0,cnt=0,f=0;
    for(int i=0; i<m; i++)
    {
        int x=cha(E[i].from);
        int y=cha(E[i].to);
        if(x!=y)
        {
            ans+=E[i].w;p[x]=y;
            if(E[i].w==0)
                continue;//去除那些权值为0的情况。删除权值为0的结点,求出的最小生成树不影响
            k[cnt].x=E[i].from;
            k[cnt].y=E[i].to;
            cnt++;

        }
    }
    int sum=0;
    for(int j=0; j<cnt;j++)
    {
        sum=0;
        memset(p,-1,sizeof(p));//注意这里要初始化~
        for(int i=0; i<m; i++)
        {
            int x=cha(E[i].from);
            int y=cha(E[i].to);
            if(E[i].from==k[j].x&&E[i].to==k[j].y)
                continue;
            if(x!=y)
            {
                sum+=E[i].w;
                p[x]=y;
            }
        }
        if(sum==ans)
        {
            f=1;
            break;
        }
    }
    if(f)
        puts("Not Unique!");
    else
        printf("%d\n",ans);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(p,-1,sizeof(p));
        scanf("%d%d",&n,&m);
        for(int i=0; i<m; i++)
            scanf("%d%d%d",&E[i].from,&E[i].to,&E[i].w);
        sort(E,E+m);
        lxsb();
    }
    return 0;
}
/*
附上一组测试数据
1
6 7
1 3 1
1 2 2
2 3 3
3 4 0
4 6 5
4 5 4
5 6 6
*/



你可能感兴趣的:(POJ1679-The Unique MST(最小生成树))