poj1679 The Unique MST

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!

 

 

#include<algorithm>

#include<cstdio>

#include<cstring>

#include<iostream>

using namespace std;

#define N 10090

struct node

{

    int st,ed,val;

    bool same,used,del;

    //used表示在第一次求出的最小生成树中添加过的边

    //same表示在存在和已添加边权值相同的边

    //del标记之后生成的树的边

}e[N];//把每条边成为一个结构体,包括起点、终点和权值

int pre[N];

int n,m;

bool first;//判断是不是第一次生成最小生成树

int cmp(node a,node b)

{

    return a.val<b.val;//按照价值从小到大排

}

void make()

{

    for(int i=0;i<=205;i++)

    {

        pre[i] = i;

    }

}//把每个元素初始化

int Find(int m)

{

    int a=m;

    while(a!=pre[a])

        a=pre[a];

    //pre[m]=n;

    return a;

}//查找一个元素所在的集合,即找到根节点

void make_Same(int m)

{

    for(int i=1;i<m;i++)

    {

        if(e[i].val==e[i-1].val)

        {

            e[i-1].same=true;

        }

    }

}

int kruskal(int m)//n为边的数量

{

    int sum=0;

    for(int i=0;i<m;i++)

    {

        if(e[i].del)

            continue;

        int fx=Find(e[i].st);

        int fy=Find(e[i].ed);

        if(fx!=fy)

        {

            pre[fx]=fy;//合并x,y

            sum+=e[i].val;

            if(first)

                e[i].used=true;

        }

    }

    return sum;

}

int main()

{

    int T;

    scanf("%d",&T);

    while(T--)

    {

        scanf("%d%d",&n,&m);

        for(int i=0;i<m;i++)

        {

            cin>>e[i].st>>e[i].ed>>e[i].val;

            e[i].same=false;

            e[i].used=false;

            e[i].del=false;

        }

        sort(e,e+m,cmp);

        first=true;

        make();

        int ans1=kruskal(m);

        make_Same(m);

        first=false;

        int flag=0;

        for(int i=0;i<m;i++)

        {

            if(e[i].used&&e[i].same)//used表示在第一次求出的最小生成树中添加过的边

            {//same表示在存在和已添加边权值相同的边,此时标记删除该边在判断是否ans相等

                e[i].del=true;

                make();

                int ans2=kruskal(m);

                if(ans1==ans2)

                {

                    printf("Not Unique!\n");

                    flag=1;

                    break;

                }

                e[i].del=false;

            }

        }

        if(flag==0)

        printf("%d\n",ans1);

    }

    return 0;

}

 

题意:判断最小生成树是否唯一。唯一输出权值。

【分析】最小生成树的唯一性,思路是先判断每条边是否有重边,有的话eq=1,否则0.然后第一次求出最小生成树,将结果记录下来,
 然后依次去掉第一次使用过的且含有重边的边,再求一次最小生成树,若结果与第一次结果一样,则不唯一。

 

思路:先求出一颗最小生成树,然后标记权值相等的边,将权值相等的边删去,重新生成一个最小生成树,判断两棵树的值是否相等。(循环实现)

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值