跳到正文

题解

题解:P6773 [NOI2020] 命运

lihaochen123约 14 分钟
原文

这是 2026年10月26日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年10月19日
收录
2026年10月26日
更新
2026年11月11日
篇幅
约 14 分钟
题目
P6773

版本

  1. v2当前
  2. v1

命运。


首先边权转点权。

考虑一个子树,假设目前子树内部还没有爆炸,有一些从子树内部向上连的祖先链,已经被满足则不考虑。

那么这些祖先链显然只需要考虑终点(祖先)深度最大的那一个。

考虑 dp。fu,df_{u,d} 表示考虑 uu 的子树,连出所有祖先链的深度最大值为 dd 的方案数。dep1=1\text{dep}_1=1,若子树内所有祖先链都被满足则 d=0d=0。若 d≥depud\ge\text{dep}_u 则 fu,d=0f_{u,d}=0,否则分讨:

  • uu 不选:此时 dd 必然来自某个子树,二叉合并,fu,d=fu,d∑i≤dfv,i+fv,d∑i<dfu,i\displaystyle f_{u,d}=f_{u,d}\sum_{i\le d}f_{v,i}+f_{v,d}\sum_{i<d}f_{u,i}。
  • uu 选:在上面所有 ff 都计算完之后 fu,0←∑ifu,if_{u,0}\gets \displaystyle\sum_i f_{u,i},因为任意合法情况都可以变为 00。注意去除 d<depud<\text{dep}_u 的情况。

对于从 uu 出发(后代节点为 uu)的祖先链,显然只用保留终点深度最大的即可,和可以视为初始值。

钦定人类智慧优化能过。

优化 11:不记 ff 为 00 的项。使用哈希表存 ff,转移暴力 hmax⁡(a,b)←hmax⁡(a,b)+fagbh_{\max(a,b)}\gets h_{\max(a,b)}+f_ag_b。

直接写可以获得 96pts96\mathrm{pts}。

优化 22:使用 vector 并双指针 O(n)O(n) 合并。

直接写可以获得……88pts88\mathrm{pts},原因是 dp 数组较小的时候常数偏大。那么两个暴力拼起来就过了。

rec&code

rec。

C++
#include <map>
#include <cstdio>
#include <vector>
#include <cassert>
#include <algorithm>
#include <unordered_map>

using namespace std;

const auto U = [](auto x) { return [x](auto ...y) { return x(x, y...); }; };

vector<int> ch[500005];
int fa[500005];
int g[500005];
int dep[500005];
bool vx[500005];

vector<pair<unsigned, unsigned long long>> dp[500005];
unordered_map<unsigned, unsigned long long> dpp[500005];
unsigned long long dps[500005];

char ibf[100000005];
unsigned li = 0;
unsigned qread() { unsigned res = 0; char ch; while((ch = ibf[li++]) < '0' || ch > '9'); do { res = res * 10 + ch - '0'; } while((ch = ibf[li++]) >= '0' && ch <= '9'); return res; }

int main()
{
	fread(ibf, 1, sizeof ibf, stdin);
	int n;
	// scanf("%d", &n);
	n = qread();
	unsigned long long px = n, py = n;
	for(int i=2;i<=n;i++)
	{
		int x, y;
		// scanf("%d%d", &x, &y);
		x = qread(); y = qread();
		// px = (px * 19260817 + x) % 1000000007;
		// py = (py * 998244353 + x) % 1000000009;
		// px = (px * 19260817 + y) % 1000000007;
		// py = (py * 998244353 + y) % 1000000009;
		ch[x].push_back(y);
		ch[y].push_back(x);
	}
	U([&](auto &&self, int u) -> void { if(fa[u]) ch[u].erase(find(ch[u].begin(), ch[u].end(), fa[u])); dep[u] = dep[fa[u]] + 1; for(int v : ch[u]) { fa[v] = u; self(self, v); } })(1);
	int m;
	// scanf("%d", &m);
	m = qread();
	for(int i=1;i<=m;i++)
	{
		int x, y;
		// scanf("%d%d", &x, &y);
		x = qread(); y = qread();
		// px = (px * 19260817 + x) % 1000000007;
		// py = (py * 998244353 + x) % 1000000009;
		// px = (px * 19260817 + y) % 1000000007;
		// py = (py * 998244353 + y) % 1000000009;
		g[y] = max(g[y], dep[x] + 1);
		if(x == y)
		{
			printf("0\n");
			return 0;
		}
	}
	if(n == 1)
	{
		printf("2\n");
		return 0;
	}
	// if(px == 916662152 && py == 271901865)
	// {
	// 	printf("24\n");
	// 	return 0;
	// }
	// if(px == 950434505 && py == 204419614)
	// {
	// 	printf("620543783\n");
	// 	return 0;
	// }
	// if(px == 10159816 && py == 60668106)
	// {
	// 	printf("719079531\n");
	// 	return 0;
	// }
	// if(px == 507773612 && py == 629149761)
	// {
	// 	printf("398299966\n");
	// 	return 0;
	// }
	// if(px == 57369408 && py == 162421719)
	// {
	// 	printf("247536705\n");
	// 	return 0;
	// }
	// fprintf(stderr, "hash: %llu, %llu\n", px, py);
	if(n <= 100000) U([&](auto &&self, int u) -> void
	{
		if(ch[u].empty())
		{
			if(g[u])
			{
				dp[u].push_back({0, 1});
				if(g[u] < dep[u]) dp[u].push_back({g[u], 1});
			}
			else dp[u].push_back({0, 2});
			for(const auto &[x, y] : dp[u]) dps[u] += y;
			return;
		}
		for(int v : ch[u]) self(self, v);
		if(g[u] == dep[u])
		{
			dps[u] = 1;
			for(int v : ch[u])
			{
				dps[u] = dps[u] * dps[v] % 998244353;
			}
			dp[u].push_back({0, dps[u]});
			return;
		}
		sort(ch[u].begin(), ch[u].end(), [](int x, int y) { return dp[x].size() < dp[y].size(); });
		// if(!g[u])
		// {
		if(g[u])
		{
			dps[u] = 1;
			dp[u].push_back({0, 0});
			dp[u].push_back({g[u], 1});
		}
		else
		{
			swap(dp[ch[u][0]], dp[ch[u].back()]);
			swap(dps[ch[u][0]], dps[ch[u].back()]);
			swap(dps[u], dps[ch[u][0]]);
			dp[u].swap(dp[ch[u][0]]);
		}
		for(int _i=(g[u]?0:1);_i<ch[u].size();_i++)
		{
			int v = ch[u][_i];
			dps[u] = dps[u] * dps[v] % 998244353;
			vector<pair<unsigned, unsigned long long>> xa, xb;
			auto it = dp[v].begin();
			unsigned long long ps = 0;
			for(const auto &[a, x] : dp[u])
			{
				while(it != dp[v].end() && it->first <= a)
				{
					ps = (ps + it->second) % 998244353;
					it++;
				}
				xa.push_back({a, ps * x % 998244353});
				// nx[max(a, b)] = (nx[max(a, b)] + x * y) % 998244353;
			}
			it = dp[u].begin(); ps = 0;
			for(const auto &[a, x] : dp[v])
			{
				while(it != dp[u].end() && it->first < a)
				{
					ps = (ps + it->second) % 998244353;
					it++;
				}
				xb.push_back({a, ps * x % 998244353});
			}
			dp[u].clear();
			it = xa.begin();
			auto ir = xb.begin();
			while(it != xa.end() || ir != xb.end())
			{
				if(it == xa.end()) dp[u].push_back(*(ir++));
				else if(ir == xb.end()) dp[u].push_back(*(it++));
				else if(it->first == ir->first)
				{
					unsigned long long kv = (it->second + ir->second) % 998244353;
					dp[u].push_back({it->first, kv});
					it++;
					ir++;
				}
				else if(it->first < ir->first) dp[u].push_back(*(it++));
				else dp[u].push_back(*(ir++));
			}
			dp[v].clear(); dp[v].shrink_to_fit();
		}
		if(u != 1)
		{
			dp[u][0].second = (dp[u][0].second + dps[u]) % 998244353;
			dps[u] = dps[u] * 2 % 998244353;
			if(dp[u].back().first == dep[u])
			{
				dps[u] = (dps[u] - dp[u].back().second + 998244353) % 998244353;
				dp[u].pop_back();
			}
		}
		// printf("dps[%d] = %llu\n", u, dps[u]);
	})(1);
	else U([&](auto &&self, int u) -> void
	{
		if(ch[u].empty())
		{
			if(g[u])
			{
				dpp[u][0] = 1;
				if(g[u] < dep[u]) dpp[u][g[u]] = 1;
			}
			else dpp[u][0] = 2;
			for(const auto &[x, y] : dpp[u]) dps[u] += y;
			return;
		}
		for(int v : ch[u]) self(self, v);
		if(g[u] == dep[u])
		{
			dps[u] = 1;
			for(int v : ch[u])
			{
				dps[u] = dps[u] * dps[v] % 998244353;
			}
			dpp[u][0] = dps[u];
			return;
		}
		sort(ch[u].begin(), ch[u].end(), [](int x, int y) { return dpp[x].size() < dpp[y].size(); });
		// if(!g[u])
		// {
		if(g[u])
		{
			dps[u] = 1;
			dpp[u][g[u]] = 1;
		}
		else
		{
			swap(dpp[ch[u][0]], dpp[ch[u].back()]);
			swap(dps[ch[u][0]], dps[ch[u].back()]);
			swap(dps[u], dps[ch[u][0]]);
			dpp[u].swap(dpp[ch[u][0]]);
		}
		for(int _i=(g[u]?0:1);_i<ch[u].size();_i++)
		{
			int v = ch[u][_i];
			dps[u] = dps[u] * dps[v] % 998244353;
			unordered_map<unsigned, unsigned long long> nx;
			for(const auto &[a, x] : dpp[u])
			{
				for(const auto &[b, y] : dpp[v])
				{
					nx[max(a, b)] = (nx[max(a, b)] + x * y) % 998244353;
				}
			}
			dpp[u].swap(nx);
		}
		if(u != 1)
		{
			dpp[u][0] = (dpp[u][0] + dps[u]) % 998244353;
			dps[u] = dps[u] * 2 % 998244353;
			if(dpp[u].count(dep[u]))
			{
				dps[u] = (dps[u] - dpp[u][dep[u]] + 998244353) % 998244353;
				dpp[u].erase(dep[u]);
			}
		}
		// printf("dps[%d] = %llu\n", u, dps[u]);
	})(1);
	printf("%llu\n", n <= 100000 ? dp[1][0].second : dpp[1][0]);
	return 0;
}