http://codeforces.com/problemset/problem/538/E

题目大意:

给出一棵树,叶子节点上都有一个值,从1-m。有两个人交替从根选择道路,先手希望到达的叶子节点尽量大,后手希望到达的叶子节点尽量小,叶子节点的放置方案任意。两个人都足够聪明,能够得到的最大值和最小值分别是多少。

思路:

先考虑最大的情况

考虑dp[i]代表i这个节点能达到的最大的数字在这个子树中排第几。

如果当前是先手操作,那么他肯定会往最大的那个子树的方向走,即dp[u]=min(dp[v])

如果当前是后手操作,那么他肯定往最小的走,即dp[u]=Σdp[v],这样就走到了最差子树的最大数字去了。

然后最小的情况类似

  1. #include<cstdio>
  2. #include<cmath>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<iostream>
  6. int tot,go[],next[],first[];
  7. int n,f1[],f2[],pd[],son[],deep[];
  8. int read(){
  9. int t=,f=;char ch=getchar();
  10. while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
  11. while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
  12. return t*f;
  13. }
  14. void insert(int x,int y){
  15. tot++;
  16. go[tot]=y;
  17. next[tot]=first[x];
  18. first[x]=tot;
  19. }
  20. void add(int x,int y){
  21. insert(x,y);
  22. insert(y,x);
  23. }
  24. void dfs(int x,int fa){
  25. int pdd=;
  26. for (int i=first[x];i;i=next[i]){
  27. int pur=go[i];
  28. if (pur==fa) continue;
  29. pdd=;
  30. deep[pur]=deep[x]+;
  31. dfs(pur,x);
  32. son[x]+=son[pur];
  33. }
  34. if (!pdd) son[x]=,pd[x]=;
  35. }
  36. void dfs1(int x,int fa){
  37. if (pd[x]==) {
  38. f1[x]=;
  39. return;
  40. }
  41. if (deep[x]%){
  42. f1[x]=0x7fffffff;
  43. for (int i=first[x];i;i=next[i]){
  44. int pur=go[i];
  45. if (pur==fa) continue;
  46. dfs1(pur,x);
  47. f1[x]=std::min(f1[x],f1[pur]);
  48. }
  49. }else{
  50. f1[x]=;
  51. for (int i=first[x];i;i=next[i]){
  52. int pur=go[i];
  53. if (pur==fa) continue;
  54. dfs1(pur,x);
  55. f1[x]+=f1[pur];
  56. }
  57. }
  58. }
  59. void dfs2(int x,int fa){
  60. if (pd[x]==) {
  61. f2[x]=;
  62. return;
  63. }
  64. if (deep[x]%){
  65. f2[x]=;
  66. for (int i=first[x];i;i=next[i]){
  67. int pur=go[i];
  68. if (pur==fa) continue;
  69. dfs2(pur,x);
  70. f2[x]+=f2[pur];
  71. }
  72. }else{
  73. f2[x]=0x7fffffff;
  74. for (int i=first[x];i;i=next[i]){
  75. int pur=go[i];
  76. if (pur==fa) continue;
  77. dfs2(pur,x);
  78. f2[x]=std::min(f2[x],f2[pur]);
  79. }
  80. }
  81. }
  82. int main(){
  83. n=read();
  84. for (int i=;i<n;i++){
  85. int x=read(),y=read();
  86. add(x,y);
  87. }
  88. deep[]=;
  89. dfs(,);
  90. dfs1(,);
  91. printf("%d ",son[]-f1[]+);
  92. dfs2(,);
  93. printf("%d\n",f2[]);
  94. return ;
  95. }

Codeforces 538E Demiurges Play Again(博弈DP)的更多相关文章

  1. Codeforces Round #222 (Div. 1) 博弈 + dp

    一般这种要倒着来. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #def ...

  2. HDU 5623 KK's Number (博弈DP)

    KK's Number 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/K Description Our lovely KK h ...

  3. 博弈dp 以I Love this Game! POJ - 1678 为例

    写在前面的话 知识基础:一些基础的博弈论的方法,动态规划的一些知识 前言:博弈论就是一些关于策略或者游戏之间的最优解,动态规划就是对于一些状态之间转移的一些递推式(or 递归),dp分为很多很多种,比 ...

  4. 博弈dp入门 POJ - 1678 HDU - 4597

    本来博弈还没怎么搞懂,又和dp搞上了,哇,这真是冰火两重天,爽哉妙哉. 我自己的理解就是,博弈dp有点像对抗搜索的意思,但并不是对抗搜索,因为它是像博弈一样,大多数以当前的操作者来dp,光想是想不通的 ...

  5. [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)

    [Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...

  6. [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】

    [CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...

  7. [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT)

    [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT) 题面 给出一个\(n\)个点\(m\)条边的有向图(可能有环),走每条边需要支付一个价格\(c_i ...

  8. Codeforces 455B A Lot of Games:博弈dp【多局游戏】

    题目链接:http://codeforces.com/problemset/problem/455/B 题意: 给你n个字符串,然后进行k局游戏. 每局游戏开始有一个空串,然后双方轮流给这个串的末尾添 ...

  9. Codeforces 768 E. Game of Stones 博弈DP

    E. Game of Stones   Sam has been teaching Jon the Game of Stones to sharpen his mind and help him de ...

随机推荐

  1. 自动删除n天前日志

    自动删除n天前日志 linux 是一个很能自动产生文件的系统,日志.邮件.备份等.虽然现在硬盘廉价,我们可以有很多硬盘空间供这些文件浪费,让系统定时清理一些不需要的文件很有一种 爽快的事情.不用你去每 ...

  2. 【转】Beaglebone Black

    原文网址:http://bbs.eeworld.com.cn/thread-431409-1-1.html 开源硬件在国外火得一塌糊涂,国内却没有那么多人玩,直接导致中文论坛资料严重缺乏……但这也挡不 ...

  3. httpd与tomcat基于mod_jk整合

    搞定在前面述, httpd与tomcat整合方式 当前已知的有 ajp_proxy,mod_jk.so jk connecteor连接器下载地址 http://archive.apache.org/d ...

  4. Nginx缓存解决方案:SRCache

    前些天帮别人优化PHP程序,搞得灰头土脸,最后黔驴技穷开启了FastCGI Cache,算是勉强应付过去了吧.不过FastCGI Cache不支持分布式缓存,当服务器很多的时候,冗余的浪费将非常严重, ...

  5. 2017年开年的第一次比较大的安全事件: MongoDB “赎金事件”,如何看待互联网安全问题

    今天上午(2017年1月7日),我的微信群中同时出现了两个MongoDB被黑掉要赎金的情况,于是在调查过程中,发现了这个事件.这个事件应该是2017年开年的第一次比较大的安全事件吧,发现国内居然没有什 ...

  6. 【iOS基础】iOS 网络请求

    一.一个HTTP请求的基本要素1.请求URL:客户端通过哪个路径找到服务器 2.请求参数:客户端发送给服务器的数据* 比如登录时需要发送的用户名和密码 3.返回结果:服务器返回给客户端的数据* 一般是 ...

  7. 高性能WEB开发(11) - flush让页面分块,逐步呈现

    高性能WEB开发(11) - flush让页面分块,逐步呈现 在处理比較耗时的请求的时候,我们总希望先让用户先看到部分内容,让用户知道系统正在进行处理,而不是无响应.一般大家在处理这样的情况,都使用a ...

  8. FusionChart实现金字塔分布图

    1.XML提供数据源 Pyramid.xml: <?xml version="1.0" encoding="UTF-8"?> <chart m ...

  9. 使用断言assert

    之前有看过关于Assert的书,但是不懂得如何去用,最近看别人写的代码有用这个断言(assert),今天自己动手看看如何使用断言. 断言(assert)的语义如下:如果表达式的值为0(假),则输出错误 ...

  10. android常用软件下载资源链接

    最新内容请看:http://www.androiddevtools.cn/ https://github.com/inferjay/AndroidDevTools 官方adt下载地址:http://d ...