跳到正文

题解

P10064 [SNOI2024] 公交线路

ningago约 6 分钟
原文
文章信息与版本

文章信息

发布
2024年2月20日
收录
2026年7月24日
更新
2026年11月10日
篇幅
约 6 分钟
获赞
8
题目
P10064

版本

  1. v1当前
  • 性质:对于题目中的 SS 合法性,只用考虑任意两叶子节点 u,vu,v 在 GG 中;距离是否 ≤2\leq 2 即可。

如果 u,vu,v 任意一个点不是叶子节点,把 u/vu/v 修改成其对应子树内任意叶子节点,同样满足条件。

也就是说可以将 SS 合法的判定条件转化为:令 TxT_x 表示叶节点 xx 能够通过 SS 中路径一步到达的节点集合,则对于任意两叶子节点 u,vu,v,Tu∩Tv≠∅T_u\cap T_v\ne \varnothing。

  • 性质:上述条件相当于 ⋂x∈leafTx=∅\bigcap_{x\in \text{leaf}}T_x=\varnothing。

反证,由于 TxT_x 一定是连通块,所以若上式为空则相当于存在环,显然不成立。

也就是说此时只需要判断 ⋂x∈leafTx\bigcap_{x\in \text{leaf}}T_x 是否非空即可,由于 ⋂xTx\bigcap_{x}T_x(若非空则)必然是一个连通块,所以可以使用点 −- 边容斥,拆分成两个类似的问题:

  • 对于每个节点 kk,求 ∀x∈leaf:k∈Tx\forall{x\in \text{leaf}}:k\in T_x 的方案数。
  • 对于每条边 u→vu\to v,求 ∀x∈leaf:u,v∈Tx\forall{x\in \text{leaf}}:u,v\in T_x 的方案数。

考虑第一个问题(第二个问题是类似的,且更容易求解),直接做的困难在于如果一条经过 kk 的路径 (p,q)(p,q) 的两端点都是叶子,将会让 k∈Tp/k∈Tqk\in T_p/k\in T_q 两个条件同时成立。

但 k∉Txk\not\in T_x 这个条件是简单的,于是考虑容斥:固定 kk,令 fif_i 表示钦定有 ii 个叶子节点满足 k∉Txk\not\in T_x 的方案数,此时对于以叶节点 uu 为端点的所有路径 (u,v)(u,v),vv 都不能是 kk 以及子树外的点。可以直接对于每个子树背包求解,复杂度 O(n∣leaf∣2)O(n|\text{leaf}|^2)。

一个巧妙的优化:考虑背包合并子树时,合并到最后一个子树的时候,此时就不存在 (p,q)(p,q) 的问题了,所以可以不对这个子树进行容斥,直接贡献(钦定过的叶子不连路径,否则随便连)。

考虑任意指定一个(非叶子)根 rootroot,在考虑到 kk 时,将 fakfa_k 对应的子树放在最后一个处理。此时的合并复杂度只与子数内叶子个数有关(即树形背包复杂度),复杂度 O(n∣leaf∣)=O(n2)O(n|\text{leaf}|)=O(n^2)。(假定预处理了快速幂)

C++
#define N 3010
int n, root;
int fac[N], invf[N]; int C(int n, int m) { return 1ll * fac[n] * invf[m] % mod * invf[n - m] % mod; }
int h[N], e[N << 1], ne[N << 1], idx = -1, du[N];
void add_edge(int x, int y) { ne[++idx] = h[x], h[x] = idx, e[idx] = y; }
void add(int x, int y) { add_edge(x, y), add_edge(y, x); }
int f[N], tmp[N], sz[N], tot[N], all, ans;
void dfs(int k, int fa)
{
	sz[k] = 1, tot[k] = (du[k] == 1);
	if(du[k] == 1) { /*点 - 边抵消了*/ return; }
	for(int _ = h[k]; ~_; _ = ne[_]) if(e[_] != fa) dfs(e[_], k);
	f[0] = 1;
	for(int _ = h[k]; ~_; _ = ne[_])
	{
		int nx = e[_]; if(nx == fa) continue;
		for(int i = 0; i <= tot[k]; i++) for(int j = 0; j <= tot[nx]; j++)
			plus_(tmp[i + j], 1ll * f[i] * C(tot[nx], j) % mod * ksm(2, sz[nx] * (sz[nx] - 1) / 2 + (sz[nx] - j) * (sz[k] - i)) % mod);
		sz[k] += sz[nx]; tot[k] += tot[nx];
		for(int i = 0; i <= tot[k]; i++) f[i] = tmp[i], tmp[i] = 0;
	}
	int s = n - sz[k], t = all - tot[k], res = 0;
	for(int i = 0; i <= tot[k]; i++)
	{
		int v = 1ll * f[i] * ksm(2, s * (s - 1) / 2 + (s - t) * (sz[k] - i)) % mod * ksm(ksm(2, sz[k] - i) - 1, t) % mod;
		if(i & 1) plus_(res, sm(mod - v));
		else plus_(res, v);
	}
	plus_(ans, res);
	if(!fa) return;
	for(int i = res = 0; i <= tot[k]; i++)
	{
		int v = 1ll * ksm(2, s * (s - 1) / 2 + sz[k] * (sz[k] - 1) / 2 + (s - t) * (sz[k] - i)) * ksm(ksm(2, sz[k] - i) - 1, t) % mod * C(tot[k], i) % mod;
		if(i & 1) plus_(res, sm(mod - v));
		else plus_(res, v);
	}
	plus_(ans, sm(mod - res));
}

void solve()
{
	memset(h, idx = -1, sizeof(h));
	n = read();
	if(n <= 2) { print(1, '\n'); return; }
	for(int i = 1, x, y; i < n; i++) du[x = read()]++, du[y = read()]++, add(x, y);
	for(root = 1; du[root] == 1; root++);
	all = std::count(du + 1, du + 1 + n, 1);
	dfs(root, 0);
	print(ans, '\n');
}
原文
存档评论4 条抓取于