首页 技术 正文
技术 2022年11月23日
0 收藏 670 点赞 2,187 浏览 2401 个字

题目链接

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_backtemplate<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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,999
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,357
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,140
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848