跳到正文

题解

题解:P11824 [湖北省选模拟 2025] 团队协作 / team

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

文章信息

发布
2025年2月27日
收录
2025年12月12日
更新
2026年11月22日
篇幅
约 11 分钟
获赞
1
题目
P11824

版本

  1. v1当前

感觉只要想到静态 Top Tree 就会这个题了。

我们可以默认所有方案的贡献都为 nn,然后枚举 i=1…n−1i = 1\dots n - 1,给所有只选择权值 ≤i\le i 的点的方案贡献减一。然后换根求出包含每个点的独立集个数,这样可以做到 55pts\mathtt {55pts}。

建立静态 Top Tree,设 fas(p,0/1,0/1)fas(p, 0/1, 0/1) 表示 Top Tree 上的点 xx 的上界点是/否选择,下界点是/否选择,此时树簇 pp 内所有点的独立集方案数。

每次给当前所有合法独立集增加贡献 vv 时,我们考虑在 Top Tree 上每个点 pp 维护一个标记 tag(p,0/1,0/1)tag(p, 0/1, 0/1) 表示 xx 的上界点是/否选择,下界点是/否选择,此时所有贡献乘上对应树簇外方案数的总和。

设 pp 的左右儿子为 lcplc_p 和 rcprc_p。以 compress 为例,我们给点 pp 下传标记的时候,设上、下界点的选择状态为 ii 和 jj,枚举中间界点的选择状态 kk,更新 tag(lcp,i,k)←+tag(p,i,j)×fas(rcp,k,j)tag(lc_p, i, k) \xleftarrow{+} tag(p, i, j)\times fas(rc_p, k, j),右儿子同理。

从大到小枚举值域的过程中,我们会修改若干个点强制不能被选,每次修改时先将路径上的点都 pushdown,修改后往上 pushup 更新 fas 的值。

时间复杂度 O(nlog⁡n)\mathcal O(n\log n)。

C++
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define fi first
#define se second
#define mkp make_pair
#define pir pair <ll, ll>
#define pb push_back
#define i128 __int128
using namespace std;
char buf[1 << 22], *p1, *p2;
// #define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, (1 << 22) - 10, stdin), p1 == p2)? EOF : *p1++)
template <class T>
const inline void rd(T &x) {
	char ch; bool neg = 0;
	while(!isdigit(ch = getchar()))
		if(ch == '-') neg = 1;
	x = ch - '0';
	while(isdigit(ch = getchar()))
		x = (x << 1) + (x << 3) + ch - '0';
	if(neg) x = -x;
}
const ll maxn = 6e5 + 10, inf = 1e18, mod = 998244353, iv = mod - mod / 2;
ll power(ll a, ll b = mod - 2, ll p = mod) {
	ll s = 1;
	while(b) {
		if(b & 1) s = 1ll * s * a % p;
		a = 1ll * a * a % p, b >>= 1;
	} return s;
}
template <class T, class _T>
const inline ll pls(const T x, const _T y) { return x + y >= mod? x + y - mod : x + y; }
template <class T, class _T>
const inline void add(T &x, const _T y) { x = x + y >= mod? x + y - mod : x + y; }
template <class T>
const inline void chkmax(T &x, const T y) { x = x < y? y : x; }
template <class T>
const inline void chkmin(T &x, const T y) { x = x < y? x : y; }

ll n, q, b[maxn];
vector <ll> to[maxn];

namespace Build_TopTree {
	ll tot, lc[maxn], rc[maxn], op[maxn];
	ll siz[maxn], son[maxn], nowid[maxn], fa[maxn], ft[maxn];
	vector <ll> nd, nds;
	void dfs1(ll u, ll f = 0) {
		siz[u] = 1;
		for(ll v: to[u])
			if(v ^ f) {
				dfs1(v, u), siz[u] += siz[v];
				if(siz[v] > siz[son[u]]) son[u] = v;
			}
	}
	ll build(ll l, ll r, ll o) {
		if(l == r) return nd[l];
		ll w = nds[r] - (l? nds[l - 1] : 0), lo = l, hi = r - 2;
		while(lo <= hi) {
			ll mid = lo + hi >> 1;
			if((nds[mid] - (l? nds[l - 1] : 0)) * 2 < w) lo = mid + 1;
			else hi = mid - 1;
		}
		ll x = lo, a = build(l, x, o), b = build(x + 1, r, o), id = ++tot;
		op[id] = o, ft[id] = ft[nd[o == 1? r : l]];
		return fa[lc[id] = a] = fa[rc[id] = b] = id;
	}
	void dfs2(ll u, ll f = 0, bool istp = true) {
		nowid[u] = ft[u] = u;
		for(ll v: to[u])
			if(v != f && v != son[u]) dfs2(v, u);
		if(son[u]) {
			dfs2(son[u], u, false);
			nd.clear(), nds.clear();
			nd.pb(nowid[son[u]]), nds.pb(1);
			for(ll v: to[u])
				if(v != son[u] && v != f)
					nd.pb(nowid[v]), nds.pb(siz[v]);
			for(ll i = 1; i < nds.size(); i++) nds[i] += nds[i - 1];
			nowid[son[u]] = build(0, nd.size() - 1, 2);
			if(istp) {
				nd.clear(), nds.clear();
				for(ll x = u; x; x = son[x])
					nd.pb(nowid[x]), nds.pb(siz[x] - siz[son[x]]);
				for(ll i = 1; i < nds.size(); i++) nds[i] += nds[i - 1];
				nowid[u] = build(0, nd.size() - 1, 1);
			}
		}
	}
} using namespace Build_TopTree;

ll tag[maxn][2][2], fas[maxn][2][2], a[maxn], rt;
vector <ll> vec[maxn];

namespace Pushdown {
	void pushdown_compress(ll x) {
		ll t = a[ft[lc[x]]];
		for(ll i = 0; i < 2; i++)
			for(ll j = 0; j < 2; j++)
				for(ll k = 0; k <= t; k++)
					add(tag[lc[x]][i][k], tag[x][i][j] * fas[rc[x]][k][j] %mod),
					add(tag[rc[x]][k][j], tag[x][i][j] * fas[lc[x]][i][k] %mod);
	}
	void pushdown_rake(ll x) {
		ll t = a[ft[rc[x]]];
		for(ll i = 0; i < 2; i++)
			for(ll j = 0; j < 2; j++) {
				add(tag[lc[x]][i][j], tag[x][i][j]
				 * (fas[rc[x]][i][0] + fas[rc[x]][i][1]) %mod);
				add(tag[rc[x]][i][0], tag[x][i][j] * fas[lc[x]][i][j] %mod);
				if(t) add(tag[rc[x]][i][1], tag[x][i][j] * fas[lc[x]][i][j] %mod);
			}
	}
	void pushdown(ll x) {
		if(op[x] == 1) pushdown_compress(x);
		else pushdown_rake(x);
		memset(tag[x], 0, sizeof tag[x]);
	}
} using namespace Pushdown;

namespace Pushup {
	void pushup_compress(ll x) {
		ll t = a[ft[lc[x]]];
		for(ll i = 0; i < 2; i++)
			for(ll j = 0; j < 2; j++)
				for(ll k = 0; k <= t; k++)
					add(fas[x][i][j], fas[lc[x]][i][k] * fas[rc[x]][k][j] %mod);
	}
	void pushup_rake(ll x) {
		ll t = a[ft[rc[x]]];
		for(ll i = 0; i < 2; i++)
			for(ll j = 0; j < 2; j++)
				for(ll k = 0; k < 2; k++)
					add(fas[x][i][j], fas[lc[x]][i][j] * fas[rc[x]][i][k] %mod);
	}
	void pushup(ll x) {
		memset(fas[x], 0, sizeof fas[x]);
		if(op[x] == 1) pushup_compress(x);
		else pushup_rake(x);
	}
} using namespace Pushup;


void modify(ll x) {
	static ll stk[maxn], top;
	top = 0;
	for(ll i = fa[x]; i; i = fa[i]) stk[++top] = i;
	while(top) pushdown(stk[top--]);
	a[x] = 0, fas[x][0][1] = 0;
	for(ll i = fa[x]; i; i = fa[i]) pushup(i);
}

int main() {
	rd(n); tot = n;
	for(ll i = 2, x; i <= n; i++) rd(x), to[x].pb(i);
	dfs1(1), dfs2(1); rt = nowid[1];
	for(ll i = 1; i <= n; i++) rd(b[i]), vec[b[i]].pb(i);
	tag[rt][0][0] = tag[rt][0][1] = n;
	for(ll i = 1; i <= n; i++)
		fas[i][0][1] = fas[i][1][0] = fas[i][0][0] = 1;
	for(ll i = 1; i <= n; i++) a[i] = 1;
	for(ll i = n + 1; i <= tot; i++) pushup(i);
	for(ll i = n; i > 1; i--) {
		for(ll j: vec[i]) modify(j);
		add(tag[rt][0][0], mod - 1);
		if(a[ft[rt]]) add(tag[rt][0][1], mod - 1);
	}
	for(ll i = tot; i > n; i--) pushdown(i);
	for(ll i = 1; i <= n; i++)
		printf("%lld ", tag[i][0][1]);
	return 0;
}
原文
存档评论1 条抓取于