当前位置:网站首页>P3047 [USACO12FEB]Nearby Cows G(树形dp)
P3047 [USACO12FEB]Nearby Cows G(树形dp)
2022-07-06 07:35:00 【eva_can(not)survive】
[USACO12FEB]Nearby Cows G - 洛谷https://www.luogu.com.cn/problem/P3047一个很有趣的树形dp
我们可以设状态f【i】【j】, i为当前根,j为距离为j时的点权和
首先我们可以取1为根跑一遍dfs,将以i为根的子树的点权和记录下即用儿子更新父亲,此时1肯定已经求完了,我们就可以再一次从1开始跑dfs用父亲去更新儿子。
#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;
}
边栏推荐
- Leecode-c language implementation -15 Sum of three ----- ideas to be improved
- Chrome view page FPS
- 杰理之BLE【篇】
- 【mysql学习笔记30】锁(非教程)
- If Jerry needs to send a large package, he needs to modify the MTU on the mobile terminal [article]
- 【mysql学习笔记29】触发器
- Scala language learning-08-abstract classes
- navicat如何导入MySQL脚本
- Force buckle day31
- OpenJudge NOI 2.1 1661:Bomb Game
猜你喜欢
Sharing of source code anti disclosure scheme under burning scenario
Go learning --- use reflection to judge whether the value is valid
【线上问题处理】因代码造成mysql表死锁的问题,如何杀掉对应的进程
Three no resumes in the software testing industry. What does the enterprise use to recruit you? Shichendahai's resume
NiO programming introduction
Force buckle day31
Do you really think binary search is easy
TS 类型体操 之 循环中的键值判断,as 关键字使用
杰理之BLE【篇】
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
随机推荐
杰理之BLE【篇】
Comparison of usage scenarios and implementations of extensions, equal, and like in TS type Gymnastics
Set picture annotation in markdown
SSM learning
Bloom taxonomy
TS类型体操 之 字符串的妙用
Oracle column to row -- a field is converted to multiple rows according to the specified separator
GET/POST/PUT/PATCH/DELETE含义
可变参数重载时的内存错误
Chrome view page FPS
Scala语言学习-08-抽象类
Supervisor usage document
Is the super browser a fingerprint browser? How to choose a good super browser?
When the Jericho development board is powered on, you can open the NRF app with your mobile phone [article]
In the era of digital economy, how to ensure security?
杰理之需要修改 gatt 的 profile 定义【篇】
上线APS系统,破除物料采购计划与生产实际脱钩的难题
TypeScript void 基础类型
[MySQL learning notes 32] mvcc
opencv学习笔记九--背景建模+光流估计