跳到正文

题解

题解:P7422 「PMOI-2」城市

sample_mean_VVV约 11 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 11 分钟
题目
P7422

版本

  1. v1当前

一道将圆方树和虚树揉在一起的题,做起来也蛮有意思。(虽然由于树剖写错导致我 WA 了好几发)

看到题干,要求统计方案数,要作为一个方案要满足两个要求,我们发现如果这是在一棵树上,条件一就很好判断,只要 AA 是 BiB_i 的祖先就行了,但是这是在一个图上,我们想到用圆方树将图转化成树。但是我们看到第二个条件,如果没有仔细看的话会以为两点只要都各自有一条路径到 AA 使这两条路径不相交就算答案,那么用圆方树就会导致错误。但实际上是任意路径,就像样例里的 3 和 4,它们对于 2 并不是互不影响的,所以在环上的两点是会互相影响的,所以可以放心用圆方树。

接着考虑将图化成树后怎么去完成,条件一已经说明 BB 肯定在 AA 的子树中,条件二说明每个 BB 必须出自 AA 的不同子树。我们发现两个不同颜色的点是一定不会出现在同一个集合的,我们可以尝试对于每个颜色分别处理,考虑到对于每个颜色建虚树,这样所有虚树上的点一共就是 nn 个。

接着我们考虑在虚树上进行统计,我们发现,在虚树上的点要么是我们当前枚举的颜色的点,要么拥有至少两棵包含有当前枚举的颜色的点的子树。而一个不是当前枚举颜色的圆点,它到每个儿子节点的边其实在原树上相当于一条链,这条链上面的除了当前点和儿子节点以外的圆点只有一个包含当前枚举颜色的点的子树,即儿子节点所在的那个。也就是说它们只能拥有 k=1k=1 的集合,不过也是有的,所以要把它统计上。我们提前预处理从 1 号点到每个点的圆点个数。这样拿儿子节点的和当前点的一减就能求出它们之间的链上的圆点个数,如果儿子节点也是圆点还要减一。得出来的圆点个数乘上儿子节点子树上的当前枚举颜色的点的个数就是这些点提供的方案数。

然后考虑虚树上的非当前枚举颜色的圆点,我们发现,对于每棵虚树上的子树,我们都可以选择将其包含的一个当前枚举的颜色的点加入集合或者都不加入,这让我们想到动态规划。而 kk 最大只有 20,所以我们可以直接枚举。于是我们设 dp[u][i]dp[u][i] 为第 uu 个点 ii 元集合的个数,初始 0 元点集有 1 个,其他全部为 0。设 size[u]size[u] 为点 uu 子树上的当前枚举颜色的点的个数。那么每枚举到一个子树,我们有 size[v]size[v] 种可能,即将其中一个点加入集合,不加入不需要讨论。也就是说 dp[u][i]+=dp[u][i−1]∗size[v]dp[u][i]+=dp[u][i-1]*size[v]。解决完这个我们发现,这样的数组占用空间太大了,每个点的值也不会影响其他点,况且最后我们都要将它们统计到方案数里,所以我们去掉第一维,每次 dp 时先清空数组就行。

最后我们想用 vector 保存每个颜色点的个数,但是颜色值实在太大,但是最多只有 5×1055\times 10^5 个,我们可以使用 unordered_map 离散化,这里用的是手写哈希表。

最后给出马蜂丑陋的代码:

C++
//Just Sayori
#pragma GCC diagnostic ignored "-Wnarrowing"
#pragma GCC diagnostic ignored "-Wsign-compare"
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#define ll long long
#define rnt register int
#define gr getchar
#define pr putchar
#define N 1000005
#define M 998244353
using namespace std;
inline ll read()
{
	ll x = 0, f = 1;
	char ch = gr();
	while (ch < '0' || ch > '9') ch == '-' ? f = -1, ch = gr() : ch = gr();
	while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = gr();
	return x * f;
}
inline void write(ll x)
{
	static int top = 0, sta[39];
	if (x < 0) pr('-'), x = -x;
	do sta[++top] = x % 10, x /= 10;
	while (x);
	while (top) pr(sta[top--] ^ 48);
}
struct HASH
{
#define HM 500000
	struct edge
	{
		int x, y;
		int next;
	} e[N];
	int head[HM], cnt;
	inline int get_num(int x)
	{
		return (x + HM) % HM;
	}
	int query(int x)
	{
		int g = get_num(x);
		for (rnt i = head[g]; i; i = e[i].next)
			if (e[i].x == x)
				return e[i].y;
		e[++cnt] = {x, cnt, head[g]}, head[g] = cnt;
		return cnt;
	}
} h;
struct EDGE
{
	struct edge
	{
		int v, next;
	} e[N << 2];
	int head[N << 1], cnt;
	inline void tdd(int u, int v)
	{
		e[++cnt] = {v, head[u]}, head[u] = cnt;
		e[++cnt] = {u, head[v]}, head[v] = cnt;
	}
} g, t, vt;
int n, m, k, bcc, cnt, lca, top, color;
ll ans;
int c[N << 1], d[N << 1], fa[N << 1], sz[N << 1], tp[N << 1], dfn[N << 1], low[N], bson[N << 1], deep[N << 1], stack[N];
ll dp[21];
vector<int> p[N];
void tarjan(int u)
{
	dfn[u] = low[u] = ++cnt;
	stack[++top] = u;
	for (rnt i = g.head[u]; i; i = g.e[i].next)
	{
		int v = g.e[i].v;
		if (!dfn[v])
		{
			tarjan(v), low[u] = min(low[u], low[v]);
			if (low[v] == dfn[u])
			{
				bcc++;
				while (stack[top + 1] != v)
					t.tdd(bcc, stack[top--]);
				t.tdd(bcc, u);
			}
		}
		else
			low[u] = min(low[u], dfn[v]);
	}
}
void dfs1(int u, int f)
{
	fa[u] = f, sz[u] = 1;
	deep[u] = deep[f] + 1;
	d[u] = d[f] + (u <= n);
	for (rnt i = t.head[u]; i; i = t.e[i].next)
	{
		int v = t.e[i].v;
		if (v == f) continue;
		dfs1(v, u);
		sz[u] += sz[v];
		if (sz[v] > sz[bson[u]]) bson[u] = v;
	}
}
void dfs2(int u, int p)
{
	tp[u] = p;
	dfn[u] = ++cnt;
	if (!bson[u]) return;
	dfs2(bson[u], p);
	for (rnt i = t.head[u]; i; i = t.e[i].next)
	{
		int v = t.e[i].v;
		if (v == fa[u] || v == bson[u]) continue;
		dfs2(v, v);
	}
}
int get_lca(int a, int b)
{
	while (tp[a] != tp[b])
	{
		if (deep[tp[a]] < deep[tp[b]]) swap(a, b);
		a = fa[tp[a]];
	}
	if (deep[a] > deep[b]) swap(a, b);
	return a;
}
void dfs3(int u, int f)
{
	sz[u] = c[u] == color;
	for (rnt i = vt.head[u]; i; i = vt.e[i].next)
	{
		int v = vt.e[i].v;
		if (v == f) continue;
		dfs3(v, u);
		sz[u] += sz[v];
		(ans += 1ll * sz[v] * (d[v] - d[u] - (v <= n)) % M) %= M; //求出两点路径上的不在虚树上的圆点个数,这些都是满足 k=1 的
	}
	if (c[u] == color || u > n) return;
	dp[0] = 1;
	for (rnt i = 1; i <= k; i++) dp[i] = 0;
	for (rnt i = vt.head[u]; i; i = vt.e[i].next)
	{
		int v = vt.e[i].v;
		if (v == f) continue;
		for (rnt j = k; j; j--)
			(dp[j] += 1ll * dp[j - 1] * sz[v] % M) %= M;//求出当前点 k 等于各个值的答案
	}
	for (rnt i = 1; i <= k; i++)
		ans += dp[i];
}
inline bool cmp(int a, int b)
{
	return dfn[a] < dfn[b];
}
void build()
{
	sort(p[color].begin(), p[color].end(), cmp);
	stack[top = 1] = 1, vt.cnt = vt.head[1] = 0;
	for (rnt i = 0; i < p[color].size(); i++)
		if (p[color][i] != 1)
		{
			lca = get_lca(p[color][i], stack[top]);
			if (lca != stack[top])
			{
				while (dfn[lca] < dfn[stack[top - 1]])
					vt.tdd(stack[top - 1], stack[top]), top--;
				if (dfn[lca] != dfn[stack[top - 1]])
					vt.head[lca] = 0, vt.tdd(lca, stack[top]), stack[top] = lca;
				else
					vt.tdd(lca, stack[top--]);
			}
			vt.head[p[color][i]] = 0, stack[++top] = p[color][i];
		}
	for (rnt i = 1; i < top; i++)
		vt.tdd(stack[i], stack[i + 1]);
}
int main()
{
	n = read(), m = read(), k = read(), bcc = n;
	for (rnt i = 1; i <= n; i++) c[i] = read(), c[i] = h.query(c[i]), p[c[i]].push_back(i);
	for (rnt i = 1; i <= m; i++)
		g.tdd(read(), read());
	tarjan(1), cnt = 0, dfs1(1, 0), dfs2(1, 1);
	for (color = 1; color <= h.cnt; color++)
		build(), dfs3(1, 0);
	write(ans % M);
	return 0;
}

感谢您的观看!

原文