Atcoder ARC101 E 树dp
https://arc101.contest.atcoder.jp/tasks/arc101_c
题解是也是dp,好像是容斥做的,但是看不懂,而且也好像没讲怎么变n^2,看了写大佬的代码,自己理解了一下
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define SZ(X) (X.size())
#define mst(a,b) memset((a),(b),sizeof(a))
#define lowbit(a) ((a)&(-a))
using namespace std;
typedef unsigned long long ull;
typedef long long LL;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int mod=1e9+;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int maxn=;
inline int add(int x,int y){
if((x+=y)>=mod)x-=mod;
return x;
}
inline int mul(int x,int y){
return (ll)x*y%mod;
}
inline int sub(int x,int y){
if((x-=y)<)x+=mod;
return x;
}
int ci[maxn];
vector<int>to[maxn];
int dp[maxn][maxn];//dp[pos][i] 子树,有i个点未匹配的合法方案
//除了根之外的子树自匹配完是不合法的,所以-dp[pos][0]表示以pos为根的子树匹配完,但pos之下子树各自之间未匹配完
int sz[maxn],uu[maxn];
void dfs(int pos,int fa){
sz[pos]=;
dp[pos][]=;
for(int d:to[pos])if(d!=fa){
dfs(d,pos);
for(int i=;i<=sz[pos]+sz[d];++i)
uu[i]=;
for(int i=;i<=sz[pos];++i)
for(int j=;j<=sz[d];++j)
uu[i+j]=add(uu[i+j],mul(dp[pos][i],dp[d][j]));
sz[pos]+=sz[d];
for(int i=;i<=sz[pos];++i)
dp[pos][i]=uu[i];
}
for(int i=;i<=sz[pos];i+=)
dp[pos][]=sub(dp[pos][],mul(dp[pos][i],ci[i]));
}
int main() {
#ifdef local
freopen("in.txt", "r", stdin);
#endif // local
ios::sync_with_stdio();
cin.tie();cout.tie();
ci[]=;
for(int i=;i<maxn;i+=)
ci[i]=mul(ci[i-],i-);
int n;cin>>n;
for(int i=;i<n;++i){
int a,b;cin>>a>>b;
to[a].push_back(b);
to[b].push_back(a);
}
dfs(,);
cout<<(mod-dp[][]); return ;
}
Atcoder ARC101 E 树dp的更多相关文章
- CF456D A Lot of Games (字典树+DP)
D - A Lot of Games CF#260 Div2 D题 CF#260 Div1 B题 Codeforces Round #260 CF455B D. A Lot of Games time ...
- HDU4916 Count on the path(树dp??)
这道题的题意其实有点略晦涩,定义f(a,b)为 minimum of vertices not on the path between vertices a and b. 其实它加一个minimum ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- HDU4276 The Ghost Blows Light SPFA&&树dp
题目的介绍以及思路完全参考了下面的博客:http://blog.csdn.net/acm_cxlove/article/details/7964739 做这道题主要是为了加强自己对SPFA的代码的训练 ...
- Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)
[题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- bzoj 3572世界树 虚树+dp
题目大意: 给一棵树,每次给出一些关键点,对于树上每个点,被离它最近的关键点(距离相同被标号最小的)控制 求每个关键点控制多少个点 分析: 虚树+dp dp过程如下: 第一次dp,递归求出每个点子树中 ...
- bzoj 2286 [Sdoi2011]消耗战 虚树+dp
题目大意:多次给出关键点,求切断边使所有关键点与1断开的最小费用 分析:每次造出虚树,dp[i]表示将i和i子树与父亲断开费用 对于父亲x,儿子y ①y为关键点:\(dp[x]\)+=\(dismn( ...
- (纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland
Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes inpu ...
随机推荐
- CSS3面包屑菜单导航
在线演示 本地下载
- Jenkins的安装配置及使用
一.以Jenkins在tomcat容器里运行的方式,jenkins的安装及安装时所涉及的JDK和tomcat的配置 1.首先下载tomcat, 2.下载Jenkins.war包,将war包放在tomc ...
- Linux-1.3目录结构,基础命令
1.Linux目录结构 2.Linux基础命令(常用) ctrl+alt+T(打开终端) cd 切换文件夹(pwd查看当前目录) cd /home 绝对路径 以根目录开头 cd admin 相对路径 ...
- java -cp 执行jar包里的某个类
当我们需要测试某个功能时,需要在linux环境下执行某个类的main方法来测试 可以使用java -cp xxx.jar com.MyClass来执行某个类 java -cp test.jar:lib ...
- 简单搭建http服务器-HttpListener使用
使用HTTPListener可以简单搭建一个Http服务器,对于本地使用很是方面,想起之前使用了WebSocket来与本地网页通讯的例子,也是可以改为使用HTTPListener来做的.看下HTTPL ...
- C# 面向对象5 this关键字和析构函数
this关键字 1.代表当前类的对象 2.在类当中显示的调用本类的构造函数(避免代码的冗余) 语法: ":this" 以下一个参数的构造函数调用了参数最全的构造函数!并赋值了那些不 ...
- 权限(rwx)对于目录与文件的意义
1-权限对于目录的意义 首先要明白的是目录主要的内容是记录文件名列表和子目录列表,而不是实际存放数据的地方. r权限:拥有此权限表示可以读取目录结构列表,也就是说可以查看目录下的文件名和子目录名,注意 ...
- this(ES6箭头函数里的this)
一,了解前须知 1,箭头函数:出现的作用除了让函数的书写变得很简洁,可读性很好外:最大的优点是解决了this执行环境所造成的一些问题.比如:解决了匿名函数this指向的问题(匿名函数的执行环境具有全局 ...
- DedeAMPZ 网吧能安装却不能打开网站
只需把 监听IP的连接里的 LMHOSTS查询 禁用就行了. 方法: 连接属性-->TCP/IP 协议属性-->WINS 选项卡-->去掉 启用 LMHOSTS查询 前面的勾. by ...
- Python、mysql四-1:单表查询
一 单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 二 关键 ...