判断最小生成树是否唯一

http://poj.org/problem?id=1679

The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 20487 Accepted: 7207

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!
程序:

#include"stdio.h"
#include"string.h"
#include"stack"
#include"math.h"
#include"algorithm"
#include"stdlib.h"
#define M 222
#define inf 100000000
#define eps 1e-12
using namespace std;
struct node
{
    int u,v,w;
}e[M*M];
int cmp(node a,node b)
{
    return a.w<b.w;
}
int f[M];
int finde(int x)
{
    if(x!=f[x])
        f[x]=finde(f[x]);
    return f[x];
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m,i,j;
        scanf("%d%d",&n,&m);
        for(i=0;i<m;i++)
            scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
        sort(e,e+m,cmp);
        int num=0;
        int fuck=0;
        int sum=0;
        for(i=1;i<=n;i++)
            f[i]=i;
        for(i=0;i<m;)
        {
            j=i;
            while(e[i].w==e[j].w&&j<m)
            {
                int x=finde(e[j].u);
                int y=finde(e[j].v);
                if(x!=y)
                    fuck++;
                j++;
            }
            j=i;
            while(e[i].w==e[j].w&&j<m)
            {
                int x=finde(e[j].u);
                int y=finde(e[j].v);
                if(x!=y)
                {
                    f[x]=y;
                    sum+=e[i].w;
                    num++;
                }
                j++;
            }
            i=j;
            if(num==n-1)
                break;
        }
        if(fuck>num)
            printf("Not Unique!\n");
        else
            printf("%d\n",sum);
        //printf("%d %d\n",fuck,num);
    }
}


### 最小生成树唯一性 #### 权重不同的情况 当带权无向连通图 \( G \) 中每条边的权重都不相同时,最小生成树(MST)是唯一的。这是因为,在这种情况下,每次选择最短路径时都只有一种可能的选择[^2]。 #### 连通分量的影响 对于给定的 \( n \) 个点和 \( m \) 条边的情况,如果该图不是完全连通的,则会存在多个连通分量。此时无法形成单一的最小生成树;相反,每个连通分量内部会有自己的最小生成树。因此,整个图形不存在全局意义上的“唯一”的最小生成树[^1]。 #### 边数量等于顶点数减一的情形 若一个图恰好拥有 \( |V| - 1 \) 条边,并且这些边能够连接所有的顶点而不形成环路,则此图为一棵树结构,其本身就是唯一最小生成树。 #### 同样权重多条边的存在 然而,当存在两条或多条具有相同权重的不同边时,可能会有不同的组合方式来构建同样总权重下的最小生成树。这意味着即使总的最小生成树权重保持不变,具体的树形态也可能有所不同[^5]。 ```python def is_unique_mst(graph_edges, vertices_count): """ 判断给定图是否存在唯一最小生成树 参数: graph_edges (list of tuples): 图中所有边及其权重 [(u,v,w),...] vertices_count (int): 节点数目 返回: bool: True 表示 MST 是唯一的; False 表示 不唯一 或者 存在多个连通块. """ # 实现判断逻辑... pass ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值