最小生成树的唯一性,部分参考了oi-wiki

如果一条不在最小生成树边集内的边,它可以替换一条在最小生成树边集内,且权值相等的边,那么最小生成树不是唯一的

同过kruskal来判断

考虑权值相等的边,记录有几条边是目前可以被选入的,和实际选入了几条边,如果不相同,则最小生成树不唯一

原因是如果出现这两个值不相等的情况,则一定出现了环,且这个环内至少有两条边权值相同

具体实现,用一个\(\text{tail}\)指针指向当前权值的最后一条边,当\(\text{i}>\text{tail}\)(也就是当前边权的边全部被考虑完)的时候,就去判断上述的两个值是否相等

注意kruskal循环的时候要循环到\(m+1\),这样如果权值最大的一部分边要被选到的话,会在\(i=m+1\)的时候进行判断

而且在判断当前选的边数已经等于\(n-1\),不能直接跳出,要等到\(\text{i}>\text{tail}\)判断那两个值是否相等,如果相等再跳出,否则也是不唯一

 

当然网上也有那种每次删边跑kruskal的做法,但复杂度明显没有这个优秀

模板题

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstring>
#define reg register
#define EN std::puts("")
#define LL long long
inline int read(){
int x=0,y=1;
char c=std::getchar();
while(c<'0'||c>'9'){if(c=='-') y=0;c=std::getchar();}
while(c>='0'&&c<='9'){x=x*10+(c^48);c=std::getchar();}
return y?x:-x;
}
int n,m;
struct data{
int from,to,w;
}e[10006];
int fa[10006];
inline int cmp(data aa,data aaa){return aa.w<aaa.w;}
inline int find(int k){return k==fa[k]?k:fa[k]=find(fa[k]);}
int main(){int t=read();while(t--){
n=read();m=read();
for(reg int i=1;i<=n;i++) fa[i]=i;
for(reg int i=1;i<=m;i++){
e[i].from=read();e[i].to=read();e[i].w=read();
}
std::sort(e+1,e+1+m,cmp);
int ans=0,cnt=1,tail=0;
int choose=0,sum=0;
for(reg int i=1;i<=m+1;i++){
if(i>tail){
// std::printf("%d %d\n",choose,sum);
if(choose!=sum) goto NOT;
if(cnt>=n) break;
sum=choose=0;
for(tail++;e[tail].w==e[i].w&&tail<=m;tail++)
if(find(e[tail].from)!=find(e[tail].to)) sum++;//如果这条边目前能被加进去,sum才+1
tail--;
}
int from=find(e[i].from),to=find(e[i].to);
if(from==to) continue;
cnt++;fa[from]=to;
if(cnt<=n) ans+=e[i].w,choose++;;
}
std::printf("%d\n",ans);continue;
NOT:;std::puts("Not Unique!");
}
return 0;
}

poj1679 The Unique MST(最小生成树唯一性)的更多相关文章

  1. [poj1679]The Unique MST(最小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28207   Accepted: 10073 ...

  2. POJ1679 The Unique MST(Kruskal)(最小生成树的唯一性)

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

  3. POJ-1679 The Unique MST(次小生成树、判断最小生成树是否唯一)

    http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its minimum s ...

  4. POJ1679 The Unique MST[次小生成树]

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28673   Accepted: 10239 ...

  5. POJ1679 The Unique MST 【次小生成树】

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

  6. POJ1679 The Unique MST 2017-04-15 23:34 29人阅读 评论(0) 收藏

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29902   Accepted: 10697 ...

  7. POJ1679 The Unique MST —— 次小生成树

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

  8. POJ-1679 The Unique MST,次小生成树模板题

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K       Description Given a connected undirec ...

  9. poj1679 The Unique MST(判定次小生成树)

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

  10. POJ-1679.The Unique MST.(Prim求次小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39561   Accepted: 14444 ...

随机推荐

  1. 【物理】AABB物理碰撞检测

    什么是AABB? AABB,指轴对齐包围盒(Axis-aligned bounding boxes).在3D空间中,AABB是一个长方体,在2D空间中是一个长方形.特征是面法线皆平行于坐标轴,即当物体 ...

  2. javascript 入门 之 bootstrap 第一个程序

    <table data-toggle="table"> <thead> <tr> <th>Item ID</th> &l ...

  3. 【django基础】django接口 异步ajax请求 导出数据库成excel表(包裹前端后端)

    py文件: from django.utils.http import urlquote from rest_framework.views import APIView from django.sh ...

  4. PHPStorm IDE 快捷键

    ⌘——Command ⌃ ——Control ⌥——Option/Alt ⇧——Shift ⇪——Caps Lock fn——功能键就是fn编辑 Command+alt+T 用 (if..else, ...

  5. template_showpost

    使用<a href='...'>name<\a>实现点击"name"与转向'...'网址的超链接操作 from django.shortcut import ...

  6. hive常用函数四

    字符串函数 1. 字符串长度函数:length 语法: length(string A) 返回值: int 说明:返回字符串A的长度 举例: hive> select length('abced ...

  7. 条件变量 condition_variable wait_for

    wait_for(阻塞当前线程,直到条件变量被唤醒,或到指定时限时长后) #include <iostream> #include <atomic> #include < ...

  8. 【Tool】Windows系统安装Maven依赖管理工具

    安装Maven依赖管理工具 官网下载地址:http://maven.apache.org/download.cgi 系统环境要求: [JDK]Maven3.3版本+需要JDK1.7版本以上支持 [内存 ...

  9. Spring 下,关于动态数据源的事务问题的探讨

    开心一刻 毒蛇和蟒蛇在讨论谁的捕猎方式最高效. 毒蛇:我只需要咬对方一口,一段时间内它就会逐渐丧失行动能力,最后死亡. 蟒蛇冷笑:那还得等生效时间,我只需要缠住对方,就能立刻致它于死地. 毒蛇大怒:你 ...

  10. ST表(求解静态RMQ问题)

    例题:https://www.acwing.com/problem/content/1272/ ST表类似于dp. 定义st[i][j]表示以i为起点,长度位2^j的一段区间,即[ i , i + 2 ...