The Unique MST(OpenJ_Bailian-1679)

Problem 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 条边,问所给的图中最小生成树是否唯一

思路:次小生成树

首先求出最小生成树与次小生成树,然后判断最小生成树与次小生成树是否相同,若相同则不唯一,若不同则唯一

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {-1,1,0,0};
const int dy[] = {0,0,-1,1};
using namespace std;

int n,m;
struct Node{
    int u,v,w;
    bool vis;
    bool operator <(const Node &rhs)const{
        return w<rhs.w;
    }
} node[N];
vector<int>G[110];
int father[110],maxDis[110][110];
int Find(int x){
    return x==father[x]?x:father[x]=Find(father[x]);
}
void Kruskal(){
    sort(node,node+m);
    for(int i=0; i<=n; i++){//初始化
        G[i].clear();
        G[i].push_back(i);
        father[i]=i;
    }

    int mst=0,k=0;//k为当前生成树中的点
    for(int i=0; i<m; i++){//枚举边
        if(k==n-1)//等于n-1个点
            break;

        int x=Find(node[i].u),y=Find(node[i].v);
        if(x!=y){
            k++;
            mst+=node[i].w;
            node[i].vis=true;//边已用过,标记


            int lenX=G[x].size();
            int lenY=G[y].size();
            for(int j=0; j<lenX; j++){//更新两点之间距离的最大值
                for(int k=0; k<lenY; k++){
                    maxDis[G[x][j]][G[y][k]]=node[i].w;//因为后面的边会越来越大,所以这里可以直接等于当前边的长度
                    maxDis[G[y][k]][G[x][j]]=node[i].w;
                }
            }
            father[x]=y;
            for(int j=0; j<lenX; j++)
                G[y].push_back(G[x][j]);
        }
    }

    int cimst=INF;//次小生成树权值
    for(int i=0; i<m; i++)
        if(!node[i].vis)
            cimst=min(cimst,mst+node[i].w-maxDis[node[i].u][node[i].v]);
    if(cimst>mst)
        printf("%d\n",mst);
    else
        printf("Not Unique!\n");
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        for(int i=0; i<m; i++){
            scanf("%d%d%d",&node[i].u,&node[i].v,&node[i].w);
            node[i].vis=false;
        }
        Kruskal();
    }
    return 0;
}

 

posted @ 2022-09-20 22:55  老程序员111  阅读(11)  评论(0)    收藏  举报