ImageVerifierCode 换一换
你正在下载:

树形dp.docx

[预览]
格式:DOCX , 页数:29 ,大小:26.56KB ,
资源ID:6655282      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/6655282.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(树形dp.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

树形dp.docx

1、树形dp树形dpAnniversary partyTime Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7954Accepted Submission(s): 3462Problem DescriptionThere is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchi

2、cal structure of employees. It means that the supervisor relation forms a tree rooted at the rector V. E. Tretyakov. In order to make the party funny for every one, the rector does not want both an employee and his or her immediate supervisor to be present. The personnel office has evaluated convivi

3、ality of each employee, so everyone has some number (rating) attached to him or her. Your task is to make a list of guests with the maximal possible sum of guests conviviality ratings.InputEmployees are numbered from 1 to N. A first line of input contains a number N. 1 = N = 6 000. Each of the subse

4、quent N lines contains the conviviality rating of the corresponding employee. Conviviality rating is an integer number in a range from -128 to 127. After that go T lines that describe a supervisor relation tree. Each line of the tree specification has the form:L KIt means that the K-th employee is a

5、n immediate supervisor of the L-th employee. Input is ended with the line0 0OutputOutput should contain the maximal sum of guests ratings.Sample Input711111111 32 36 47 44 53 50 0Sample Output5 题意:一个公司要举办party,不过每一个员工都不愿意和自己的上级在一起,因为这样自己会不开心,而每个人参加party都有一个开心的程度,同时给出来上下级之间的关系,让我们求最大的开心程度是多少。 这题用来树形d

6、p入门,首先就是如何来建树的问题和怎样dp的问题,我们直接用一个数组fi来表示第i个人的上司是谁,这样通过层层递进来找到最大的大boss,也就是一棵树的根,然后,用dfs的方式来进行层层搜索,同时dp。具体的dp过程也就是用dpi0表示如果这个人不来时候的最大开心程度,dpi1表示这个人来的时候的最大开心程度,如果树上面的父亲节点x不来,那么dpx0+=max(dpi0,dpi1);,如果树上的父亲节点x来(意味着子节点不会来),那么dpx1+=dpi0;#include #include #include #include#include#include#includeusing names

7、pace std;typedef long long ll;const int N=6010;int m,n;int dpN2,rdN;vector tuN;void dfs(int x) int l=tux.size(); for(int i=0;il;i+) int t=tuxi; dfs(t); dpx0+=max(dpt1,dpt0); dpx1+=dpt0; int main() while(scanf(%d,&n) for(int i=1;i=n;i+) tui.clear(); memset(rd,0,sizeof(rd); for(int i=1;i=n;i+) scanf(%

8、d,&dpi1); dpi0=0; int x,y; while(scanf(%d%d,&x,&y)&x+y) tuy.push_back(x),rdx+; int ans=-1; for(int i=1;i=n;i+) if(rdi=0)/防止不止一个根节点 dfs(i);ans=max(ans,max(dpi0,dpi1); coutansendl; The more, The BetterTime Limit: 6000/2000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6

9、868Accepted Submission(s): 4033Problem DescriptionACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物。但由于地理位置原因,有些城堡不能直接攻克,要攻克这些城堡必须先攻克其他某一个特定的城堡。你能帮ACboy算出要获得尽量多的宝物应该攻克哪M个城堡吗?Input每个测试实例首先包括2个整数,N,M.(1 = M = N = 0。当N = 0, M = 0输入结束。Output对于每个测试实例,输出一个整数,代表ACboy攻克M个城堡所获得的最多宝物的数量。Sam

10、ple Input3 20 10 20 37 42 20 10 42 17 17 62 20 0Sample Output513虽然是树形dp,可是其dp方式看起来有点像区间dp(总归来说,其实二维dp也就是这样的了。)这题的dp有点不好想,不过其实仔细看还是可以看懂的。用dpij表示第i个点攻克j个城堡获得的最大财富。若t是i的一个子节点,那么dpij=max(dpij,dpij-k+dptk);这个dp方程真心不好想。还有一点是我们要防止j大于了m,所以dfs里边加上一个变量,使最多只有m次。当然,我首先就把m+了,因为我引入了0这个节点来连接所有树的根节点,这样就做到了把森林连成了树!#

11、include #include #include #include#include#include#include#define m(a,b) memset(a,b,sizeof(a)using namespace std;typedef long long ll;const int N=250;int dpNN,visN;int m,n;vectortuN;void dfs(int x,int mm) visx=1; int l=tux.size(); for(int i=0;i=1;j-)/j的上下界比较好想,不过一定要注意j是从大到小在变化的。 for(int k=1;knm&m+n)

12、 m+; m(dp,0); m(tu,0); m(vis,0); for(int i=1;iab; tua.push_back(i); dpi1=b; dfs(0,m); printf(%dn,dp0m); return 0;F. Zublicanes and Mumocratestime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputIts election time in Berland. The favorites are of course

13、 parties of zublicanes and mumocrates. The election campaigns of both parties include numerous demonstrations onnmain squares of the capital of Berland. Each of thensquares certainly can have demonstrations of only one party, otherwise it could lead to riots. On the other hand, both parties have app

14、lied to host a huge number of demonstrations, so that on all squares demonstrations must be held. Now the capital management will distribute the area between the two parties.Some pairs of squares are connected by(n-1)bidirectional roads such that between any pair of squares there is a unique way to

15、get from one square to another. Some squares are on the outskirts of the capital meaning that they are connected by a road with only one other square, such squares are calleddead endsquares.The mayor of the capital instructed to distribute all the squares between the parties so that thedead endsquar

16、es had the same number of demonstrations of the first and the second party. It is guaranteed that the number of dead end squares of the city is even.To prevent possible conflicts between the zublicanes and the mumocrates it was decided to minimize the number of roads connecting the squares with the

17、distinct parties. You, as a developer of the department of distributing squares, should determine this smallest number.InputThe first line of the input contains a single integern(2n5000) the number of squares in the capital of Berland.Nextn-1lines contain the pairs of integersx,y(1x,yn,xy) the numbe

18、rs of the squares connected by the road. All squares are numbered with integers from1ton. It is guaranteed that the number of dead end squares of the city is even.OutputPrint a single number the minimum number of roads connecting the squares with demonstrations of different parties.Sample test(s)inp

19、ut81 42 43 46 57 58 54 5output1input51 21 31 41 5output2题意:给出一个树,要求最少删除多少条边之后使叶子节点恰好平分。思路:树形dp,用dpij表示节点i删除j个叶子节点所需要的最短的步数#include #include #include #include#include#include#includeusing namespace std;typedef long long ll;const int N = 5001,INF=999999999;vector tuN;int zN;/某个节点的叶子节点数int dpNN;/dpij表示

20、叶子节点i删除j个节点所需要的最短的步数int n;void dfs(int now,int pre) if(tunow.size()=1) znow=1; else znow=0; dpnow0=0; for(int i=1; i=n; i+) dpnowi=INF; int l=tunow.size(); for(int i=0; i=0; j-)/必须逆序 for(int k=0; k=znext; k+) dpnowj+k=min(dpnowj+k,dpnowj+dpnextk);/状态转移 znow+=znext; for(int i=0; i=znow; i+) dpnowi=mi

21、n(dpnowi,dpnowznow-i+1);/把now节点算进去,删除i个叶节点相当于一刀把now节点连接父亲的边删掉(所有叶节点删掉之后,相当于代码+1操作)再把znow-i个本来不打算删掉的叶节点删回来int main() scanf(%d,&n); if(n=2) puts(1); return 0; memset(dp,0,sizeof(dp); for(int i=0; in-1; i+) int a,b; scanf(%d%d,&a,&b); tua.push_back(b); tub.push_back(a); int root=1; while(turoot+.size()

22、=1); dfs(root,0); printf(%dn,dprootzroot/2);Mahjong treeTime Limit: 6000/3000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1660Accepted Submission(s): 521Problem DescriptionLittle sun is an artist. Today he is playing mahjong alone. He suddenly feels that the tree in

23、 the yard doesnt look good. So he wants to decorate the tree.(The tree has n vertexs, indexed from 1 to n.)Thought for a long time, finally he decides to use the mahjong to decorate the tree.His mahjong is strange because all of the mahjong tiles had a distinct index.(Little sun has only n mahjong t

24、iles, and the mahjong tiles indexed from 1 to n.)He put the mahjong tiles on the vertexs of the tree.As is known to all, little sun is an artist. So he want to decorate the tree as beautiful as possible.His decoration rules are as follows:(1)Place exact one mahjong tile on each vertex.(2)The mahjong

25、 tiles index must be continues which are placed on the son vertexs of a vertex.(3)The mahjong tiles index must be continues which are placed on the vertexs of any subtrees.Now he want to know that he can obtain how many different beautiful mahjong tree using these rules, because of the answer can be

26、 very large, you need output the answer modulo 1e9 + 7.InputThe first line of the input is a single integer T, indicates the number of test cases.For each test case, the first line contains an integers n. (1 = n = 100000)And the next n - 1 lines, each line contains two integers ui and vi, which desc

27、ribes an edge of the tree, and vertex 1 is the root of the tree.OutputFor each test case, output one line. The output format is Case #x: ans(without quotes), x is the case number, starting from 1.Sample Input292 13 14 35 36 27 48 79 382 13 14 35 16 47 58 4Sample OutputCase #1: 32Case #2: 16题意:有一棵有n个节点的树,有编号1n的n个数字要放到树上,且要满足三个要求:1.树的每个节点只放一个数字2.树的任意一个节点的所有直接孩子节点上面放的数字排序后是连续的3.一棵子树的所有节点上面放的数字排序后是连续的问有多少种不同的放法,结果取模1e9+7。思路:由于所有子树是连续的,所以可以用区间来表示子树,设要给当前子树编号为1,n,如果当前子树是原树,那么根有两种选择,分别是放头和尾(如果n等于1,那么头和尾重合了,也就是只有1种选择),如果不是原树,那么根的选择是唯

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1