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

来源:互联网 发布:webpack 压缩混淆js 编辑:程序博客网 时间:2024/04/24 00:57
The Unique MST
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 17406 Accepted: 6030

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

23 31 2 12 3 23 1 34 41 2 22 3 23 4 24 1 2

Sample Output

3Not Unique!

Source

POJ Monthly--2004.06.27 srbga@POJ

判断最小生成树是否为一的方法:

1) 对图中每条边,扫描其他边,如果存在相同权值的边,则对该边作标记; 

2) 然后用 Kruskal 算法(或 Prim 算法)求 MST; 

3) 求得 MST 后,如果该 MST 中未包含作了标记的边,即可判定 MST 唯一;如果包含作了 标记的边,则依次去掉这些边再求 MST,如果求得的 MST 权值和原 MST 的权值相同,即可判定 MST 不唯一。 


代码:

#include <cstdio>#include <cstring>#include <vector>#include <algorithm>#define maxn 105using namespace std;int n,m,num,ans;int set[maxn],rank[maxn];bool vis[maxn*maxn];bool eq[maxn*maxn];struct Tnode{    int v1,v2,cost;} node[maxn*maxn];void init(){    int i;    for(i=1; i<=n; i++)    {        set[i]=i;        rank[i]=1;    }    num=n;}int find(int x)        // 查找时优化了路径{    int r=x,nn;    while(set[r]!=r) r=set[r];    while(x!=r)    {        nn=set[x];        set[x]=r;        x=nn;    }    return r;}void merge(int a,int b)    // 合并时将深度小的集合合并到大的里面{    int x,y;    x=find(a);    y=find(b);    if(x!=y)    {        if(rank[x]<rank[y])  set[x]=y;        else        {            set[y]=x;            if(rank[x]==rank[y])  rank[x]++;        }        num--;    }}bool cmp(const Tnode &xx1, const Tnode &xx2){    return  xx1.cost<xx2.cost;}void solve()    // 记录每条边是否有与之相同的权值{    int i,j,flag,ni,nx;    memset(eq,0,sizeof(eq));    for(i=1; i<=m; i++)    {        flag=0;        ni=i;        nx=node[i].cost;        i++;        if(i>m) break ;        while(node[i].cost==nx)        {            flag=1;            eq[i]=1;            i++;        }        if(flag) eq[ni]=1;        i--;    }}bool isunique()     // 判断最小生成树是否唯一{    int i,j,x1,x2,ni,flag,tans;    flag=1;    for(i=1; i<=m; i++)    {        if(vis[i]&&eq[i])        {            flag=0;            break ;        }    }    if(flag) return true ;    for(i=1; i<=m; i++)    {        if(vis[i]&&eq[i])  // 依次去掉每个点再求MST        {            ni=i;            tans=0;            init();      // 记得每次都要初始化            for(j=1; num>1&&j<=m; j++)            {                if(j==ni) continue ;                x1=find(node[j].v1);                x2=find(node[j].v2);                if(x1!=x2)                {                    merge(node[j].v1,node[j].v2);                    tans+=node[j].cost;                }            }            if(tans==ans) return false ;        }    }    return true ;}void kruskal(){    int i,j,x1,x2;    ans=0;    memset(vis,0,sizeof(vis));    for(i=1; num>1&&i<=m; i++)    {        x1=find(node[i].v1);        x2=find(node[i].v2);        if(x1!=x2)        {            merge(node[i].v1,node[i].v2);            vis[i]=1;            ans+=node[i].cost;        }    }}int main(){    int i,j,x1,x2,t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        init();        for(i=1; i<=m; i++)        {            scanf("%d%d%d",&node[i].v1,&node[i].v2,&node[i].cost);        }        sort(node+1,node+m+1,cmp);        solve();        kruskal();        if(isunique()) printf("%d\n",ans);        else printf("Not Unique!\n");    }    return 0;}


原创粉丝点击