ARC121F Logical Operations on Tree

ARC121F Logical Operations on Tree

下文叶子定义为树中最外层的点。

大大的性质:考虑当一个叶子为 11\lor 时,那么我们只要最后操作这个叶子,无论如何最后都可以得到 11

再思考合法树中叶子的其他情况:

  • 当叶子为 11\land 时,它头上的树合法。
  • 当叶子为 00\lor 时,它头上的树能够得到 11,即合法。
  • 当叶子为 00\land 时,它操作之前之后的树合法。

综上,只有 11\lor 比较特殊,存在即合法,其他情况都有“原树合法”的大前提。

注意到,出现 11\lor 这种叶子和头上树合法两种限制满足任意一种即合法。

故考虑容斥,统计没有出现 11\lor 这种叶子的树的数量以及这些树中是合法树的数,最后用总方案 - 没有出现 11\lor 这种叶子的树的数量 ++ 没有出现 11\lor 这种叶子的树且合法的树的数量得到答案,即为总方案 - 没有出现 11\lor 这种叶子且不合法的情况。

fuf_u 表示 uu 的子树中没有出现 11\lor 这种叶子的树的数量,gug_u 表示 uu 的子树中没有出现 11\lor 这种叶子的树且合法的树的数量。

初始化

fu=2,gu=1f_u=2,g_u=1

对于 ff,有转移

fufu(vsonu2×fvgv)f_u \leftarrow f_u\left(\prod_{v\in son_u} 2\times f_v-g_v\right)

22 是因为边 (u,v)(u,v)\land\lor 两种可能,减去 gvg_v 是为了去除子树合法且 (u,v)(u,v)\lor 的情况。

对于 gg,有转移

guguvsonufvg_u\leftarrow g_u\prod_{v\in son_u} f_v

注意到这里要求必为合法且无 11\lor,所以儿子的每种取值都有唯一对应,如果是 00,那么边必须为 \lor,如果子树是 11,那么边必须为 \land

最后答案就是

22n1f1+g12^{2n-1}-f_1+g_1

时间复杂度 O(n)\mathcal O(n)

#include<bits/stdc++.h>

using namespace std;

#define int long long
typedef long long ll;

#define ha putchar(' ')
#define he putchar('\n')

inline int read()
{
	int x = 0, f = 1;
	char c = getchar();
	while (c < '0' || c > '9')
	{
		if (c == '-')
			f = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9')
		x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
	return x * f;
}

inline void write(int x)
{
	if(x < 0)
	{
		putchar('-');
		x = -x;
	}
	if(x > 9)
		write(x / 10);
	putchar(x % 10 + 48);
}

const int _ = 1e5 + 10, mod = 998244353;

int n, f[_], g[_];

vector<int> d[_];

int qpow(int y)
{
	int res = 1, x = 2;
	while(y)
	{
		if(y & 1) res = res * x % mod;
		x = x * x % mod, y >>= 1;
	}
	return res;
}

void dfs(int u, int fa)
{
	f[u] = 2, g[u] = 1;
	for(int v : d[u]) if(v != fa)
	{
		dfs(v, u);
		f[u] = f[u] * ((f[v] << 1) % mod + mod - g[v]) % mod;
		g[u] = g[u] * f[v] % mod;
	}
}

signed main()
{
	n = read();
	for(int i = 1, u, v; i < n; ++i)
	{
		u = read(), v = read();
		d[u].push_back(v), d[v].push_back(u);
	}
	dfs(1, 0);
	write((qpow((n << 1) - 1) + mod - f[1] + g[1]) % mod);
	return 0;
}
posted @ 2022-07-25 16:41  蒟蒻orz  阅读(10)  评论(0)    收藏  举报  来源