首页 > 其他分享 >P3047 [USACO12FEB] Nearby Cows G

P3047 [USACO12FEB] Nearby Cows G

时间:2023-07-06 21:35:23浏览次数:47  
标签:DP1 USACO12FEB int back P3047 push Cows Nearby

#include <iostream>
#include <vector>
using namespace std;
const int N = 100010,M = 30;
int n,m;
int w[N];
vector <int> g[N];
int f[N][M],ans[N][M];
void DP1 (int u,int fa) {
	for (int i = 0;i <= m;i++) f[u][i] = w[u];
	for (int x : g[u]) {
		if (x == fa) continue;
		DP1 (x,u);
		for (int i = 1;i <= m;i++) f[u][i] += f[x][i - 1];
	}
}
void DP2 (int u,int fa) {
	for (int x : g[u]) {
		if (x == fa) continue;
		ans[x][1] += w[u];
		for (int i = 2;i <= m;i++) ans[x][i] += ans[u][i - 1] - f[x][i - 2];  //利用上一层节点的答案减去重复的子树x
		DP2 (x,u);
	}
}
int main () {
	cin >> n >> m;
	for (int i = 1;i <= n - 1;i++) {
		int a,b;
		cin >> a >> b;
		g[a].push_back (b),g[b].push_back (a);
	}
	for (int i = 1;i <= n;i++) cin >> w[i];
	DP1 (1,-1);
	for (int i = 1;i <= n;i++) {
		for (int j = 0;j <= m;j++) ans[i][j] = f[i][j];
	}
	DP2 (1,-1);
	for (int i = 1;i <= n;i++) cout << ans[i][m] << endl;
	return 0;
}

标签:DP1,USACO12FEB,int,back,P3047,push,Cows,Nearby
From: https://www.cnblogs.com/incra/p/17533389.html

相关文章

  • [USACO06FEB]Treats for the Cows G/S
    [USACO06FEB]TreatsfortheCowsG/S题目描述FJhaspurchasedN(1<=N<=2000)yummytreatsforthecowswhogetmoneyforgivingvastamountsofmilk.FJsellsonetreatperdayandwantstomaximizethemoneyhereceivesoveragivenperiodtime.Th......
  • Codeforces Round #225 (Div. 2)-C. Milking cows
    原题链接C.Milkingcowstimelimitpertestmemorylimitpertestinputoutputn cowssittinginarow,numberedfrom 1 to nIahubcandecidetheorderinwhichhemilksthecows.Buthemustmilkeachcowex......
  • (输出路径搜索)[USACO06OCT] Cows on Skates G
    题目描述本题使用SpecialJudge。FarmerJohn把农场划分为了一个 r 行 c 列的矩阵,并发现奶牛们无法通过其中一些区域。此刻,Bessie位于坐标为 (1,1)(1,1) 的区域,并想到坐标为 (,)(r,c) 的牛棚享用晚餐。她知道,以她所在的区域为起点,每次移动至相邻的四个区域之一,总有......
  • P1676 [USACO05FEB] Aggressive cows G 题解
    题目传送门解题思路最大值最小化问题,考虑二分答案。首先要排序,保证序列单调不降,然后求出两个隔间之间的距离。sort(a+1,a+1+n);for(rii=1;i<=n;i++) dis[i]=a[i+1]-a[i];二分出一个\(mid\),判断它是否合法:每次累加距离,如果距离和比\(mid\)大,说明当前可以分配牛,记录数量......
  • NC51101 Lost Cows
    题目链接题目题目描述\(N(2\leqN\leq8,000)\)cowshaveuniquebrandsintherange1..N.Inaspectaculardisplayofpoorjudgment,theyvisitedtheneighborhood'wateringhole'anddrankafewtoomanybeersbeforedinner.Whenitwastimetoli......
  • Codeforces Round #225 (Div. 2) C. Milking cows Greedy
    Iahubhelpshisgrandfatheratthefarm.Todayhemustmilkthecows.Therearencowssittinginarow,numberedfrom1tonfromlefttoright.Eachcowiseitherfacingtotheleftorfacingtotheright.WhenIahubmilksacow,allthecowsthatseet......
  • POJ - 3186 Treats for the Cows(DP)
    题目大意:给你一个数组,每次你可以取两个数中的一个进行操作,要么取数组的第一个,要么数组的最后一个(取完之后,该数删除)假设取出来的数组组成了A现在要求使Sum=A[1]*1+A[2]*2+A[3]*3…+A[n]*n达到最大解题思路:用dp[i][j]表示前面取了i个,后面取了j个最大值则转......
  • J - Til the Cows Come Home
    J-TiltheCowsComeHomeBessieisoutinthefieldandwantstogetbacktothebarntogetasmuchsleepaspossiblebeforeFarmerJohnwakesherforthem......
  • Popular Cows
    PopularCowshttp://poj.org/problem?id=2186 思路涉及到有向图的强连通子图检测,参考:https://oi-wiki.org/graph/scc/https://www.cnblogs.com/zwfymqz/p/6693881.h......
  • Til the Cows Come Home ( POJ 2387) (简单最短路 Dijkstra)
    problemBessieisoutinthefieldandwantstogetbacktothebarntogetasmuchsleepaspossiblebeforeFarmerJohnwakesherforthemorningmilking.......