P4374 [USACO18OPEN]Disruption P (树链剖分)

题目链接: P4374 [USACO18OPEN]Disruption P

大致题意

给定一棵有 n n n个节点的树(有 n − 1 n - 1 n1条边), 再给出 m m m条额外边.

问: 当删除第 i i i条原树中的边时, 为使得树仍然联通, 需要补充的最小的额外边的边权是多少. (若不存在则输出-1)

解题思路

思维

我们考虑到, 由于原树中有 n − 1 n - 1 n1条边, 那么我每条额外边的加入, 都会在原树中形成一个环.

对于一个环而言, 我删掉环中的任意一条边, 环内的点必然仍然连通.

因此相当于, 第 i i i条连接 a , b a, b a,b两点的额外边, 可以保证 a , b a, b a,b两点路径上的任意一条边被删除时, 树仍连通.


树链剖分

我们可以把边权化为点权, 用线段树维护每个点上额外边的最小值, 树剖去修改和查询每一条路径即可.

AC代码

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 1; i <= (n); ++i)
using namespace std;
typedef long long ll;
const int N = 5E4 + 10, INF = 0x3f3f3f3f;
vector<int> edge[N];
struct EDGE { int a, b, c; } EG[N]; //存原树边

int p[N], dep[N], sz[N], son[N];
// 父节点   深度   节点大小 重儿子
void dfs1(int x = 1, int fa = 0) { // x = 树根节点
	p[x] = fa, dep[x] = dep[fa] + 1, sz[x] = 1; // son[x] = 0;
	for (auto& to : edge[x]) {
		if (to == fa) continue;
		dfs1(to, x);
		sz[x] += sz[to];		// 特别的, 如果边权->点权, 应记录w[to] = 边权.
		if (sz[to] > sz[son[x]]) son[x] = to; //更新重儿子
	}
}
int id[N], nw[N], top[N], ind;
//  新编号  新值    重链顶 当前用到的编号
void dfs2(int x = 1, int tp = 1) { // x = 树根节点, tp = 树根节点
	id[x] = ++ind, top[x] = tp;

	if (!son[x]) return; //叶子结点
	dfs2(son[x], tp); //先遍历重儿子

	for (auto& to : edge[x]) {
		if (to == p[x] or to == son[x]) continue;
		dfs2(to, to);
	}
}

struct node {
	int l, r;
	int fmin; // INF
	int lazy; // INF
}t[N << 2];
void pushdown(node& op, int lazy) {
	op.fmin = min(op.fmin, lazy);
	op.lazy = min(op.lazy, lazy);
}
void pushdown(int x) {
	if (t[x].lazy == INF) return;
	pushdown(t[x << 1], t[x].lazy), pushdown(t[x << 1 | 1], t[x].lazy);
	t[x].lazy = INF;
}
void pushup(int x) { t[x].fmin = min(t[x << 1].fmin, t[x << 1 | 1].fmin); }

void build(int l, int r, int x = 1) {
	t[x] = { l, r, INF, INF };
	if (l == r) return;
	int mid = l + r >> 1;
	build(l, mid, x << 1), build(mid + 1, r, x << 1 | 1);
}

void modify(int l, int r, int c, int x = 1) {
	if (l <= t[x].l and r >= t[x].r) {
		pushdown(t[x], c);
		return;
	}
	pushdown(x);
	int mid = t[x].l + t[x].r >> 1;
	if (l <= mid) modify(l, r, c, x << 1);
	if (r > mid) modify(l, r, c, x << 1 | 1);
	pushup(x);
}

int ask(int l, int r, int x = 1) {
	if (l <= t[x].l and r >= t[x].r) return t[x].fmin;
	pushdown(x);
	int mid = t[x].l + t[x].r >> 1;
	int res = INF;
	if (l <= mid) res = ask(l, r, x << 1);
	if (r > mid) res = min(res, ask(l, r, x << 1 | 1));
	return res;
}


void modify_route(int a, int b, int c) {
	while (top[a] != top[b]) {
		if (dep[top[a]] < dep[top[b]]) swap(a, b);
		modify(id[top[a]], id[a], c);
		a = p[top[a]];
	}
	if (a == b) return;
	if (dep[a] > dep[b]) swap(a, b);
	modify(id[a] + 1, id[b], c);
}

int ask_route(int a, int b) {
	int res = INF;
	while (top[a] != top[b]) {
		if (dep[top[a]] < dep[top[b]]) swap(a, b);
		res = min(res, ask(id[top[a]], id[a]));
		a = p[top[a]];
	}
	if (a == b) return res;
	if (dep[a] > dep[b]) swap(a, b);
	res = min(res, ask(id[a] + 1, id[b]));
	return res;
}
int main()
{
	int n, m; cin >> n >> m;
	rep(i, n - 1) {
		int a, b; scanf("%d %d", &a, &b);
		EG[i] = { a, b };
		edge[a].push_back(b), edge[b].push_back(a);
	}

	dfs1(), dfs2();
	build(1, n);

	rep(i, m) {
		int a, b, c; scanf("%d %d %d", &a, &b, &c);
		modify_route(a, b, c);
	}

	rep(i, n - 1) {
		const auto& [a, b, c] = EG[i];
		int res = ask_route(a, b);
		printf("%d\n", res == INF ? -1 : res);
	}

	return 0;
}

END

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逍遥Fau

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值