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 ...
随机推荐
- python并发编程-进程理论-进程方法-守护进程-互斥锁-01
操作系统发展史(主要的几个阶段) 初始系统 1946年第一台计算机诞生,采用手工操作的方式(用穿孔卡片操作) 同一个房间同一时刻只能运行一个程序,效率极低(操作一两个小时,CPU一两秒可能就运算完了) ...
- Arkady and a Nobody-men CodeForces - 860E (虚树)
大意: 给定有根树, 根节点深度为$1$. 定义$r(a,b)$为$b$子树内深度不超过$a$的节点数$-1$ 定义$z_a$为$a$的所有祖先的$r$之和. 对于所有点求出$z$的值. 一个点$y$ ...
- Transparency Tutorial with C# - Part 3
Download image fade demo - 4 Kb Download image fade source project- 7 Kb Download image fade images ...
- python网络爬虫(7)爬取静态数据详解
目的 爬取http://seputu.com/数据并存储csv文件 导入库 lxml用于解析解析网页HTML等源码,提取数据.一些参考:https://www.cnblogs.com/zhangxin ...
- springboot mongodb jpa常用方法整理
官方文档https://docs.spring.io/spring-data/data-mongodb/docs/current/reference/html/index.html 查询: ***** ...
- Kong/Konga - Docker容器化安装
1.0 安装kong + postgresDB docker network create kong-net docker pull postgres:latest docker run -d --n ...
- css设置全屏背景图,background-size 属性
在写主题样式的时候经常会碰到用背景图铺满整个背景的需求,这里分享下使用方法 需要的效果 图片以背景的形式铺满整个屏幕,不留空白区域 保持图像的纵横比(图片不变形) 图片居中 不出现滚动条 多浏览器支持 ...
- jQuery制作弹出窗(模态框)
来源:(二少)在南极 ##index.html <!DOCTYPE html><html lang="zh"><head> <meta c ...
- PostgreSQL 按照日期范围查询
method 1 select * from user_info where create_date >= '2019-05-01' and create_date < '2019-08- ...
- C#异步编程研究学习(一)
可以使用Func<T>或者Action<T>简单实现如: Func<string, string,string,string, int> func = new Fu ...