题目链接

ARC101E - Ribbons on Tree

题解

令边集\(S \subseteq E\) 设\(f(S)\)为边集S中没有边被染色的方案数

容斥一下,那么\(ans = \sum_{S \subseteq E} (-1)^{ \| S\| f(S) }\)

那么如何求对于原边集的\(f(S)\),也就是把\(S\)集合中的边全部删掉之后的各联通块内匹配的乘积

设\(g(x)\)为大小为x的联通块内点两两匹配的方案

那么\(f(S)=\prod_{i=1}^{|S|+1}g(a_i)\)

考虑如何求ans

设\(dp[i][j]\)表示以i为跟的子树中,有j各点没有在子树种匹配(链接到父节点

转移背包一下

对于\(j=0\)的时候由于那么i节点到父亲的边是没有覆盖的,容斥系数要取反

那么

$ f[i][0]=\sum_{j=1}^{sz[i]}-1\times f[i][j]\times g(j) $

代码


#include<cstdio>
#include<cstring>
#include<algorithm>
#define rep(p,x,k) for(int p = x;p <= k;++ p)
#define per(p,x,k) for(int p = x;p >= k;-- p)
#define gc getchar()
#define pc putchar
#define LL long long
#define int long long
inline LL read() {
LL x = 0,f = 1;
char c = gc;
while(c < '0' || c > '9') c = gc;
while(c <= '9' && c >= '0') x = x * 10 + c -'0',c = gc;
return x ;
}
void print(LL x) {
if(x < 0) {
pc('-');
x = -x;
}
if(x >= 10) print(x / 10);
pc(x % 10 + '0');
}
const int maxn = 5007;
const int mod = 1e9 + 7;
int a[maxn];
int n;
struct node {
int v,next;
} edge[maxn << 1];
int num = 0,head[maxn];
inline void add_edge(int u,int v) {
edge[++ num].v = v; edge[num].next = head[u];head[u] = num;
}
int g[maxn];
int dp[maxn][maxn];
int siz[maxn];
void dfs(int x,int fa) {
static int t[maxn];
dp[x][1] = 1; siz[x] = 1;
for(int i = head[x];i;i = edge[i].next) {
int v = edge[i].v;
if(v == fa) continue;
dfs(v,x);
for(int j = 0,kel = siz[v];j <= siz[x];++ j) {
for(int k = 0;k <= siz[v];++ k) {
(t[j + k] += 1ll * dp[x][j] * dp[v][k] % mod) %= mod;
}
}
siz[x] += siz[v];
for(int j = 0;j <= siz[x];++ j) dp[x][j] = t[j],t[j] = 0;
}
LL sum = 0;
for(int i = 0;i <= siz[x];i += 2) sum += mod - 1ll * dp[x][i] * g[i] % mod;
dp[x][0] = sum % mod;
}
main() {
n = read();
int u,v;
rep(i, 1,n - 1) {
u = read(),v = read();
add_edge(u,v);
add_edge(v,u);
}
g[0] = 1;
for(int i = 2;i <= n;i += 2) (g[i] = 1ll * g[i - 2] * (i - 1)) %= mod;
dfs(1,1);
print((mod - dp[1][0]) % mod);
return 0;
}
/*
4
1 2
1 3
1 4
*/

ARC101E - Ribbons on Tree的更多相关文章

  1. [ARC101E]Ribbons on Tree(容斥,dp)

    Description 给定一棵有 \(n\) 个节点的树,满足 \(n\) 为偶数.初始时,每条边都为白色. 现在请你将这些点两两配对成 \(\frac{n}{2}\) 个无序点对.每个点对之间的的 ...

  2. ARC101E Ribbons on Tree 容斥原理+dp

    题目链接 https://atcoder.jp/contests/arc101/tasks/arc101_c 题解 直接容斥.题目要求每一条边都被覆盖,那么我们就容斥至少有几条边没有被覆盖. 那么没有 ...

  3. Atcoder ARC101 Ribbons on Tree

    题解: 前面牛客网的那个比赛也有一道容斥+dp 两道感觉都挺不错的 比较容易想到的是 f[i][j]表示枚举到了i点,子树中有j个未匹配 这样的话我们需要枚举儿子中匹配状态 这样是n^2的(这是个经典 ...

  4. ARC 101E.Ribbons on Tree(容斥 DP 树形背包)

    题目链接 \(Description\) 给定一棵\(n\)个点的树.将这\(n\)个点两两配对,并对每一对点的最短路径染色.求有多少种配对方案使得所有边都至少被染色一次. \(n\leq5000\) ...

  5. [atARC101E]Ribbons on Tree

    令$f(E')$表示强制$E'$中的边不被覆盖的方案数,根据容斥,$ans=\sum_{E'\subseteq E}(-1)^{|E'|}f(E')$ 对于给定的$E'$,$f(E')$即将$E'$中 ...

  6. [ARC101C] Ribbons on Tree

    神仙的容斥题与神仙的树形DP题. 首先搞一个指数级的做法:求总的.能够覆盖每一条边的方案数,通过容斥可以得到\(\text{ans}=\sum\limits_E{(-1)^{|E|}F(E)}\).其 ...

  7. Solution -「ARC 101E」「AT 4352」Ribbons on Tree

    \(\mathcal{Description}\)   Link.   给定一棵 \(n\) 个点的树,其中 \(2|n\),你需要把这些点两两配对,并把每对点间的路径染色.求使得所有边被染色的方案数 ...

  8. Atcoder 乱做

    最近感觉自己思维僵化,啥都不会做了-- ARC103 F Distance Sums 题意 给定第 \(i\) 个点到所有点的距离和 \(D_i\) ,要求构造一棵合法的树.满足第 \(i\) 个点到 ...

  9. 【AtCoder】ARC101题解

    C - Candles 题解 点燃的一定是连续的一段,枚举左端点即可 代码 #include <bits/stdc++.h> #define enter putchar('\n') #de ...

随机推荐

  1. python网络爬虫笔记(四)

    一.python中的高阶函数算法 1.sorted()函数的排序   sorted()函数是一个高阶函数,还可以接受一个key函数来实现自定义的函数排序,key指定的函数作用于每个序列元素上,并根据k ...

  2. bzoj 2150

    然后考虑正解 我们发现,最坏情况就是每个点都派驻军队,所以答案至多是“.”的数目 而且,每个点都至多只有一个入度和一个出度,所以我们可以将每个点拆成两个点,一个作为入点,一个作为出点,然后所有图上能到 ...

  3. vue 的router的简易运用

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Duplicate 复制数据库 搭建Dataguard

    1 操作系统环境 此处隐藏具体信息 System IP-address db_name db_version Comment         Target DB         Auxiliary D ...

  5. python爬虫快递查询系统(源码)

    import requestsimport json def get_express_type(postid): '''根据快递单号来智能判断快递类型''' url = 'http://www.kua ...

  6. JAVA追加写入文本文件

    public void method1() { FileWriter fw = null; try { //如果文件存在,则追加内容:如果文件不存在,则创建文件 File f=new File(&qu ...

  7. Centos+Redis 集群

    Redis 3.2.6集群搭建 Redis3.0版本之后支持Cluster. 1.1.redis cluster的现状 目前redis支持的cluster特性: 1):节点自动发现 2):slave- ...

  8. gradle repo conf - maven-central.storage-download.googleapis.com

    repositories { google() jcenter() maven { // The google mirror is less flaky than mavenCentral() url ...

  9. gotty---用来作为k8s的web terminal,通过参数读取指定pod的日志输出

    不要重复造轮子,我觉得这个方案比较适合现在的情况. 我们知道,如果手工查看k8s里指定pod的日志输出,一般的方案如下: kubectl logs -f -n [namespace] [pod] -c ...

  10. eclipse工具下hadoop环境搭建

    eclipse工具下hadoop环境搭建:    window10操作系统中搭建eclipse64开发系统,配置hadoop的eclipse插件,让eclipse可以查看Hdfs中的文件内容.     ...