跳到正文

题解

题解:P17382 [PacNW 2025] Farthest City

OvO_FgSn约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年9月8日
收录
2026年9月8日
更新
2026年11月12日
篇幅
约 9 分钟
题目
P17382

版本

  1. v2当前
  2. v1

好题。

题目描述非常清楚,给定了一颗基环树,对于每个点 uu,求它到图中最远点的最短距离。

基环树中只有一个环,去掉环上的边,剩余部分即为森林,考虑从这里入手。


首先对每个环上点为根的子树 dp,求出每个点 xx 向子树内部走的最远距离 subfx\text{subf}_x。


然后对于每个环上点,绕环上更短的路径,走到其他环上点的子树中。直接暴力枚举最坏 O(n2)O(n^2) 过不去,考虑使用单调队列分别维护两个方向,对于点 xx,绕环的最远距离 outx\text{out}_x。


把环上点 xx 的 outx\text{out}_x 当作初始的向上最远距离 upx\text{up}_x,再对每棵子树做一次换根 dp。

对于子树中的节点 xx,从它出发的最远距离,要么在子树中 subfx\text{subf}_x,要么通过父节点方向 upx\text{up}_x。

定义 w(u,v)w(u, v) 代表两点间的边权。upx\text{up}_x 的转移利用父节点 ff 的 upf\text{up}_f 以及 xx 的兄弟节点 yy 的 max⁡{subfy+w(f,y)}\max\{\text{subf}_y + w(f, y)\},用前后缀最大值 O(1)O(1) 得到。

最终每个点的答案就是 max⁡{subfx,upx}\max\{\text{subf}_x, \text{up}_x\}。


总时间复杂度 O(n)O(n)。

注意本题有个坑点,普通递归 dfs 会在某些点 MLE,需要改成迭代式的。

贴一个 dfs 没改迭代的代码
C++
// author: luogu @ OvO_FgSn
// time: 2026.9.7
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define err() assert(0)
#define debug(x) cerr << "debug: " << #x << " = " << x << endl
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define repp(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define perr(i, a, b) for (int i = (a); i > (b); i--)
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
inline int read(){
	int res = 0, f = 1; char c = getchar();
	while (c < '0' || c > '9'){ if (c == '-') f = -1; c = getchar(); }
	while (c >= '0' && c <= '9'){ res = res * 10 + c - '0'; c = getchar(); }
	return res * f;
}

// Define Variables & Structures & Functions & Others Here.

const int N = 3e5 + 10;
int n, deg[N], up[N], cir[N], out[N], subf[N], pref[N], dep[N], ans[N], ch[N], wch[N];
int pre[N], suf[N], rcir[N], ew[N], rew[N];
int k, rpref[N];
bool onc[N];
struct edge{
	int nxt, val;
};
vector<edge> g[N];
queue<int> q;
deque<int> dq;
void toposort(){
	memset(onc, 1, sizeof(onc));
	rep(i, 1, n)
		if (deg[i] == 1) q.push(i);
	while (!q.empty()){
		int x = q.front();
		q.pop();
		onc[x] = 0;
		repp(i, 0, g[x].size()){
			int y = g[x][i].nxt;
			deg[y]--;
			if (deg[y] == 1) q.push(y);
		}
	}
}
void dfs1(int x, int fa){
	repp(i, 0, g[x].size()){
		int y = g[x][i].nxt, yy = g[x][i].val;
		if (onc[y]) continue;
		if (y == fa) continue;
		dep[y] = dep[x] + yy;
		dfs1(y, x);
		subf[x] = max(subf[x], subf[y] + yy);
	}
}
void dfs2(int x, int fa){
    ans[x] = max(subf[x], up[x]);
    int m = 0;
    repp(i, 0, g[x].size()){
        int y = g[x][i].nxt, w = g[x][i].val;
        if (onc[y] || y == fa) continue;
        ch[++m] = y;
        wch[m] = w;
    }
    pre[0] = suf[m+1] = 0;
    rep(i, 1, m)
        pre[i] = max(pre[i-1], subf[ch[i]] + wch[i]);
    per(i, m, 1)
        suf[i] = max(suf[i+1], subf[ch[i]] + wch[i]);
    rep(i, 1, m){
        int y = ch[i], w = wch[i];
        up[y] = max(up[x], max(pre[i-1], suf[i+1])) + w;
        dfs2(y, x);
    }
}
void solve(){
	n = read();
	rep(i, 1, n){
		int u = read(), v = read(), w = read();
		g[u].push_back(edge{v, w});
		g[v].push_back(edge{u, w});
		deg[u]++;
		deg[v]++;
	}
	toposort();
	
	rep(i, 1, n)
		if (onc[i])
			dfs1(i, 0);
	
	int st, x, lst = 0, hh = 0;
	rep(i, 1, n)
		if (onc[i]){
			st = i;
			break;
		}
	x = st;
	do{
		cir[k] = x;
		repp(i, 0, g[x].size()){
			int y = g[x][i].nxt, yy = g[x][i].val;
			if (onc[y] && y != lst){
				ew[k] = yy;
				hh += yy;
				lst = x;
				x = y;
				break;
			}
		}
		k++;
	} while (x != st);
	rep(i, 0, 2 * k - 1)
		pref[i + 1] = pref[i] + ew[i % k];
	hh /= 2;
	int r = 0;
	rep(i, 0, k - 1){
		if (r < i) r = i;	
		while (r + 1 <= i + k - 1 && pref[r + 1] - pref[i] <= hh){
			r++;
			while (!dq.empty() && subf[cir[dq.back() % k]] + pref[dq.back()] <= subf[cir[r % k]] + pref[r])
				dq.pop_back();
			dq.push_back(r);
		}
		while (!dq.empty() && dq.front() <= i) dq.pop_front();
		if (!dq.empty())
			out[cir[i]] = max(out[cir[i]], subf[cir[dq.front() % k]] + pref[dq.front()] - pref[i]);
	}
	rep(i, 0, k - 1)
	    rcir[i] = cir[k - i - 1];
	rep(i, 0, k - 2)
	    rew[i] = ew[k - i - 2];
	rew[k - 1] = ew[k - 1];
	rpref[0] = 0;
	for (int i = 0; i < 2 * k; ++i)
	    rpref[i + 1] = rpref[i] + rew[i % k];
	while (!dq.empty()) dq.pop_front();
	r = 0;
	rep(i, 0, k - 1){
	    if (r < i) r = i;	
	    while (r + 1 <= i + k - 1 && rpref[r + 1] - rpref[i] <= hh){
	        r++;
	        while (!dq.empty() && subf[rcir[dq.back() % k]] + rpref[dq.back()] <= subf[rcir[r % k]] + rpref[r])
	            dq.pop_back();
	        dq.push_back(r);
	    }
	    while (!dq.empty() && dq.front() <= i) dq.pop_front();
	    if (!dq.empty())
	        out[rcir[i]] = max(out[rcir[i]], subf[rcir[dq.front() % k]] + rpref[dq.front()] - rpref[i]);
	}
	rep(i, 1, n)
		if (onc[i]){
			up[i] = out[i];
			dfs2(i, 0);
		}
	rep(i, 1, n){
		printf("%lld ", ans[i]);
	}
	
}
signed main(){
	int t = 1;
	while (t--) solve();
	return 0;
}
原文
存档评论1 条抓取于