题解:P7422 「PMOI-2」城市
sample_mean_VVV · · 题解
一道将圆方树和虚树揉在一起的题,做起来也蛮有意思。(虽然由于树剖写错导致我 WA 了好几发)
看到题干,要求统计方案数,要作为一个方案要满足两个要求,我们发现如果这是在一棵树上,条件一就很好判断,只要
接着考虑将图化成树后怎么去完成,条件一已经说明
接着我们考虑在虚树上进行统计,我们发现,在虚树上的点要么是我们当前枚举的颜色的点,要么拥有至少两棵包含有当前枚举的颜色的点的子树。而一个不是当前枚举颜色的圆点,它到每个儿子节点的边其实在原树上相当于一条链,这条链上面的除了当前点和儿子节点以外的圆点只有一个包含当前枚举颜色的点的子树,即儿子节点所在的那个。也就是说它们只能拥有
然后考虑虚树上的非当前枚举颜色的圆点,我们发现,对于每棵虚树上的子树,我们都可以选择将其包含的一个当前枚举的颜色的点加入集合或者都不加入,这让我们想到动态规划。而
最后我们想用 vector 保存每个颜色点的个数,但是颜色值实在太大,但是最多只有 unordered_map 离散化,这里用的是手写哈希表。
最后给出马蜂丑陋的代码:
//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;
}