P3047 [USACO12FEB]附近的牛Nearby Cows
https://www.luogu.org/problemnew/show/P304 1 #include <bits/stdc++.h> 2 #define up(i,l,r) for(register int i = (l); i <= (r); ++i) 3 #define dn(i,l,r) for(register int i = (l); i >= (r); --i) 4 #define ll long long
#define re register
using namespace std; template <typename T> void in(T &x) {
x = ; T f = ; char ch = getchar();
while(!isdigit(ch)) {if(ch == '-') f = -; ch = getchar();}
while( isdigit(ch)) {x = * x + ch - ; ch = getchar();}
x *= f;
} template <typename T> void out(T x) {
if(x < ) x = -x , putchar('-');
if(x > ) out(x/);
putchar(x% + );
} int n,k;
const int N = ;
struct edge {
int v,nxt;
}e[N<<]; int tot,head[N];
void add(int u,int v) {e[++tot] = (edge){v,head[u]}; head[u] = tot;} int f[][N][],c[N]; void dfs1(int u,int fa) {
up(i,,k)
f[][u][i] = c[u];
for(re int i = head[u]; i ; i = e[i].nxt) {
int v = e[i].v;
if(v == fa) continue;
dfs1(v,u);
up(i,,k)
f[][u][i] += f[][v][i-];
}
} void dfs2(int u,int fa) { f[][u][] = c[u];
up(i,,k) f[][u][i] = f[][fa][i-] - f[][u][i-] + f[][u][i];
//别忘了+f[0][u][i]
for(re int i = head[u]; i ; i = e[i].nxt) {
int v = e[i].v;
if(v == fa) continue;
dfs2(v,u);
}
} int main() {
freopen("input.txt","r",stdin);
in(n); in(k);
int u,v;
up(i,,n-) {
in(u); in(v);
add(u,v); add(v,u);
}
up(i,,n) in(c[i]);
dfs1(,); up(i,,k)
f[][][i] = f[][][i-];
//必须给0号节点赋初值
dfs2(,); up(i,,n) out(f[][i][k]),putchar('\n');
return ;
}
P3047 [USACO12FEB]附近的牛Nearby Cows的更多相关文章
- 洛谷 P3047 [USACO12FEB]附近的牛Nearby Cows
P3047 [USACO12FEB]附近的牛Nearby Cows 题目描述 Farmer John has noticed that his cows often move between near ...
- 【题解】Luogu p3047 [USACO12FEB]附近的牛Nearby Cows 树型dp
题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into accoun ...
- LUOGU P3047 [USACO12FEB]附近的牛Nearby Cows
传送门 解题思路 树形dp,看到数据范围应该能想到是O(nk)级别的算法,进而就可以设出dp状态,dp[x][j]表示以x为根的子树,距离它为i的点的总和,第一遍dp首先自底向上,dp出每个节点的子树 ...
- 树形DP【洛谷P3047】 [USACO12FEB]附近的牛Nearby Cows
P3047 [USACO12FEB]附近的牛Nearby Cows 农民约翰已经注意到他的奶牛经常在附近的田野之间移动.考虑到这一点,他想在每一块土地上种上足够的草,不仅是为了最初在这片土地上的奶牛, ...
- 【洛谷3047】[USACO12FEB]附近的牛Nearby Cows
题面 题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into acc ...
- [USACO12FEB]附近的牛Nearby Cows
题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into accoun ...
- 【[USACO12FEB]附近的牛Nearby Cows】
我记得我调这道题时中耳炎,发烧,于是在学长的指导下过了也没有发题解 发现我自己的思路蛮鬼畜的 常规操作:\(f[i][j]\) 表示到\(i\)的距离为\(j\)的奶牛有多少只,但注意这只是在第二遍d ...
- [luoguP3047] [USACO12FEB]附近的牛Nearby Cows(DP)
传送门 dp[i][j][0] 表示点 i 在以 i 为根的子树中范围为 j 的解 dp[i][j][1] 表示点 i 在除去 以 i 为根的子树中范围为 j 的解 状态转移就很好写了 ——代码 #i ...
- luogu 3047 [USACO12FEB]附近的牛Nearby Cows 树形dp
$k$ 十分小,直接暴力维护 $1$~$k$ 的答案即可. 然后需要用父亲转移到儿子的方式转移一下. Code: #include <bits/stdc++.h> #define M 23 ...
随机推荐
- springboot + schedule
参考文章:https://blog.csdn.net/sinianliushui/article/details/78841713 参考文章: https://blog.csdn.net/hao703 ...
- WPF 学习笔记
依赖属性(Dependency Property) 相比较于普通属性,依赖属性有以下特点: 变化通知(change notification) 节省内存 使用某种带优先级决定策略(resolution ...
- go调查内存泄漏
curl x.x.x.x/debug/pprof/heap > base.heap 过段时间 curl x.x.x.x/debug/pprof/heap > current.heap go ...
- Kickstart自动化安装平台
PXE(preboot execute environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作站通过网络从远端服务器下载映像,并 ...
- 爬虫之Selenium 动态渲染页面爬取
Selenim 是一个自动化测试工具,可以利用它驱动浏览器执行特定的动作,如点击.下拉等操作,同时可以获取浏览器当前呈现的页面的源代码,做到可见及可爬 1.使用流程 1)声明浏览器对象 Seleniu ...
- pytest学习--快速入门
一.pytest简介 Pytest是python的一种单元测试框架. pytest的特点: 入门简单,文档丰富 支持单元测试,功能测试 支持参数化,重复执行,部分执行,测试跳过 兼容其他测试框架(no ...
- servlet的继承关系
一.servlet的继承关系 1.servlet程序是sun公司开发用于web资源技术,任何一个类只需要实现了servlet接口,那么就可以成为servlet程序 2.继承体系: ---------- ...
- Jenkins Maven Selenium TestNG踩坑记
1)Maven TestNG什么的都配置好了,在本地用eclipse->Run AS->MAVEN INSTALL运行正常 2) Jenkins安装了插件,也建立了MAVEN项目.MAVE ...
- DVWA中low级的sql注入漏洞的简单复现
第一次成功复现一个简单漏洞,于是写下这篇随笔记录一下 首先我们来看dvwa中low级的sql注入的源码 源码文件路径如下图: 源码如下: <?php if(isset($_GET['Submit ...
- Ubuntu---regex
摘要:今天我们学习正则表达式(regex),因为掌握这个是用好 grep 命令的前提条件. 1,位置相关的 patten: ^:表示锚定行首,才能匹配. $:表示锚定行尾,才能匹配. ^$:表示匹配空 ...