题意

给出一棵n个点的无根树,每个点有权值,问每个点向外不重复经过k条边的点权和


题解

设f[i][j]表示所有离i节点距离为j的点权和,v为它周围相邻的点,t为v的个数,则
j > 2 f[i][j] = (sigma f[v][j - 1]) - (t - 1) * f[i][j - 2]
j==2 f[i][j] = (sigma f[v][j - 1]) - t * f[i][j - 2]
枚举j,再Dfs即可。


常数巨大的丑陋代码

# include <stdio.h>
# include <stdlib.h>
# include <iostream>
# include <string.h>
# include <math.h>
using namespace std; # define IL inline
# define RG register
# define UN unsigned
# define ll long long
# define rep(i, a, b) for(RG int i = a; i <= b; i++)
# define per(i, a, b) for(RG int i = b; i >= a; i--)
# define uev(e, u) for(RG int e = ft[u]; e != -1; e = edge[e].nt)
# define mem(a, b) memset(a, b, sizeof(a))
# define max(a, b) ((a) > (b)) ? (a) : (b)
# define min(a, b) ((a) < (b)) ? (a) : (b) IL int Get(){
RG char c = '!'; RG int num = 0, z = 1;
while(c != '-' && (c > '9' || c < '0')) c = getchar();
if(c == '-') z = -1, c = getchar();
while(c >= '0' && c <= '9') num = num * 10 + c - '0', c = getchar();
return num * z;
} const int MAXN = 100001, INF = 2147483647;
struct Edge{
int to, nt;
} edge[MAXN << 1];
int n, cnt, ft[MAXN], k, f[MAXN][21], sum[MAXN]; IL void Add(RG int u, RG int v){
edge[cnt] = (Edge){v, ft[u]}; ft[u] = cnt++;
} IL void Dfs(RG int u, RG int fa, RG int d){
RG int t = 0;
uev(e, u){
RG int v = edge[e].to;
t++; f[u][d] += f[v][d - 1];
if(v == fa) continue;
Dfs(v, u, d);
}
if(d == 2) f[u][d] -= t * f[u][0];
if(d > 2) f[u][d] -= (t - 1) * f[u][d - 2];
sum[u] += f[u][d];
} int main(){
mem(ft, -1);
n = Get(); k = Get();
rep(i, 1, n - 1){
RG int u = Get(), v = Get();
Add(u, v); Add(v, u);
}
rep(i, 1, n) sum[i] = f[i][0] = Get();
rep(i, 1, k) Dfs(1, 0, i);
rep(i, 1, n) printf("%d\n", sum[i]);
return 0;
}

[USACO12FEB]Nearby Cows的更多相关文章

  1. 洛谷P3047 [USACO12FEB]Nearby Cows(树形dp)

    P3047 [USACO12FEB]附近的牛Nearby Cows 题目描述 Farmer John has noticed that his cows often move between near ...

  2. 解题:USACO12FEB Nearby Cows

    题面 比较简单的树形dp(递推?) 设$dp[i][j]$表示距离$i$距离为$j$的点的数目,先预处理$g[i][j]$表示点$i$的子树中距离这个点距离为$j$的点的数目(猫老师讲过,用一个栈维护 ...

  3. 树形DP【洛谷P3047】 [USACO12FEB]附近的牛Nearby Cows

    P3047 [USACO12FEB]附近的牛Nearby Cows 农民约翰已经注意到他的奶牛经常在附近的田野之间移动.考虑到这一点,他想在每一块土地上种上足够的草,不仅是为了最初在这片土地上的奶牛, ...

  4. 洛谷 P3047 [USACO12FEB]附近的牛Nearby Cows

    P3047 [USACO12FEB]附近的牛Nearby Cows 题目描述 Farmer John has noticed that his cows often move between near ...

  5. 【洛谷3047】[USACO12FEB]附近的牛Nearby Cows

    题面 题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into acc ...

  6. [USACO12FEB]附近的牛Nearby Cows

    题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into accoun ...

  7. 【题解】Luogu p3047 [USACO12FEB]附近的牛Nearby Cows 树型dp

    题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into accoun ...

  8. P3047 [USACO12FEB]附近的牛Nearby Cows

    https://www.luogu.org/problemnew/show/P304 1 #include <bits/stdc++.h> 2 #define up(i,l,r) for( ...

  9. 【[USACO12FEB]附近的牛Nearby Cows】

    我记得我调这道题时中耳炎,发烧,于是在学长的指导下过了也没有发题解 发现我自己的思路蛮鬼畜的 常规操作:\(f[i][j]\) 表示到\(i\)的距离为\(j\)的奶牛有多少只,但注意这只是在第二遍d ...

随机推荐

  1. Eclipse Java,debug模式无法调试,调试按钮不可用时解决办法

    经常出现debug模式进入后,debug的几个按钮置灰,F5,6,8,没有任何反应时,这样操作: 退出 Eclipse.打开 Eclipse 目录下的 configuration 下的 org.ecl ...

  2. 2n皇后问题

    此题为蓝桥杯基础练习题. 问题描述 给定一个n*n的棋盘,棋盘中有一些位置不能放皇后.现在要向棋盘中放入n个黑皇后和n个白皇后,使任意的两个黑皇后都不在同一行.同一列或同一条对角线上,任意的两个白皇后 ...

  3. golang验证提交的数据中某个字段是否重复

    提交的json数据如下: { , , , ", , , "screen_mode": "3,2", , "ad_plats":[ ...

  4. Ubuntu配置Nginx虚拟主机和支持ThinkPHP

    [Nginx配置虚拟主机] 每一个 server { listen       80;         server_name  www.a.com; ..... } 就表示一台虚拟域名, 然后对应的 ...

  5. NDK 开发中,各种指令集的坑,arm64

          最近在NDK开发中遇到了一个奇怪的问题,希望记录下,可以帮到大家:         我编译了一些 .so 动态库,只编译了armeabi-v7a.armeabi 指令集,其它指令集编译不了 ...

  6. C/C++语言简介之优缺点

    一.优点1.简洁紧凑.灵活方便 C语言一共只有32个关键字,9种控制语句,程序书写形式自由,区分大小写.把高级语言的基本结构和语句与低级语言的实用性结合起来.C 语言可以像汇编语言一样对位.字节和地址 ...

  7. Java文件复制与读写

    函数介绍 public String readLine():每次读取文件的一行,当文件读取完毕时,返回null     public int read(byte[] b):将文件内容读取到字节数组b ...

  8. uva437 DAG

    直接套用DAG的思路就行. AC代码: #include<cstdio> #include<cstring> #include<algorithm> using n ...

  9. float 与 display:inline-block

    float: 1.会导致高度塌陷 <style type="text/css"> li{ float:left; height:200px; width:200px; ...

  10. Fabric单节点安装备忘

    安装文档:http://www.cnblogs.com/studyzy/p/7437157.html 安装上面的文档安装成功,但是过程中遇到一些问题. 一.go的源码包可能下载不下来,因为被墙,go官 ...