分析:显然的,树形dp,状态也很好想到:f[i][j]表示以i为根的子树收集到j个果子的方案数.转移的话就相当于是背包问题,每个子节点可以选或不选.如果不选子节点k的话,那么以k为根的子树的边无论断不断都没关系,贡献就是f[i][j] * 2^(size[k]).如果选的话,枚举一下收集到多少个果子,对答案的贡献就是f[i][j - p] * f[k][p].基本的计数原理.

不过这个转移是O(n^3)的,怎么优化呢?状态定义为这个样子是没法继续优化的,如果把状态的表示改成dfs到第i个点,收集到j个果子的方案数,就能够神奇地做到O(n^2)了.因为dfs是每次先向下递归,然后子节点向上回溯嘛,向下递归的时候就用父节点的状态去更新子节点的状态,向上回溯就用子节点的答案去更新父节点的答案.也就是说:向下走,更新状态;向上走,统计答案.

60分暴力:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std;
const long long mod = 1e9 + ;
typedef long long ll; ll n, g[], k, q[], sizee[], a[], f[][], head[], to[], nextt[], tot = ; void add(ll x, ll y)
{
to[tot] = y;
nextt[tot] = head[x];
head[x] = tot++;
} void dfs(ll u, ll fa)
{
f[u][a[u]] = ;
sizee[u] = ;
for (ll i = head[u]; i; i = nextt[i])
{
ll v = to[i];
if (v == fa)
continue;
dfs(v, u);
sizee[u] += sizee[v];
for (ll j = ; j <= k; j++)
{
g[j] = q[sizee[v] - ] * f[u][j] % mod;
for (ll kk = ; kk <= j; kk++)
{
g[j] += f[v][kk] * f[u][j - kk] % mod;
g[j] %= mod;
}
}
for (ll j = ; j <= k; j++)
f[u][j] = g[j];
}
} int main()
{
scanf("%lld%lld", &n, &k);
for (ll i = ; i <= n; i++)
scanf("%lld", &a[i]);
for (ll i = ; i < n; i++)
{
ll x, y;
scanf("%lld%lld", &x, &y);
add(x, y);
add(y, x);
}
q[] = ;
q[] = ;
for (ll i = ; i <= n; i++)
q[i] = q[i - ] * % mod;
dfs(, );
printf("%lld\n", f[][k]); return ;
}

AC:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> const int mod = 1e9 + ; using namespace std;
typedef long long ll;
ll n, k, a[],sizee[], q[],f[][], head[], to[], nextt[], tot = ; void add(int x, int y)
{
to[tot] = y;
nextt[tot] = head[x];
head[x] = tot++;
} void dfs(int u, int fa)
{
sizee[u] = ;
for (int i = head[u]; i; i = nextt[i])
{
int v = to[i];
if (v == fa)
continue;
for (int j = ; j + a[v] <= n; j++)
f[v][j + a[v]] = f[u][j];
dfs(v, u);
sizee[u] += sizee[v];
for (int j = ; j <= n; j++)
f[u][j] = (q[sizee[v] - ] * f[u][j] % mod + f[v][j]) % mod;
}
} int main()
{
scanf("%lld%lld", &n, &k);
for (int i = ; i <= n; i++)
scanf("%lld", &a[i]);
for (int i = ; i < n; i++)
{
ll x, y;
scanf("%lld%lld", &x, &y);
add(x, y);
add(y, x);
}
f[][a[]] = ;
q[] = ;
for (int i = ; i <= n; i++)
q[i] = q[i - ] * % mod;
dfs(, );
printf("%lld\n", f[][k]); return ;
}

noip模拟赛 收集果子的更多相关文章

  1. NOIP模拟赛20161022

    NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...

  2. contesthunter暑假NOIP模拟赛第一场题解

    contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...

  3. NOIP模拟赛 by hzwer

    2015年10月04日NOIP模拟赛 by hzwer    (这是小奇=> 小奇挖矿2(mining) [题目背景] 小奇飞船的钻头开启了无限耐久+精准采集模式!这次它要将原矿运到泛光之源的矿 ...

  4. 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程

    数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...

  5. 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...

  6. 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...

  7. 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...

  8. CH Round #58 - OrzCC杯noip模拟赛day2

    A:颜色问题 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2358%20-%20OrzCC杯noip模拟赛day2/颜色问题 题解:算一下每个仆人到它的目的地 ...

  9. CH Round #52 - Thinking Bear #1 (NOIP模拟赛)

    A.拆地毯 题目:http://www.contesthunter.org/contest/CH%20Round%20%2352%20-%20Thinking%20Bear%20%231%20(NOI ...

随机推荐

  1. sed与正则表达式

    行的开头(^) ^匹配每一行的开头 [root@sishen ~]# sed -n '/^103/ p ' employee.txt 103,Raj Reddy,Sysadmin 只有^出现在正则表达 ...

  2. Windows 7操作系统下PHP 7的安装与配置(图文详解)

    前提博客 Windows 7操作系统下Apache的安装与配置(图文详解) 从官网下载           PHP的官网 http://www.php.net/         特意,新建这么一个目录 ...

  3. AJPFX关于modifier总结

    修饰符总结 Modifiers        函数修饰符始终在返回值类型之前!!!        变量修饰符始终在变量类型之前!!!---------------------------------- ...

  4. struts 2.5 There is no Action mapped for namespace [/] and action name [user_find] associated with context path [/struts2_crm].

    遇到了这个错误. There is no Action mapped for namespace [/] and action name [user_find] associated with con ...

  5. nginx教程从入门到精通

    [转]nginx教程从入门到精通 nginx教程写了一段时间,无意中发现,nginx相关文章已经达到了近100篇了.觉得很有必要汇总到一起,它是我们运维生存时间的一片心血,他是学习nginx的同学必看 ...

  6. PostgreSQL学习手册(五) 函数和操作符

    PostgreSQL学习手册(五) 函数和操作符 一.逻辑操作符:    常用的逻辑操作符有:AND.OR和NOT.其语义与其它编程语言中的逻辑操作符完全相同. 二.比较操作符:    下面是Post ...

  7. 谈谈如何学习Linux操作系统

     献给初学者:为了能把这篇不错的文章分享给大家.所以请允许我暂时用原创的形式展现给大家. @hcy 更多资源:http://blog.sina.com.cn/iihcy 一. 选择适合自己的linux ...

  8. java实现的判断括号是否成对的代码,()[]{}都可以

    本来想找找现成的,去,都写的好复杂.自己写一个吧.挺有成就感.哈哈 package com.test.jiexi; import java.util.Stack; public class Check ...

  9. sql中递归查询

    with AA as ( select * from tb_ClientBranch_Category where BRANCH_MOM_NAME='北京易华录信息技术股份有限公司' union al ...

  10. Linux内核中TCP SACK机制远程DoS预警通告

    漏洞描述 2019年6月18日,RedHat官网发布报告:安全研究人员在Linux内核处理TCP SACK数据包模块中发现了三个漏洞,CVE编号为CVE-2019-11477.CVE-2019-114 ...