跳到正文

题解

P11363 [NOIP2024] 树的遍历

Nelofus约 8 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月13日
篇幅
约 8 分钟

版本

  1. v1当前

虽然这个题现在不能交题解了但还是写一下。。。

k=1k=1 不难推出是

∏i=1n(deg⁡(i)−1)!\prod_{i=1}^{n}(\deg(i)-1)!

首先肯定是容斥,设 f(S)f(S) 表示能够被边集 SS 中所有边生成的树的数量,那么答案即

∑∣S∣≥1(−1)∣S∣−1f(S)\sum_{|S|\ge 1}(-1)^{|S|-1}f(S)

∣S∣=1|S|=1 即 k=1k=1。

接下来考虑 ∣S∣=2|S|=2 的情况,对于两条边中间的节点,设其集合为 LL:

NOIP2024_.jpg

显然连接的首尾两条边必须是路径上的,中间挂的节点可以任意顺序排列,所以贡献为 (deg⁡(u)−2)!(\deg(u)-2)!。

然后是 ∣S∣=3|S|=3 的情况,显然这三条边如果不在同一条链上根本没贡献,如果在同一条链上还是 ∏u∈L(deg⁡(u)−2)!∏u∉L(deg⁡(u)−1)!\prod_{u\in L} (\deg(u)-2)!\prod_{u\notin L}(\deg(u)-1)!。

总感觉能消掉一些东西,假设边 aa 和边 bb 中间夹着 kk 条边,那么 ∏u∈L(deg⁡(u)−2)!∏u∉L(deg⁡(u)−1)!\prod_{u\in L} (\deg(u)-2)!\prod_{u\notin L}(\deg(u)-1)! 会被计算

∑i=0k(−1)i(ki)=(1+(−1))k=[k=0]\sum_{i=0}^{k}(-1)^i\binom{k}{i}=(1+(-1))^k=[k=0]

所以如果 a,ba,b 不相邻,它们根本没有贡献。否则贡献就是 ∏u∈L(deg⁡(u)−2)!∏u∉L(deg⁡(u)−1)!\prod_{u\in L} (\deg(u)-2)!\prod_{u\notin L}(\deg(u)-1)!。

对于不在一条直链上的,我们在 LCA 处统计,再特殊统计在一条直链上的即可。

C++
// Code by Heratino & Nelofus
#include <bits/stdc++.h>
using i64 = long long;

//{{{
template<typename T>
inline void chkmin(T &a, const T &b) {if (a > b)	a = b;}
template<typename T>
inline void chkmax(T &a, const T &b) {if (a < b)	a = b;}
//}}}

constexpr int N = 1e5 + 10;
constexpr int mod = 1e9 + 7;
inline int fpow(int x, int k) {
	int res = 1;
	for (; k; k >>= 1, x = 1ll * x * x % mod)
		if (k & 1)
			res = 1ll * res * x % mod;
	return res;
}
inline int Add(const int &x, const int &y) {return x + y >= mod ? x + y - mod : x + y;}
inline int Sub(const int &x, const int &y) {return x - y < 0 ? x - y + mod : x - y;}
inline int Mul(const int &x, const int &y) {return 1ll * x * y % mod;}
inline int Div(const int &x, const int &y) {return Mul(x, fpow(y, mod - 2));}
inline void AddTo(int &x, const int &y) {x = Add(x, y);}
inline void SubTo(int &x, const int &y) {x = Sub(x, y);}
inline void MulTo(int &x, const int &y) {x = Mul(x, y);}
inline void DivTo(int &x, const int &y) {x = Div(x, y);}

int n, k;
int eu[N], ev[N], vis[N];
std::vector<std::pair<int, int>> G[N];

int fac[N], ifac[N];
int sp[N];
void dfs1(int u, int fa) {
	sp[u] = fac[G[u].size() - 1];
	for (const auto &[v, w] : G[u]) {
		if (v == fa)	continue;
		dfs1(v, u);
		MulTo(sp[u], sp[v]);
	}
}

int ans = 1;
int f[N];
int rt = 0;
void dfs2(int u, int fa) {
	int outside = Div(sp[rt], sp[u]);
	f[u] = 0;
	int suf = 1, ftp = 1, pre = 0, all = 1;
	for (const auto &[v, w] : G[u]) {
		if (v == fa)
			continue;
		MulTo(suf, sp[v]);
		MulTo(all, sp[v]);
	}
	for (const auto &[v, w] : G[u]) {
		if (v == fa)	continue;
		dfs2(v, u);

		// {{{ 直链
		if (w == 1) {
			SubTo(ans, Mul(Div(sp[rt], sp[v]), f[v]));
			// 只要最浅的
			f[v] = sp[v];
		}
		// }}}

		AddTo(f[u], Mul(f[v], Div(all, sp[v])));

		// {{{ LCA
		DivTo(suf, sp[v]);

		int t = fac[G[u].size() - 2];
		int p = Mul(t, Mul(Mul(pre, suf), f[v]));
		SubTo(ans, Mul(outside, p));

		MulTo(pre, sp[v]);
		AddTo(pre, Mul(f[v], ftp));

		MulTo(ftp, sp[v]);
		// }}}
	}
	if (f[u]) {
		MulTo(f[u], fac[G[u].size() - 2]);
	}
}

inline void clear() {
	for (int i = 1; i <= n; i++)
		G[i].clear(), vis[i] = 0, sp[i] = 0;
	ans = 1, rt = 0;
}

inline void solve() {
	std::cin >> n >> k;
	clear();

	for (int i = 1; i < n; i++)
		std::cin >> eu[i] >> ev[i];

	for (int i = 1; i <= k; i++) {
		int e;
		std::cin >> e;
		vis[e] = 1;
	}

	if (n == 2) {
		std::cout << "1" << '\n';
		return ;
	}

	for (int i = 1; i <= n; i++) {
		G[eu[i]].emplace_back(ev[i], vis[i]);
		G[ev[i]].emplace_back(eu[i], vis[i]);
	}
	for (int i = 1; i <= n; i++)
		if (G[i].size() > 1)
			rt = i;
	for (int i = 1; i <= n; i++)
		MulTo(ans, fac[G[i].size() - 1]);
	MulTo(ans, k);
	dfs1(rt, 0);
	dfs2(rt, 0);
	std::cout << ans << '\n';
}

int main() {
#ifdef HeratinoNelofus
	freopen("input.txt", "r", stdin);
#else
	freopen("traverse.in", "r", stdin);
	freopen("traverse.out", "w", stdout);
#endif
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);

	fac[0] = 1;
	for (int i = 1; i < N; i++)
		fac[i] = Mul(fac[i - 1], i);
	ifac[N - 1] = Div(1, fac[N - 1]);
	for (int i = N - 2; i >= 0; i--)
		ifac[i] = Mul(ifac[i + 1], i + 1);

	int c, T;
	std::cin >> c >> T;
	while (T--) {
		solve();
	}
	return 0;
}
原文