题意

给出一棵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. [Python Study Notes]批量将ppt转换为pdf v1.0

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

  2. yum 安装 nfs,rpcbind 出现错误 libc.so.6(GLIBC_2.14)(64bit) is needed by

    错误信息: Running rpm_check_debugERROR with rpm_check_debug vs depsolve:libc.so.6(GLIBC_2.14)(64bit) is ...

  3. PHP系统左侧菜单栏的管理与实现

    在日常的开发工作中,面对后台的日益增长的业务,以及后期业务的迭代开发,通常会选择添加菜单栏的形式来扩充业务功能,同样日益增长的后台菜单选项也为我们后期的维护,产生了一定的困难性.为此我总结出自己关于左 ...

  4. C++ 函数模板“偏特化”

         模板是C++中很重要的一个特性,利用模板可以编写出类型无关的通用代码,极大的减少了代码量,提升工作效率.C++中包含类模板.函数模板,对于需要特殊处理的类型,可以通过特化的方式来实现特定类型 ...

  5. Mock摆脱后端拖拉(借鉴官网)(一)

    mock是一个模拟数据生成器,旨在帮助前端独立于后端进行开发,帮助编写单元测试.mock有如下功能 根据数据模板生成模板数据 模拟ajax请求,生成请求数据 基于html模板生成模拟数据 下载安装 n ...

  6. typeof操作符 返回值

    Type操作符 返回值 : 1undefined   这个未定义 2.boolean    这个为boolean类型 3.string      这个是字符串 4.number    这个就是数值 5 ...

  7. C语言_scanf()和getchar() 使用[粗俗易懂]

    原文地址:http://blog.csdn.net/hao5743/article/details/6939661/,以下是我重新整理的以下. 问题描述一:[分析scanf()和getchar()读取 ...

  8. Shell脚本的基本流程控制

    if else read -p '请输入分数:' score if [ $score -lt 60 ]; then echo '60分以下' elif  [ $score -lt 70 ]; then ...

  9. 在Vue2.0中集成UEditor 富文本编辑器

    在vue的'项目中遇到了需要使用富文本编辑器的需求,在github上看了很多vue封装的editor插件,很多对图片上传和视频上传的支持并不是很好,最终还是决定使用UEditor. 这类的文章网上有很 ...

  10. 针对Student表的DAO设计实例

    完整代码以及junit,mysql--connector包下载地址 : https://github.com/CasterWx/MyStudentDao 表信息: 代码: dao包----impl包- ...