跳到正文

题解

题解:P9981 [USACO23DEC] Minimum Longest Trip G

LiuDai约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年7月3日
收录
2026年7月3日
更新
2026年11月10日
篇幅
约 7 分钟
题目
P9981

版本

  1. v1当前

模拟赛犯糖暴力做的。

题意:给出一个DAG,边长为 11,边有标签,求每个点出发的最长路长度和标签字典序最小的路径的标签之和。

做法:第一问是经典拓扑排序后dp,难的是第二问:如何保证路径的字典序最小?

一个朴素的想法是把每个点出发的最长路经过的所有节点存到一个数组里,比较时按位比较,这样的总复杂度是 O(n2)\mathcal{O}(n^2) 的,不可接受。

注意到每从一个出边转移过来的时候,只会在这个路径结尾添加一个点,于是想到用可持久化数组来维护,将空间复杂度降到 O(nlog⁡n)\mathcal{O}(n \log n)。

但是这样比较时还是只能诸位比较,时间复杂度并没有改善,考虑使用哈希优化,对每个线段树上的节点计算该节点管理区间的哈希,于是可以在线段树上二分做到 O((n+m)log⁡n)\mathcal{O}((n+m) \log n) 时间复杂度。

拓扑排序 O(n+m)\mathcal{O}(n+m),线段树插入和查询共 O((n+m)log⁡n)\mathcal{O}((n+m) \log n),总时间复杂度 O((n+m)log⁡n)\mathcal{O}((n+m) \log n)。

空间复杂度 O(nlog⁡n)\mathcal{O}(n \log n)。

细节见代码。

AC代码
C++
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using U = uint64_t;
constexpr ll N = 2e5 + 5, M = 4e5 + 5, LN = 20, V = 2e5;

struct edge { int v, w, nxt; } g[M];
int head[N], gcnt;
void add(const int u, const int v, const int w) { g[++gcnt] = {v, w, head[u]}, head[u] = gcnt; }

int n, m, deg[N];
int idx[N], tcnt;
void topo() {
	queue<int> p;
	for (int i = 1; i <= n; ++i) if (deg[i] == 0) p.emplace(i);
	while (!p.empty()) {
		const int u = p.front(); p.pop();
		idx[tcnt--] = u;
		for (int q = head[u]; q; q = g[q].nxt) {
			const int v = g[q].v;
			--deg[v];
			if (deg[v] == 0) p.emplace(v);
		}
	}
}

U H[10];
struct psegtree {
	struct segnode {
		int ls, rs, v;
		U h;
	} st[LN * N];
	int root[N], cnt;
	static U h(const U v, const U l, const U r, const U ul, const U ur) {
		const U a = (v + H[0]) * H[1] + (l + H[2]) * H[3] + (r + H[4]) * H[5];
		const U b = (ul + H[6]) * H[7] + (ur + H[8]) * H[9];
		const U k = (a >> 1) & 0x3f37f373'73ff7337ull + b ^ l & 0xf73ff733'f7ff3fffull;
		const U res = (k ^ r) % ((1ull << 61) - 1ull) ^ ul ^ (ur << 1);
		return res; // 哈希函数设计得不好容易被卡,一直85-95pts徘徊
	}
	explicit psegtree() : st{}, root{}, cnt(0) {}
	void insert(int &rt, const int p, const int ul, const int ur, const int v, const int k) {
		if (!rt) rt = ++cnt;
		if (ul == ur) {
			if (ul == v) st[rt].v = k;
			st[rt].h = h(st[rt].v, st[st[rt].ls].h, st[st[rt].rs].h, ul, ur);
			return;
		}
		const int mid = (ul + ur) >> 1;
		if (v <= mid) st[rt].rs = st[p].rs, insert(st[rt].ls, st[p].ls, ul, mid, v, k);
		if (mid < v) st[rt].ls = st[p].ls, insert(st[rt].rs, st[p].rs, mid + 1, ur, v, k);
		st[rt].h = h(st[rt].v, st[st[rt].ls].h, st[st[rt].rs].h, ul, ur);
	}
	int cmp(const int lr, const int rr, const int ul, const int ur) const {
		if (st[lr].h == st[rr].h) return 0;
		if (ul == ur) return st[lr].v < st[rr].v ? -1 : st[lr].v == st[rr].v ? 0 : 1;
		const int mid = (ul + ur) >> 1;
		if (st[st[lr].ls].h == st[st[rr].ls].h) return cmp(st[lr].rs, st[rr].rs, mid + 1, ur);
		return cmp(st[lr].ls, st[rr].ls, ul, mid);
	}
} pst;

ll rl[N], rs[N];
void maintain() {
	for (int _ = 1; _ <= n; ++_) {
		const int u = idx[_];
		int iv = 0, iw = 0;
		for (int q = head[u]; q; q = g[q].nxt) {
			const int v = g[q].v, w = g[q].w;
			if (rl[v] + 1 > rl[u] || (rl[v] + 1 == rl[u] && (w < iw || (w == iw && pst.cmp(pst.root[iv], pst.root[v], 0, V) > 0))))
				rl[u] = rl[v] + 1, rs[u] = rs[v] + w, iv = v, iw = w;
		}
		if (rl[u]) pst.insert(pst.root[u], pst.root[iv], 0, V, rl[u], iw);
	}
}

signed main() {
	{
		mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
		uniform_int_distribution<U> u(1ull);
		H[0] = u(mt);
		H[1] = u(mt);
		H[2] = u(mt);
		H[3] = u(mt);
		H[4] = u(mt);
		H[5] = u(mt);
		H[6] = u(mt);
		H[7] = u(mt);
		H[8] = u(mt);
		H[9] = u(mt);
	}
	cin >> n >> m; tcnt = n;
	for (int i = 1, u, v, w; i <= m; ++i) {
		cin >> u >> v >> w;
		add(u, v, w);
		++deg[v];
	}
	topo();
	maintain();
	for (int i = 1; i <= n; ++i) cout << rl[i] << ' ' << rs[i] << '\n';
	return 0;
}