POJ 1679 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
Sample Output

3
Not Unique!

本题的题目目的:让你判断最小生成树是不是唯一,如果唯一的话就输出对应的值,否则的话就输出给的那句话。

方法:最小生成树+边的枚举实现,首先建立最小生成树的时候把组成最小生成树的边都记录下来,然后依次去掉每条边 看是否依旧能组成值的大小一样的最小生成树,注意其中的剪枝,要慢慢学会剪枝了,渣渣太弱了,要慢慢的学会总结,呜呜呜~~~。
下面是AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

struct node
{
    int u,v,cost;
}a[10005];
int pre[105];
int fin(int x)
{
    if(pre[x]==x)
    {
        return x;
    }
    else
    {
        pre[x]=fin(pre[x]);
        return pre[x];
    }
}

void join(int x,int y)
{
    int t1=fin(x);
    int t2=fin(y);
    if(t1!=t2)
    {
        pre[t1]=t2;
    }
    return ;
}

void init(int n)
{
    for(int i=0;i<=n;i++)
    {
        pre[i]=i;
    }
}

bool cmp(node x,node y)
{
    return x.cost<y.cost;
}

int b[10005];

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d %d",&n,&m);
        init(n);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].cost);
        }
        sort(a,a+m,cmp);
        int sum=0;
        int cnt=0;
        int cnt1=0;
        for(int i=0;i<m;i++)
        {
            int cnt2=0;
            if(fin(a[i].u)!=fin(a[i].v))
            {
                join(a[i].u,a[i].v);
                b[cnt]=i;
                cnt++;
                cnt2++;
                sum+=a[i].cost;
            }
            if(cnt1==n-1)
            {
                break;
            }
        }
        int flag=0;
        for(int i=0;i<cnt;i++)
        {
            init(n);
            int sum1=0;
            int cnt2=0;
            for(int j=0;j<n;j++)
            {
                if(b[i]==j)
                {
                    continue;
                }
                if(fin(a[j].u)!=fin(a[j].v))
                {
                    cnt2++;
                    join(a[j].u,a[j].v);
                    sum1+=a[j].cost;
                }
                if(cnt2==n-1)
                {
                    break;
                }
            }
            if(sum1==sum)
            {
                flag=1;
                break;
            }
        }
        if(flag==1)
        {
            printf("Not Unique!\n");
        }
        else
        {
            printf("%d\n",sum);
        }
    }
    return 0;
}

你可能感兴趣的:(poj)