The Unique MST(次小生成树问题)

A - The Unique MST

Time Limit:1000MS    Memory Limit:10000KB    64bit IO Format:%I64d & %I64u
Submit Status

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!

题意: 题目给了一个无向联通图,求图的最小生成树是不是唯一的.

题解:
        次小生成树的定义: 对于一个无向联通图G,T是图G的一颗最小生成树,如果存在另外一个生成树T',使得对于除T外的所有生成树T'',都有 w(T'') >= w(T'),则T'为次小生成树.
因为图为联通图,所以易知次小生成树为最小生成树改变任意一条边得到. 所以由这样一个定理可以得出次小生成树的解法. 
        一.用prim算法求得一个最小生成树,并维护树内任意两点的最大权值maxn[u][v];
       二.枚举图G的所有剩余边,更新维护次小生成树.
       三.具体看下代码;

AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <cmath>
#include <cctype>

using namespace std;
typedef long long ll;
const int M = 110;
const int INF = 0xffffff;
int d[M],vis[M],Stack[M],fa[M];
int mp[M][M],maxn[M][M],n,m;

int prim()
{
    int to,ans;
    to = ans = 0;
    Stack[to++] = 1;
    vis[1] = 1;
    for(int i = 1; i <= n; i++)
        if(mp[1][i] != INF) fa[i] = 1,d[i] = mp[1][i];
    while(true)
    {
        int Min = INF,next = 1;
        for(int i = 1; i <= n; i++)
            if(!vis[i] && Min > d[i]) Min = d[next = i];
        if(Min == INF) break;
        vis[next] = 1;
        for(int i = 1; i <= n; i++)
            if(!vis[i] && mp[next][i] < d[i])
                d[i] = mp[next][i],fa[i] = next;
        for(int i = 0; i < to; i++) //维护最小生成树中任意两点的最大的边权值
        {
            int u = Stack[i];
            int v = fa[next];
            //最大边权值存在于新加入的边或者之前的边
            maxn[u][next] = maxn[next][u] = max(Min,maxn[u][v]);
        }
        Stack[to++] = next; //节点入栈
        ans += Min;
    }
    return ans;
}

void unit()
{
    memset(maxn,0,sizeof(maxn)); //最小生成树中任意两点的最大的边权值
    memset(vis,0,sizeof(vis)); //标记节点
    for(int i = 1; i <= n; i++) d[i] = INF; //表示虚拟节点到i节点的最小距离,初始化为最大值
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            mp[i][j] = INF;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d",&n,&m);
        unit();
        while(m--)
        {
            int a,b,c;
            scanf("%d %d %d",&a,&b,&c);
            mp[a][b] = mp[b][a] = c;//建图
        }
        int ans = prim(),Min = INF;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                if(i != j && fa[i] != j && fa[j] != i && mp[i][j] != INF)
                {
                    if(Min > mp[i][j] - maxn[i][j])
                        Min = mp[i][j] - maxn[i][j];
                }
            }
        }
        if(Min) printf("%d\n",ans);
        else puts("Not Unique!");
    }
    return 0;
}


你可能感兴趣的:(次小生成树)