题意

给出一棵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. AnnotationUtils

    /** * 查询类中符合指定annotation的属性信息 * @param objCls 实体类 * @param annCls 注解类 * @return HashMap<实体属性名, An ...

  2. Windows使用问题总结

    1 电脑休眠恢复之后无法识别Wifi无线网络 首先,重启电脑:其次,打开网络和共享中心,点击更改适配器设置:最后,在对应的无线网络连接图标上点击鼠标右键,属性,配置,电源选项,允许计算机关闭此设备以节 ...

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

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

  4. mongodb的TTL索引介绍(超时索引)

    TTL索引是mongodb新支持的用于延时自动删除记录的一种索引.它仅包含一个字段,该字段值需要是Date()类型,并且不支持复合索引.可以指定某条记录在延时固定时间后自动删除.数据自动超时删除主要用 ...

  5. makefile讲解

    仅供自己学习使用 一.Makefile介绍 Makefile 或 makefile: 告诉make维护一个大型程序, 该做什么.Makefile说明了组成程序的各模块间的相互 关系及更新模块时必须进行 ...

  6. MysqL应该考虑到的安全策略

    1:使用预处理语句防止SQL注入2:写入数据库的数据要进行特殊字符的转义,比如字符中带单引号和双引号需要在应用层转义,这样为了防止SQL注入3:查询的错误信息不要返回给用户,将错误记录到日志.错误信息 ...

  7. C# 如何使用预处理指令?

    #define Debug #define Release #undef Release #if Debug using System; #endif 注意:#define 必须在文档最前面

  8. ImportError: No module named 'xlrd' 解决办法

    import pandas as pd data = pd.read_excel('工作簿1.xls',sheetname='Sheet1') 用pandas读取Excel文件时,会提示 Import ...

  9. Css中路径data:image/png;base64的用法详解

    今天查看一些网站的css中发现了 background-image:url(data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAEAAAAkCAYAAAB ...

  10. (十九)java小练习

    练习1:计算13-23+33-43+--+993-1003的结果     package demo; /**  * 计算13-23+33-43+--+993-1003的结果  * @author tu ...