POJ1679 The Unique MST

来源:互联网 发布:海岛奇兵建筑升级数据 编辑:程序博客网 时间:2024/05/17 09:05

The Unique MST
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 29902 Accepted: 10697

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

23 31 2 12 3 23 1 34 41 2 22 3 23 4 24 1 2

Sample Output

3Not Unique!

Source


————————————————————————————————————
题目意思是判最小生成树是否唯一,唯一输出最小生成树,否则输出提示语
思路:找出次小生成树,判断和最小生成树是否一样。找次小生成树的时候可以枚举去掉最小生成树上的每一条边,在在剩下的边找最小生成树。

#include <iostream>#include<queue>#include<cstdio>#include<algorithm>#include<cmath>#include<set>#include<cstring>using namespace std;#define LL long longstruct node{    int u,v,w,flag;} p[100005];int n,m,cnt,tot,pre[1005],f[100005];bool cmp(node a,node b){    return a.w<b.w;}void init(){    for(int i=0; i<1004; i++)        pre[i]=i;}int fin(int x){    return pre[x]==x?x:pre[x]=fin(pre[x]);}int kruskal(int xx){    init();    int cost=0;    int ans=0;    int flg=0;    for(int i=0; i<cnt; i++)    {        if(f[i])            continue;        int a=fin(p[i].u);        int b=fin(p[i].v);        if(a!=b)        {            pre[a]=b;            cost+=p[i].w;            ans++;            if(xx==1)                p[i].flag=1;        }    }   if(ans==n-1)        return cost;    else        return -1;}int main(){    int T;    scanf("%d",&T);    while(T--)    {        memset(p,0,sizeof p);        memset(f,0,sizeof f);        scanf("%d%d",&n,&cnt);        for(int i=0; i<cnt; i++)            scanf("%d%d%d",&p[i].u,&p[i].v,&p[i].w);        sort(p,p+cnt,cmp);        int mintree=kruskal(1);        int sectree=999999999;        for(int i=0; i<cnt; i++)        {            if(p[i].flag==1)            {                f[i]=1;                int ans=kruskal(0);                if(ans!=-1)                    sectree=min(sectree,ans);                f[i]=0;            }        }        if(sectree==mintree)            printf("Not Unique!\n");        else            printf("%d\n",mintree);    }    return 0;}









0 0
原创粉丝点击