poj 1679-The Unique MST

Posted Alex丶Baker

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 1679-The Unique MST相关的知识,希望对你有一定的参考价值。

http://poj.org/problem?id=1679

 

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!

描述

给定一个连接的无向图,告诉它的最小生成树是否是唯一的。

定义1(生成树):考虑连接的无向图G =(V,E)。 G的生成树是G的子图,说T =(V‘,E‘),具有以下属性:
V‘= V.
T连接无环。

定义2(最小生成树):考虑边缘加权,连接,无向图G =(V,E)。 G的最小生成树T =(V,E‘)是总成本最小的生成树。总成本T表示E‘中所有边缘的权重之和。
输入

第一行包含单个整数t(1 <= t <= 20),测试用例数。每个案例都代表一个图表。它以包含两个整数n和m(1 <= n <= 100)的行开始,节点数和边数。以下m行中的每一行包含三(xi,yi,wi),表示xi和yi通过权重= wi的边连接。对于任何两个节点,最多连接一个边。
产量

对于每个输入,如果MST是唯一的,打印它的总成本,否则打印字符串“不唯一!”。
样品输入

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
样品输出

3
不是唯一!

 

 

注意多组数据初始化

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define maxn 1000000
int n,m,k,ans=maxn,cnt,tot,flag,fa[maxn],use[maxn],t;
struct Edge{
    int l,r,w;
}edge[maxn];

int find(int x) { return fa[x]==x?x:fa[x]=find(fa[x]); }
bool cmp(Edge a,Edge b) { return a.w<b.w; }

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        memset(use,0,sizeof(use));
        memset(fa,0,sizeof(fa));
        cnt=0,tot=0,ans=maxn;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) fa[i]=i;
        for(int i=1;i<=m;i++) scanf("%d%d%d",&edge[i].l,&edge[i].r,&edge[i].w);
        sort(edge+1,edge+m+1,cmp);
        for(int i=1;i<=m;i++)
        {
            int fx=find(edge[i].l),fy=find(edge[i].r);
            if(fx!=fy)
            {
                use[++cnt]=i;
                fa[fx]=fy;
                tot+=edge[i].w;
            }
            if(cnt==n-1) break;
        }
        for(int i=1;i<=cnt;i++)
        {
            for(int j=1;j<=n;j++) fa[j]=j;
            int cnt2=0,tot2=0;
            sort(edge+1,edge+1+m,cmp);
            for(int j=1;j<=m;j++)
            {
                if(j!=use[i])
                {
                    int fx=find(edge[j].l),fy=find(edge[j].r);
                    if(fx!=fy)
                    {
                        cnt2++;
                        tot2+=edge[j].w;
                        fa[fx]=fy;
                    }
                }
                if(cnt2==n-1) break;    
            }
            if(cnt2==n-1) ans=min(ans,tot2);
        }
        if(ans!=tot) printf("%d\n",tot);
        else printf("Not Unique!\n");
    }
    return 0;
}

 

 

以上是关于poj 1679-The Unique MST的主要内容,如果未能解决你的问题,请参考以下文章

「POJ1679 」The Unique MST

[2016-01-27][POJ][1679][The Unique MST]

POJ 1679 The Unique MST

POJ——T1679 The Unique MST

POJ1679 The Unique MST —— 次小生成树

POJ-1679 The Unique MST---判断最小生成树是否唯一