K - The Unique MST

K - The Unique MST

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct nond{
    int x,y,z;
}edge[101*101];
int T,N,M,x,y,z,fa[1000],num,ans[1000];
int tot,bns,k,answer=9999999;
int cmp(nond aa,nond bb){
    return aa.z<bb.z;
}
int find(int x){
    return fa[x]==x?x:fa[x]=find(fa[x]);
}
int main(){
    cin>>T;
    while(T--){
        cin>>N>>M;
        tot=0;bns=0;
        answer=9999999;
        memset(fa,0,sizeof(fa));
        memset(ans,0,sizeof(ans));
        for(int i=1;i<=M;i++){
            cin>>x>>y>>z;
            edge[i].x=x;
            edge[i].y=y;
            edge[i].z=z;
        }
        sort(edge+1,edge+1+M,cmp);
        for(int i=1;i<=N;i++)    fa[i]=i;
        for(int i=1;i<=M;i++){
            int dx=find(edge[i].x),dy=find(edge[i].y);
            if(dx!=dy){
                fa[dx]=dy;
                tot++;
                ans[tot]=i;
                bns+=edge[i].z;
            }
            if(tot==N-1)    break;
        }
        for(int i=1;i<=tot;i++){
            k=0;num=0;
            for(int j=1;j<=N;j++)    fa[j]=j;
            sort(edge+1,edge+1+M,cmp);
            for(int j=1;j<=M;j++){
                if(j==ans[i])    continue;
                int dx=find(edge[j].x),dy=find(edge[j].y);
                if(dx!=dy){
                    fa[dx]=dy;
                    num++;
                    k+=edge[j].z;
                }
                if(num==N-1)    break;
            }
            if(num==N-1) answer=min(k,answer);
        }
        if(answer==bns)    cout<<"Not Unique!"<<endl;
        else    cout<<bns<<endl;
    }
}

原文地址:https://www.cnblogs.com/cangT-Tlan/p/8463217.html

时间: 2024-10-13 00:55:57

K - The Unique MST的相关文章

[kuangbin带你飞]专题六 最小生成树 K - The Unique MST (判断最小生成树是否唯一)

K - The Unique MST 题目链接:https://vjudge.net/contest/66965#problem/K 题目: 给定连接的无向图,告诉它的最小生成树是否唯一. 定义1(生成树):考虑连通的无向图G =(V,E). G的生成树是G的子图,比如T =(V',E'),具有以下属性:    1. V'= V.    2.T是连接的和非循环的. 定义2(最小生成树):考虑边加权,连通,无向图G =(V,E). G的最小生成树T =(V,E')是总成本最小的生成树. T的总成本

K - The Unique MST - poj 1679

题目的意思已经说明了一切,次小生成树... ************************************************************************************ #include<algorithm>#include<stdio.h>#include<string.h>#include<queue>using namespace std; const int maxn = 105;const int oo 

POJ 1679 The Unique MST(次短生成树)

Language: Default The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29098   Accepted: 10404 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider

POJ - 1679 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 followin

POJ 1679 The Unique MST【MST是否唯一,Prime算法,最好的代码】

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27389   Accepted: 9816 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ1679 The Unique MST【Kruskal】【次小生成树】

The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21304 Accepted: 7537 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undirected

POJ 1679 The Unique MST(求最小生成树是否唯一)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20430   Accepted: 7186 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ 1679 The Unique MST

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29079   Accepted: 10398 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undir

The Unique MST (判断是否存在多个最小生成树)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24058   Accepted: 8546 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire