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 ...
随机推荐
- laravel5.6上传图片
第一种:修改config里边的filesystems.php文件,在disks中加入下列代码 'local' => [ 'driver' => 'local', 'root' => ...
- Res-Family: From ResNet to SE-ResNeXt
Res-Family: From ResNet to SE-ResNeXt 姚伟峰 http://www.cnblogs.com/Matrix_Yao/ Res-Family: From ResNet ...
- python day07笔记总结
2019.4.4 S21 day07笔记总结 一.深浅拷贝 1.copy.copy() 浅拷贝 deep.copy() 深拷贝 2.一般情况 1.str/int/bool 是不可变类型 ...
- hibernate的开始
1.1对象的持久化 对象持久化是指将内存中的对象保存到可永久保存的存储设备中(如磁盘)的一种技术.(hibernate是通过id来管理对象) 1.2怎样实现持久化 1 对象序列化 2 JDBC 3 O ...
- flask 需要下载的包
Flask 需要下载的包1.pip install flask2.pip install flask-script3.pip install flask-sqlalchemy4.pip install ...
- git clone失败
操作: $ git clone https://github.com/zjun615/DragListView.gitCloning into 'DragListView'...fatal: unab ...
- ajax 跳转页面时添加header
在页面中添加一个form表单隐藏域,表单的一个属性是需要传的token,然后在ajax的回调函数中提交这个表单.要把这个token添加到header中就需要重写一个过滤器filter继承org.spr ...
- Jboss项目部署出现java.lang.UnsupportedClassVersionError 问题的解决方法
出现java.lang.UnsupportedClassVersionError 错误的原因,是因为我们使用高版本的JDK编译的Java class文件试图在较低版本的JVM上运行,所报的错误. 解决 ...
- 从 PC 卸载 Office
https://support.office.com/zh-cn/article/%E4%BB%8E-PC-%E5%8D%B8%E8%BD%BD-Office-9dd49b83-264a-477a-8 ...
- LeetCode Smallest Range
数据范围是3500,3500也就是说n的平方是可以接受的.这里告诉你就是有序的,也就是在提醒你可能会是一个类似于二分的算法,所以的话其实基于这两个认识的话我们就可以利用一个枚举叫二分的算法来解决这道题 ...