洛谷 P4180 【模板】严格次小生成树

题目链接

https://www.luogu.org/problem/P4180

分析

根据Kruskal算法的思想,(非)严格次小生成树应该是来自最小生成树的;

具体来说,是将某条非树边加入到MST上,再删去产生的环里原树边中边权最大的一条;

枚举加入的非树边,再用倍增算法求要删去的树边;

但要求严格次小生成树,所以删去的树边不可以和插入的边权值相等,

这样维护树上路径最大值的同时还要维护次大值。

AC代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

inline int read() {
   
	int num = 0;
	char c = getchar();
	while (c < '0' || c > '9') c = getchar();
	while (c >= '0' && c <= '9')
		num = num * 10 + c - '0', c = getchar();
	return num;
}

const int maxn = 1e5 + 5, maxm = 3e5 + 5;

int head[maxn], eid;

struct Edge {
   
	int u, v, w, flag, next;

	bool operator < (const Edge& rhs) const {
   
		return w < rhs.w;
	}
} edge0[maxm], edge[2 * maxn];

inline void insert(int u, int v, int w) {
   
	edge[++eid].v = v;
	edge[eid].w = w;
	edge[eid].next = head[u];
	head[u] = eid;
}

int fa[maxn], depth[maxn], f[maxn]
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值