POJ 1679 The Unique MST(次小生成树)

版权声明:随意转载,转载请注明出处。 https://blog.csdn.net/qq_36258516/article/details/81976540

POJ 1679 The Unique MST(次小生成树)

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 36126 Accepted: 13182

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!

题意

  给出一个图,判断该图的最小生成树是否唯一,如果是就输出值,如果不是就输出Not Unique!。

解题思路

  看着题目的数据很小,我就直接用了最暴力的解法,先用kruskal跑一遍最小生成树,同时记录下用到的边,然后再依次枚举这些边,再除去他们跑最小生成树,最后把得到的最小值与第一次的值相比较,如果一样就说明最小生成树不唯一。

  这个题的数据是真的很水,我这样写都是0ms过的。。

代码

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn = 1e4+50;

struct node
{
    int x,y,val;
} arr[maxn];
int flag[maxn],p[maxn];
int n,m,t;

int finds(int x)
{
    return p[x]==x?x:p[x]=finds(p[x]);
}
int kruskal(int pos)
{
    for(int i=1; i<=n; i++) p[i]=i;
    int cnt=0,ans=0;
    for(int i=0; i<m; i++)
    {
        if(i==pos) continue;
        int xx=finds(arr[i].x);
        int yy=finds(arr[i].y);
        if(xx!=yy)
        {
            p[xx]=yy;
            ans+=arr[i].val;
            if(pos==-1) flag[i]=1;
            cnt++;
        }
        if(cnt==n-1) return ans;
    }
    return -1;
}
int cmp(node a,node b)
{
    return a.val<b.val;
}
int main()
{
//    freopen("in.txt","r",stdin);
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0; i<m; i++) scanf("%d%d%d",&arr[i].x,&arr[i].y,&arr[i].val);
        sort(arr,arr+m,cmp);
        memset(flag,0,sizeof flag);
        int ans=kruskal(-1);
        if(ans==-1)
        {
            printf("0\n");
            continue;
        }
        int minn=0x3f3f3f3f;
        for(int i=0; i<m; i++) if(flag[i]) minn=min(minn,kruskal(i));
        if(minn==ans) printf("Not Unique!\n");
        else printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36258516/article/details/81976540