POJ1679 The Unique MST (judging whether the minimum spanning tree is unique)

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!

思路

有t组数据,有n个点和m条边,问最小生成树的个数是否唯一,如果唯一输出最小生成树的权值,不唯一输出Not Unique!. 先用kruskal求一下最小生成树,然后尝试删除最小生成树的每一条边,每次再有一下最小生成树,如果出现删去边后求出来的最小生成树的权值和已经知道的一样,那么最小生成树就不唯一

代码

#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <sstream>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#include<list>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define inf 0x3f3f3f3f
typedef long long ll;
const int N=100+10;
const int M=N*N;
int pre[N],n,m,vis[M];
struct edge
{
    int u,v,w;
} e[M];
vector<int>v;
vector<int>p;
void init()
{
    for(int i=1; i<=n; i++)
        pre[i]=i;
}
int find(int x)
{
    return x==pre[x]?x:pre[x]=find(pre[x]);
}
int mix(int x,int y)
{
    int fx=find(x),fy=find(y);
    if(fx!=fy)
    {
        pre[fy]=fx;
        return 1;
    }
    return 0;
}
bool cmp(edge a,edge b)
{
    return a.w<b.w;
}
int kruskal(int flag)
{
    init();
    int sum=0,cnt=0;
    for(int i=1; i<=m; i++)
    {
        if(vis[i])continue;
        if(mix(e[i].u,e[i].v))
        {
            if(flag)
                v.push_back(i);
            cnt++;
            sum+=e[i].w;
        }
        if(cnt==n-1)
            break;
    }
    if(cnt==n-1)
        return sum;
    return inf;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        v.clear();
        p.clear();
        mem(vis,0);
        scanf("%d%d",&n,&m);
        for(int i=1; i<=m; i++)
            scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
        sort(e+1,e+m+1,cmp);
        int ans=kruskal(1);
        for(int i=0; i<v.size(); i++)
        {
            vis[v[i]]=1;
            p.push_back(kruskal(0));
            vis[v[i]]=0;
        }
        sort(p.begin(),p.end());
        if(ans==p[0])
            puts("Not Unique!");
        else
            printf("%d\n",ans);
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325855587&siteId=291194637