当前位置:网站首页>P3047 [usaco12feb]nearby cows g (tree DP)
P3047 [usaco12feb]nearby cows g (tree DP)
2022-07-06 07:42:00 【eva_ can(not)survive】
[USACO12FEB]Nearby Cows G - Luogu https://www.luogu.com.cn/problem/P3047 A very interesting tree dp
We can set the State f【i】【j】, i Is the current root ,j The distance is j Point weight sum of time
First we can take 1 Run for the root dfs, Will be with i The point weight and record of the subtree for the root, that is, update the father with the son , here 1 It must be over , We can start from 1 Start running dfs Use the father to renew the son .
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
#include <bitset>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1e6 + 10;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false)
#define lowbit(x) ((x)&(-x))
using P = pair<int, int>;
int n, k;
int head[MAXN];
int ver[MAXN];
int nxt[MAXN];
int cost[MAXN];
int cnt;
int dep[MAXN];
int dp[MAXN][21];
void add(int x, int y) {
ver[++cnt] = y;
nxt[cnt] = head[x];
head[x] = cnt;
}
void dfs1(int p, int fa) {
for (int i = head[p]; i; i = nxt[i]) {
int v = ver[i];
if (v == fa)
continue;
// dep[v] = dep[p] + 1;
dfs1(v, p);
for (int j = 1; j <= k; j++) {
dp[p][j] += dp[v][j - 1];
}
}
}
void dfs2(int p, int fa) {
for (int i = head[p]; i; i = nxt[i]) {
int v = ver[i];
if (v == fa)
continue;
for (int j = k; j >= 2; j--) {
dp[v][j] += dp[p][j - 1] - dp[v][j - 2];
}
dp[v][1] += cost[p];
dfs2(v, p);
}
}
int main() {
scanf("%d %d", &n, &k);
int x, y;
for (int i = 1; i <= n - 1; i++) {
scanf("%d %d", &x, &y);
add(x, y);
add(y, x);
}
for (int i = 1; i <= n; i++) {
scanf("%d", cost + i);
dp[i][0] = cost[i];
}
dfs1(1, 0);
dfs2(1, 0);
for (int i = 1; i <= n; i++) {
int ans = 0;
for (int j = 0; j <= k; j++) {
ans += dp[i][j];
}
printf("%d\n", ans);
}
return 0;
}
边栏推荐
- onie支持pice硬盘
- 实现精细化生产, MES、APS、ERP必不可少
- 继电反馈PID控制器参数自整定
- Hackathon ifm
- word中把带有某个符号的行全部选中,更改为标题
- Full Score composition generator: living on code
- CF1036C Classy Numbers 题解
- Launch APS system to break the problem of decoupling material procurement plan from production practice
- Ble of Jerry [chapter]
- word中把帶有某個符號的行全部選中,更改為標題
猜你喜欢

861. Score after flipping the matrix

Related operations of Excel

jmeter性能测试步骤实战教程
TS 类型体操 之 extends,Equal,Alike 使用场景和实现对比

Apache middleware vulnerability recurrence

How to prevent Association in cross-border e-commerce multi account operations?

Qualitative risk analysis of Oracle project management system

Significance and measures of encryption protection for intelligent terminal equipment
![Ble of Jerry [chapter]](/img/d8/d080ccaa4ee530ed21d62755808724.png)
Ble of Jerry [chapter]

Simulation of Michelson interferometer based on MATLAB
随机推荐
esRally国内安装使用避坑指南-全网最新
The way to learn go (I) the basic introduction of go to the first HelloWorld
C # connect to SQLite database to read content
Sélectionnez toutes les lignes avec un symbole dans Word et changez - les en titre
[非线性控制理论]9_非线性控制理论串讲
Fundamentals of C language 9: Functions
When the Jericho development board is powered on, you can open the NRF app with your mobile phone [article]
2022年Instagram运营小技巧简单讲解
[KMP] template
Position() function in XPath uses
TS类型体操 之 字符串的妙用
Basics of reptile - Scratch reptile
Opencv learning notes 9 -- background modeling + optical flow estimation
Scala language learning-08-abstract classes
How can word delete English only and keep Chinese or delete Chinese and keep English
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Compliance and efficiency, accelerate the digital transformation of pharmaceutical enterprises, and create a new document resource center for pharmaceutical enterprises
Redis builds clusters
解决方案:智慧工地智能巡检方案视频监控系统
jmeter性能测试步骤实战教程