1. 程式人生 > >1679(次小生成樹)

1679(次小生成樹)

The Unique MST

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!

題意:給n點m邊,求最小生成樹是否唯一,無重邊。

思路:求次小生成樹,如果和最小生成樹結果一樣則不唯一。

次小生成樹:次小生成樹由最小生成樹變化而來,通過最小生成樹概念可以知道“次小”只需要通過變化最小生成樹上的一條邊實現,並且要使得這種變化是最小。最小生成樹上的不相鄰的兩點相連必定成成為一個環,所以我們可以嘗試列舉這些不相鄰的點使他們相連,再刪除環中屬於最小生成樹的最大邊(令當前被確定的點為u,已經被確定的點為v,則u<->v路徑中最大的邊要麼來自father[u]<->v路徑中的最大,要麼就是當前被確定的邊dis[u],dp的思想),這樣既保證樹的結構又能使樹的變化最小。這些列舉中最小的結果即為次小生成樹。

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <functional>
#define inf 0X3f3f3f3f
using namespace std;
typedef long long ll;
const int MAXN=1e5+10;
const int MAX=1000+10;
const double eps=1e-6;

int n,m;
bool vis[MAX],book[MAX][MAX];
int mapp[MAX][MAX];
int dis[MAX];
int pre[MAX];
int maxx[MAX][MAX];

void init(){
    for(int i=0;i<=n;i++){
        for(int j=0;j<=n;j++){
            if(i==j)
                mapp[i][j]=0;
            else
                mapp[i][j]=inf;
        }
    }
    memset(vis,0,sizeof(vis));
    memset(book,0,sizeof(book));
    memset(maxx,0,sizeof(mapp));    
}

int prime(int s){
    int sum=0;
    vis[s]=1;
    pre[s]=-1;
    dis[s]=0;
    for(int i=2;i<=n;i++){
        dis[i]=mapp[s][i];
        pre[i]=s;
    }
    for(int k=1;k<n;k++){
        int minn=inf,t;
        for(int i=1;i<=n;i++){
            if(!vis[i]&&minn>dis[i]){
                minn=dis[i];
                t=i;
            }
        }
        if(minn==inf)   
            return -1;
        sum+=minn;
        vis[t]=1;
        book[pre[t]][t]=book[t][pre[t]]=1;
        for(int i=1;i<=n;i++){
            if(vis[i]){
                maxx[t][i]=maxx[i][t]=max(maxx[pre[t]][i],dis[t]);
            }
            if(!vis[i]&&dis[i]>mapp[t][i]){
                dis[i]=mapp[t][i];
                pre[i]=t;
            }
        }
    }
    return sum;
}

int sec_mst(int sum){
    int ans=inf;
    for(int i=1;i<=n;i++){
        for(int j=i+1;j<=n;j++){
            if(mapp[i][j]!=inf&&!book[i][j]){
                ans=min(ans,sum-maxx[i][j]+mapp[i][j]);
            }
        }
    }
    if(ans==inf)
        return -1;
    else
        return ans;
}

int main(){
    #ifdef ONLINE_JUDGE
    #else
    freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    #endif
    int T;
    cin>>T;
    while(T--){
        cin>>n>>m;
        //cout<<n<<" "<<m<<endl;
        init();
        for(int i=1;i<=m;i++){
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            mapp[u][v]=mapp[v][u]=w;
        }
        int flag=prime(1);
        if(flag==-1){
            cout<<"Not Unique!"<<endl;
            continue;
        }
        if(flag==sec_mst(flag))
            cout<<"Not Unique!"<<endl;
        else
            cout<<flag<<endl;
    }
    return 0;
}