当前位置:网站首页>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;
}
边栏推荐
- [KMP] template
- Force buckle day31
- opencv学习笔记八--答题卡识别
- Ali's redis interview question is too difficult, isn't it? I was pressed on the ground and rubbed
- Pre knowledge reserve of TS type gymnastics to become an excellent TS gymnastics master
- Is the super browser a fingerprint browser? How to choose a good super browser?
- Typescript function definition
- Memory error during variable parameter overload
- Typescript interface properties
- 洛谷P1836 数页码 题解
猜你喜欢
word中如何删除某符号前面或后面所有的文字
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
解决方案:智慧工地智能巡檢方案視頻監控系統
Simulation of holographic interferogram and phase reconstruction of Fourier transform based on MATLAB
Basics of reptile - Scratch reptile
Sharing of source code anti disclosure scheme under burning scenario
How MySQL merges data
Jerry's ad series MIDI function description [chapter]
Ble of Jerry [chapter]
QT color is converted to string and uint
随机推荐
智能终端设备加密防护的意义和措施
How can word delete English only and keep Chinese or delete Chinese and keep English
Google可能在春节后回归中国市场。
In the era of digital economy, how to ensure security?
The difference between TS Gymnastics (cross operation) and interface inheritance
word中如何删除某符号前面或后面所有的文字
Relevant introduction of clip image
Solution: intelligent site intelligent inspection scheme video monitoring system
Type of data in energy dashboard
PHP Coding Standard
Three no resumes in the software testing industry. What does the enterprise use to recruit you? Shichendahai's resume
C # display the list control, select the file to obtain the file path and filter the file extension, and RichTextBox displays the data
MEX有关的学习
leecode-C语言实现-15. 三数之和------思路待改进版
软件开发的一点随记
P3047 [USACO12FEB]Nearby Cows G(树形dp)
Methods for JS object to obtain attributes (. And [] methods)
esRally国内安装使用避坑指南-全网最新
(lightoj - 1410) consistent verbs (thinking)
edge瀏覽器 路徑獲得