The Unique MST(次小生成树)

时间:2023-02-23 17:35:28

The Unique MST

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<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=10010;
int t,n,m,father[110],b[110][110],c[110][110];
bool flag[maxn],in[110][110];
struct node
{
    int x;
    int y;
    int w;
    bool operator < (node tmp)const
    {
        return w<tmp.w;
    }
}a[maxn];
int find(int x)
{
    if(x!=father[x])
    father[x]=find(father[x]);
    return father[x];
}
int kruskal()
{
    memset(flag,0,sizeof(flag));
    memset(in,0,sizeof(in));
    int sum=0,v=0;sort(a+1,a+m+1);
    for(int i=1;i<=n;i++)
    father[i]=i;
    for(int i=1;i<=m;i++)
    {
        int f1=find(a[i].x);
        int f2=find(a[i].y);
        if(f1!=f2)
        {
            father[f2]=f1;
            sum++;flag[i]=1;
            in[a[i].x][a[i].y]=in[a[i].y][a[i].x]=1;
            v+=a[i].w;
        }
        if(sum==n-1) break;
    }
    return v;
}
void dfs(int star,int now,int from,int maxx)
{
    b[star][now]=maxx;
    for(int i=1;i<=n;i++)
    {
        if(!in[now][i]) continue;
        if(i==from) continue;
        dfs(star,i,now,max(maxx,c[now][i]));
    }
}
void prepare()
{
    for(int i=1;i<=n;i++)
    dfs(i,i,i,0);
}
int main()
{
    int x,y,z;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(a,0,sizeof(a));
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            a[i].x=x,a[i].y=y,a[i].w=z;
            c[x][y]=c[y][x]=z;
        }
        int minn=kruskal(),ans=0x7fffffff;
        prepare();
        for(int i=1;i<=m;i++)
        {
            if(flag[i]) continue;
            int u=a[i].x,v=a[i].y;
            ans=min(ans,minn+a[i].w-b[u][v]);
        }
        if(ans==minn) printf("Not Unique!\n");
        else printf("%d\n",minn);
    }
    return 0;
}