ARC101E Ribbons on Tree 容斥原理+dp

题目链接

https://atcoder.jp/contests/arc101/tasks/arc101_c

题解

直接容斥。题目要求每一条边都被覆盖,那么我们就容斥至少有几条边没有被覆盖。

那么没有被覆盖的几条边一个可以把整棵树划分成很多连通块,每一块的贡献就是 \((siz-1)!!\)。(\(x!!=x(x-2)(x-4)\cdots\)

然后就可以 dp 了。

\(dp[x][i][j]\) 表示以 \(x\) 为根的子树内,\(x\) 位于一个大小为 \(i\) 的联通块,子树内有 \(j\) 条边没有被覆盖时,各种方案各个连通块的贡献之和。由于 \(x\) 所在的连通块还没有完结,所以 \(dp\) 数组不算上 \(x\) 所在连通块的贡献。(方案还是要算的)

然后就是一个基础的背包合并。

等一下,这个复杂度是?三维的 dp,要嵌套的枚举背包合并,复杂度大概是 \(O(n^4)\)

这个肯定凉透啊。

可以发现,最后容斥的时候,我们只关心 \(j\) 的奇偶性,不关心 \(j\) 到底是几。于是我们可以把 \(j\) 那一维缩减成 \(0/1\) 的状态。

于是就是一个常规的背包合并的复杂度了,\(O(n^2)\)

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b , 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b , 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I>
inline void read(I &x) {
	int f = 0, c;
	while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
	x = c & 15;
	while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
	f ? x = -x : 0;
}

const int N = 5000 + 7;
const int P = 1e9 + 7;

int n, m;
int siz[N], dp[N][N][2], f[N][2], ffac[N];

struct Edge { int to, ne; } g[N << 1]; int head[N], tot;
inline void addedge(int x, int y) { g[++tot].to = y, g[tot].ne = head[x], head[x] = tot; }
inline void adde(int x, int y) { addedge(x, y), addedge(y, x); }

inline int smod(int x) { return x >= P ? x - P : x; }
inline void sadd(int &x, int y) { x += y; x >= P ? x  -= P : x; }

inline void dfs(int x, int fa = 0) {
	siz[x] = 1, dp[x][1][0] = 1;
	for fec(i, x, y) if (y != fa) {
		dfs(y, x);
		for (int i = 1; i <= siz[x]; ++i)
			for (int j = 1; j <= siz[y]; ++j) {
				sadd(f[i + j][0], ((ll)dp[x][i][0] * dp[y][j][0] + (ll)dp[x][i][1] * dp[y][j][1]) % P);
				sadd(f[i + j][1], ((ll)dp[x][i][0] * dp[y][j][1] + (ll)dp[x][i][1] * dp[y][j][0]) % P);
				sadd(f[i][0], ((ll)dp[x][i][0] * dp[y][j][1] % P * ffac[j - 1] + (ll)dp[x][i][1] * dp[y][j][0] % P * ffac[j - 1]) % P);
				sadd(f[i][1], ((ll)dp[x][i][0] * dp[y][j][0] % P * ffac[j - 1] + (ll)dp[x][i][1] * dp[y][j][1] % P * ffac[j - 1]) % P);
			}
		siz[x] += siz[y];
		for (int i = 1; i <= siz[x]; ++i) dp[x][i][0] = f[i][0], dp[x][i][1] = f[i][1], f[i][0] = f[i][1] = 0;
	}
}

inline void work() {
	ffac[1] = 1;
	for (int i = 3; i <= n; i += 2) ffac[i] = (ll)ffac[i - 2] * i % P;
	dfs(1);
	int ans = 0;
	for (int i = 2; i <= n; i += 2) sadd(ans, (ll)(dp[1][i][0] - dp[1][i][1] + P) * ffac[i - 1] % P);
	printf("%d\n", ans);
}

inline void init() {
	read(n);
	for (int i = 1; i < n; ++i) {
		int x, y;
		read(x), read(y);
		adde(x, y);
	}
}

int main() {
#ifdef hzhkk
	freopen("hkk.in", "r", stdin);
#endif
	init();
	work();
	fclose(stdin), fclose(stdout);
	return 0;
}
posted @ 2019-09-06 19:44  hankeke303  阅读(288)  评论(0编辑  收藏  举报