解题:USACO12FEB Nearby Cows】的更多相关文章

P3047 [USACO12FEB]附近的牛Nearby Cows 题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into account, he wants to plant enough grass in each of his fields not only for the cows situated initially in that field, but…
题面 比较简单的树形dp(递推?) 设$dp[i][j]$表示距离$i$距离为$j$的点的数目,先预处理$g[i][j]$表示点$i$的子树中距离这个点距离为$j$的点的数目(猫老师讲过,用一个栈维护一下就好了),然后再预处理根节点,之后开始考虑dp. 当进入一个儿子时,首先这个儿子对于所有距离$dis$会继承距离父亲$dis-1$的点,然后是它的子树中距离它为$dis$的点,但是这样距离它为$dis-2$的点会被算重复,减掉就好了 #include<cstdio> #include<c…
题意 给出一棵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> # i…
P3047 [USACO12FEB]附近的牛Nearby Cows 农民约翰已经注意到他的奶牛经常在附近的田野之间移动.考虑到这一点,他想在每一块土地上种上足够的草,不仅是为了最初在这片土地上的奶牛,而且是为了从附近的田地里去吃草的奶牛. 具体来说,FJ的农场由N块田野构成(1 <= n <= 100,000),每两块田野之间有一条无向边连接(总共n-1条边).FJ设计了农场,任何两个田野i和j之间,有且只有一条路径连接i和j.第 i块田野是C(i)头牛的住所,尽管奶牛们有时会通过k条路到达其…
P3047 [USACO12FEB]附近的牛Nearby Cows 题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into account, he wants to plant enough grass in each of his fields not only for the cows situated initially in that field, but…
题面 题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into account, he wants to plant enough grass in each of his fields not only for the cows situated initially in that field, but also for cows visiting from nea…
题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into account, he wants to plant enough grass in each of his fields not only for the cows situated initially in that field, but also for cows visiting from nearby…
题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into account, he wants to plant enough grass in each of his fields not only for the cows situated initially in that field, but also for cows visiting from nearby…
传送门 解题思路 树形dp,看到数据范围应该能想到是O(nk)级别的算法,进而就可以设出dp状态,dp[x][j]表示以x为根的子树,距离它为i的点的总和,第一遍dp首先自底向上,dp出每个节点的子树中到他距离为j的,转移方程dp[x][j]=dp[u][j-1] ,第二遍dp自顶向下,dp出每个节点父亲那头的距离为j的,转移方程dp[u][j]+=dp[x][j-1]-dp[u][j-2] 代码 //dp[x][j] 以i为根的子树,距离为j的牛的和 //dp[x][j]+=dp[u][j-1…
https://www.luogu.org/problemnew/show/P304 1 #include <bits/stdc++.h> 2 #define up(i,l,r) for(register int i = (l); i <= (r); ++i) 3 #define dn(i,l,r) for(register int i = (l); i >= (r); --i) 4 #define ll long long #define re register using na…