POJ 1679 The Unique MST【暴力求次小生成树】

The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 26269 Accepted: 9381

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!

Source


题目大意:让你求在连通图里边是否存在一颗次小生成树的值和最小生成树的值相同,如果有,那么输出mst的值,否则输出NOT UNIQUE!

思路:


第二次做这个题,最近在学次小生成树的相关问题,看到了这个例题,我就再来做一次,上一次的方法和这次方法不同,有兴趣的同学也可以看看那种解法,连接:http://blog.csdn.net/mengxiang000000/article/details/51166896


这一次的解法我们采用暴力求次小生成树的方法,首先在克鲁斯卡尔的算法过程中标记选择入树的边,对其标记,然后暴力枚举这n-1条边,如果把这条边去掉之后看看能否找到能够替换这个边入树的边存在,如果存在,辣么我们就能求出当前情况的生成树的值,然后维护这样暴力枚举出来的生成树的值,求得最小值,如果这个值==mst的值,辣么就输出mst的值,否则输出Not Unique!


AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct path
{
    int x,y,w,flag;
}a[12102];
int f[12102];
int mst,n,m;
int cmp(path a,path b)
{
    return a.w<b.w;
}
void init()
{
    for(int i=0;i<=n;i++)
    {
        f[i]=i;
    }
}
int find(int x)
{
    return f[x] == x ? x : (f[x] = find(f[x]));
}
void merge(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
    f[B]=A;
}
void kruskal()//克鲁斯卡尔算法求最小生成树的值
{
    init();
    mst=0;
    for(int i=0;i<m;i++)
    {
        if(find(a[i].x)!=find(a[i].y))
        {
            merge(a[i].x,a[i].y);
            mst+=a[i].w;
            a[i].flag=1;
        }
    }
}
int  kruskal2()
{
    for(int i=0;i<m;i++)
    {
        if(a[i].flag==1)//暴力枚举在最小生成树中的边,然后去掉
        {
            init();
            int tmp=0;
            int k=0;
            for(int j=0;j<m;j++)//去掉之后再来一次克鲁斯卡尔求当前状态的最小生成树
            {
                if(i==j)continue;
                if(find(a[j].x)!=find(a[j].y))
                {
                    merge(a[j].x,a[j].y);
                    tmp+=a[j].w;
                    k++;
                }
            }
            //printf("%d %d\n",k,tmp);
            if(k!=n-1)continue;
            if(tmp==mst)return 1;//如果存在这样一颗树的值和mst的值相同.return 1
        }
    }
    return 0;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);
            a[i].flag=0;
        }
        sort(a,a+m,cmp);
        kruskal();
        if(kruskal2()==0)
        {
            printf("%d\n",mst);
        }
        else printf("Not Unique!\n");
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值