<题目链接>

题目大意:

给定一张无向图,判断其最小生成树是否唯一。

解题分析:

对图中每条边,扫描其它边,如果存在相同权值的边,则标记该边;用kruskal求出MST。

如果MST中无标记的边,则该MST唯一;否则,在MST中依次去掉标记的边,再求MST,若求得MST权值和原来的MST 权值相同,则MST不唯一。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int M = 1e4+;
struct EDGE{
int u,v,w;
int eq,used,del;
}edge[M];
int n,m;
int father[M];
bool flag;
bool cmp(EDGE a,EDGE b){
return a.w<b.w;
}
int find(int x){
if(father[x]!=x)father[x]=find(father[x]);
return father[x];
}
int Kruscal(){
for(int i=;i<1e4+;i++)father[i]=i;
int sum=,num=;
for(int i=;i<m;i++){
if(edge[i].del)continue;//如果该边被删除,则跳过
int u=edge[i].u,v=edge[i].v,w=edge[i].w;
if(find(u)!=find(v)){
sum+=w,num++;
if(flag)edge[i].used=;//将第一次最小生成树的所有边标记
father[find(v)]=find(u);
}
if(num>=n-)break;
}
return sum;
}
int main(){
int T;scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
memset(edge,,sizeof(edge)); //用memset能给edge内的所有变量初始化吗?
for(int i=;i<m;i++){
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
}
for(int i=;i<m;i++){
for(int j=;j<m;j++){
if(i==j)continue;
if(edge[i].w==edge[j].w)edge[i].eq=;//判断是否有和它权值相同的边
}
}
sort(edge,edge+m,cmp);
flag=true;
int cnt=Kruscal();
flag=false;
bool fp=false;
for(int i=;i<m;i++){
if(edge[i].eq&&edge[i].used){
edge[i].del=;//如果这条边在第一次求的最小生成树中,并且还有与这条边权值相同的边,那么就试着删除这条边,再跑一遍最小生成树,看得到的权值总和是否与第一次的最小生成树相同
int res=Kruscal();
if(res==cnt){
fp=true;
printf("Not Unique!\n");
break;
}
edge[i].del=;
}
}
if(!fp)printf("%d\n",cnt);
}
return ;
}

2018-10-01

POJ-1679 The Unique MST (判断最小生成树的唯一性)的更多相关文章

  1. poj 1679 The Unique MST 判断最小生成树是否唯一(图论)

    借用的是Kruskal的并查集,算法中的一点添加和改动. 通过判定其中有多少条可选的边,然后跟最小生成树所需边做比较,可选的边多于所选边,那么肯定方案不唯一. 如果不知道这个最小生成树的算法,还是先去 ...

  2. poj 1679 The Unique MST (判定最小生成树是否唯一)

    题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total S ...

  3. 【POJ 1679 The Unique MST】最小生成树

    无向连通图(无重边),判断最小生成树是否唯一,若唯一求边权和. 分析生成树的生成过程,只有一个圈内出现权值相同的边才会出现权值和相等但“异构”的生成树.(并不一定是最小生成树) 分析贪心策略求最小生成 ...

  4. POJ 1679 The Unique MST (最小生成树)

    The Unique MST 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/J Description Given a conn ...

  5. POJ 1679 The Unique MST(最小生成树)

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...

  6. POJ 1679 The Unique MST 【最小生成树/次小生成树模板】

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22668   Accepted: 8038 D ...

  7. POJ 1679 The Unique MST 推断最小生成树是否唯一

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22715   Accepted: 8055 D ...

  8. poj 1679 The Unique MST 【次小生成树】【模板】

    题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...

  9. poj 1679 The Unique MST(唯一的最小生成树)

    http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

随机推荐

  1. MVVM 简介

    转:https://objccn.io/issue-13-1/ 所以,MVVM 到底是什么?与其专注于说明 MVVM 的来历,不如让我们看一个典型的 iOS 是如何构建的,并从那里了解 MVVM: 我 ...

  2. nginx官方模块之http_sub_module

    作用 http内容替换 语法 示例 html代码与结果如下:

  3. 树形dp 入门

    今天学了树形dp,发现树形dp就是入门难一些,于是好心的我便立志要发一篇树形dp入门的博客了. 树形dp的概念什么的,相信大家都已经明白,这里就不再多说.直接上例题. 一.常规树形DP P1352 没 ...

  4. SpringBoot定时任务

    代码做定时任务:1.开个线程,线程里面休眠去做 2.使用一些定时任务的框架去做 1.创建TimerTest类 package com.cppdy.service; import org.springf ...

  5. Python学习【第2篇】:Python数据结构

    Python数据结构 1.数字类型 2.字符串 3.列表 4.元组 5.字典 6.集合

  6. 动态获取后台传过来的值作为select选项

    问题描述:点击左侧菜单项,进入对应的具体页面a.html,页面上方有个select框,点击框后,会浮现选择项. 解决思路:对左侧菜单项添加一个onclick事件,进入后台做具体的查询,将查询到的lis ...

  7. Idea 12配置SPring MVC 和Tomcat Server

    配置Spring 1. 添加idea插件 都选上了.也许有用! 2. 添加Spring库 下载spring,添加java库,指向spring库的目录: 配置tomcat Server 1. 安装tom ...

  8. 数组练习题A财务管理

    第一次看全英文的题,还是有点不舒服的感觉,还是用了翻译器 Larry graduated this year and finally has a job. He's making a lot of m ...

  9. VUE,页面跳转传多个参数

    <template> <a @click="goNext">Next</a> <input type="text" v ...

  10. 金蝶k3密码批量修改

    该字段含有单引号,直接使用查询语句,需要转义其中的单引号.select * from t_User where FSID = ') F ", ,P T #8 *P!D &D 80!N ...