POJ--1679--The Unique MST【kruskal判断MST是否唯一】

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

题意:告诉你有n个点,m条边,以及m条边的信息(起点、终点、权值),判断最小生成树是否唯一

判断MST是否唯一的思路是这样:对于每条边如果有和他相等权值的边,则做一个标记,然后进行一遍kruskal或prim找出最小生成树权值,然后对于每个使用过并且有相等边标记的边,把它从图中删去,再进行一遍kruskal或prim,如果此时最小生成树权值和第一次一样,则说明最小生成树不唯一,否则最小生成树唯一。

#include<cstring>
#include<string>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cctype>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<ctime>
#include<cstdlib>
#include<functional>
#include<cmath>
using namespace std;
#define PI acos(-1.0)
#define MAXN 110000
#define eps 1e-7
#define INF 0x7FFFFFFF
#define seed 131
#define ll long long
#define ull unsigned ll
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

struct node{
    int u,v,dis;
    int used,equa,del;
}edge[MAXN];
int father[105],vis[105];
int n,m,flag,ans;
bool cmp(node x,node y){
    return x.dis<y.dis;
}
int find(int x){
    int t = x;
    while(father[t]!=t)
        t = father[t];
    int k = x;
    while(k!=t){
        int temp = father[k];
        father[k] = t;
        k = temp;
    }
    return t;
}
int kruskal(){
    int i,j=0;
    int sum = 0;
    for(i=1;i<=n;i++)   father[i] = i;
    for(i=0;i<m;i++){
        if(edge[i].del)     continue;
        int a = find(edge[i].u);
        int b = find(edge[i].v);
        if(a!=b){
            father[a] = b;
            sum += edge[i].dis;
            j++;
            if(flag)    edge[i].used = 1;
            if(j>=n-1)  break;
        }
    }
    return sum;
}
int main(){
    int t,i,j;
    scanf("%d",&t);
    while(t--){
        flag = 1;
        scanf("%d%d",&n,&m);
        for(i=0;i<m;i++){
            scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].dis);
            edge[i].used = edge[i].equa = edge[i].del = 0;
        }
        for(i=0;i<m;i++){
            for(j=0;j<m;j++){
                if(i==j)    continue;
                if(edge[i].dis == edge[j].dis)  edge[i].equa = 1;
            }
        }
        sort(edge,edge+m,cmp);
        int ans1 = kruskal();
        flag = 0;
        for(i=0;i<m;i++){
            if(edge[i].used&&edge[i].equa){
                edge[i].del = 1;
                int ans2 = kruskal();
                if(ans2==ans1){
                    printf("Not Unique!\n");
                    break;
                }
            }
        }
        if(i>=m)    printf("%d\n",ans1);
    }
    return 0;
}

POJ--1679--The Unique MST【kruskal判断MST是否唯一】

时间: 2024-11-29 08:31:06

POJ--1679--The Unique MST【kruskal判断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, undire

poj 1679 The Unique MST (次小生成树)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20293   Accepted: 7124 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 (次小生成树)

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(求最小生成树是否唯一)

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 【次小生成树】【模板】

题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后添加到最小生成树上,然后把原树上添加了之后形成环的最长的边删去,知道一个最小的.就是次小生成树. 这些需要的都可以在求解最小生成树的时候处理出来. AC代码: #include <cstdio> #include <cstring> #include <iostream> #i

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

The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21646 Accepted: 7661 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 (判断最小生成树是否唯一)

链接:poj 1679 题意:判断最小生成树是否唯一, 若唯一,输出最小权值和,否则,输出  Not Unique! 判断最小生成树是否唯一的思路: 1.对图中的每一条边,扫描其他边,如果存在相同权值的边,则对该边做标记 2.然后用Kruskal算法或Prim算法求MST 3.求得MST后,如果该MST中未包含做了标记的边,即可判断MST唯一: 如果包含做了标记的边,则依次去掉这些边的一条边,再求MST, 如果求得的MST权值和原来的MST的权值一样,即可判断MST不唯一. 个人思路是先求最小生

POJ 1679 The Unique MST 判断最小生成树是否唯一/次小生成树

题目链接: 1679 题意: 给出 M个点N条边 求它的的最小生成树 不唯一则输出:Not Unique! 题解: prim:判断"最小生成树是否唯一"可以理解为"最小生成树和次小生成树是否相等" 求次小生成树的步骤如下 1)先求出最小生成树T,在prim的同时,用一个矩阵maxx[u][v]记录在树中连接u-v的路径中权值最大的边. 2)枚举所有不在T中的边map[u][v],加入边u-v,删除权值为maxx[u][v]的边; 3)找到MST-maxx[u][v]

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

题目链接: http://poj.org/problem?id=1679 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