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 ...
随机推荐
- mongodb 数据操作(1)
切换/创建数据库 use test 添加数据db.student.save({name:"J33ack",age:25}) 查看数据库show dbs 删除当前数据库 db.dro ...
- python 写接口供外部调用
.py: import requests import urllib2 import commands import subprocess def check(): status, msg = com ...
- Dubbo架构
原文链接http://dubbo.apache.org 架构图 节点角色说明 节点 角色说明 Provider 暴露服务的服务提供方 Consumer 调用远程服务的服务消费方 Registry 服务 ...
- Mysql学习(三)之数据库管理工具Navicat
前言 mysql安装完后默认只有命令行工具,所以我们可以下载一些数据库管理工具Navicat Navicat使用 首先建立一个连接选择mysql,填写信息 发现多了一个localhost,双击,打开连 ...
- Gsview裁剪EPS文件
(1)菜单栏 “options--show bounding boxs” 选中. (2)打开eps图,然后File->PS TO EPS,不选择Automatically calculate ...
- U盘重装系统
一.准备工作 (1)8G以上空间的U盘一个: (2)将U盘制作好启动工具: 1.下载启动工具制作软件(常用的有:大白菜.电脑店.老毛桃.快启动等等一系列软件,直接百度这些软件的名称,或者百度U盘启动制 ...
- HTML 5的革新之一:语义化标签一节元素标签。
摘至于:<HTML 5的革新——语义化标签(一)> HTML 5的革新之一:语义化标签一节元素标签. 在HTML 5出来之前,我们用div来表示页面章节,但是这些div都没有实际意义.(即 ...
- 【项目构建工具】 Gradle笔记2
一.Gradle执行流程 1.Gradle的执行流程(生命周期)主要是三个阶段: 初始化阶段:解析整个工程中所有Project,构建所有的Project对应的project对象 配置阶段:解析所有的p ...
- NTL 库函数
NTL是一个高性能,可移植的C ++库,为任意长度的整数提供数据结构和算法; 可用于整数和有限域上的向量,矩阵和多项式; 可用于任意精度浮点运算. NTL为以下方面提供高质量的最先进算法实现: 任意长 ...
- python: 基本数据类型 与 内置函数 知识整理
列表 list.append(val) #末尾追加,直接改变无返回 list.inert(2,val) #插入到指定位置 list.extend(mylist1) #list会被改变 list2=li ...