首页 > 代码库 > poj 1679 The Unique MST (判断最小生成树是否唯一)

poj 1679 The Unique MST (判断最小生成树是否唯一)

The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 20679 Accepted: 7255

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!

Source

POJ Monthly--2004.06.27 srbga@POJ

判断最小生成树是否唯一:

1、对图中每条边,扫描其它边,如果存在相同权值的边,则标记该边。

2、用kruskal或prim求出MST。

3、如果MST中无标记的边,则MST唯一;否则,在MST中依次去掉标记的边,再求MST,若求得MST权值和原来的MST 权值相同,则MST不唯一。


#include"stdio.h"
#include"string.h"
#include"iostream"
#include"algorithm"
using namespace std;
#define N 105
const int inf=0x7fffffff;
struct node
{
    int u,v,w;
    int eq,used,del;
}e[N*N];
int n,first,m;
int pre[N];
bool cmp(node a,node b)
{
    return a.w<b.w;
}
int find(int x)
{
    if(x!=pre[x])
        pre[x]=find(pre[x]);
    return pre[x];
}
int kruskal()
{
    int i,f1,f2,ans,cnt;
    ans=cnt=0;
    for(i=1;i<=n;i++)
        pre[i]=i;
    for(i=0;i<m;i++)
    {
        if(e[i].del)
            continue;
        f1=find(e[i].u);
        f2=find(e[i].v);
        if(f1!=f2)
        {
            if(first)
                e[i].used=1;
            pre[f1]=f2;
            ans+=e[i].w;
            cnt++;
            if(cnt>=n-1)
                break;
        }
    }
    return ans;
}
int main()
{
    int i,j,T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
            e[i].del=e[i].eq=e[i].used=0;
        }
        sort(e,e+m,cmp);
        for(i=0;i<m;i++)
        {
            for(j=i+1;j<m;j++)
            {
                if(e[i].w==e[j].w)
                {
                    e[i].eq=e[j].eq=1;
                }
                else
                    break;
            }
        }
        first=1;
        int ans=kruskal();
        first=0;
        for(i=0;i<m;i++)
        {
            if(e[i].used&&e[i].eq)
            {
                e[i].del=1;
                if(kruskal()==ans)
                    break;
                e[i].del=0;
            }
        }
        if(i<m)
            printf("Not Unique!\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}