判断最小生成树是否唯一

The Unique MST

 POJ - 1679 

 

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!

思路:先生成最小数,然后依次删掉节点生成树,判断权值是不是相等,如果相等就是不唯一,不相等就是唯一。

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
const int N = 109;
const int MAX = 100000000;
int dis[N];
int g[N][N];
int pre[N];
pair<int, int> v[N];
int prim(int n, int m, bool f)
{
    for(int i=0;i<n;i++)
    {
        dis[i] = g[0][i];
        pre[i] = 0;
    }
    dis[0] = -1;
    int ans = 0;
    for(int i=1;i<n;i++)
    {
        int min = MAX, mini = -1;
 
        for(int j=0;j<n;j++)
        {
            if(dis[j]!=-1 && dis[j]<min)
            {
                min = dis[j];
                mini = j;
            }
        }
        if(mini == -1)
            return -1;
        dis[mini] = -1;
        if(f)
        {
            v[i].first = mini;
            v[i].second = pre[mini];
        }
        ans += min;
        for(int j=0;j<n;j++)
            if(dis[j]!=-1 && dis[j] > g[mini][j])
            {
                pre[j] = mini;
                dis[j] = g[mini][j];
            }
    }
    return ans;
}
int main()
{
    int T;
    IOS;cin>>T;
    while(T--)
    {
        int n, m;
        memset(g, 0x3f, sizeof(g));
        cin >>n >>m;
        int a, b, c;
        for(int i=0;i<m;i++)
        {
            cin >>a >>b >>c;
            g[a-1][b-1] = g[b-1][a-1] = c;
        }
        int ans = prim(n, m, 1);
        for(int i=1;i<n;i++)
        {
            int t = g[v[i].first][v[i].second];
            g[v[i].first][v[i].second]= g[v[i].second][v[i].first] = MAX;
            if(ans == prim(n, m, 0))
            {
                ans = -1;
                break;
            }
            g[v[i].first][v[i].second]= g[v[i].second][v[i].first] = t;
        }
        if(ans == -1)
            cout<<"Not Unique!\n";
        else
            cout<<ans<<endl;
    }
    return 0;
}

 

全部评论

相关推荐

屌丝逆袭咸鱼计划:心态摆好,man,晚点找早点找到最后都是为了提升自己好进正职,努力提升自己才是最关键的😤难道说现在找不到找的太晚了就炸了可以鸡鸡了吗😤早实习晚实习不都是为了以后多积累,大四学长有的秋招进的也不妨碍有的春招进,人生就这样
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务