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

题目大意:

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

思路:

先考虑最大的情况

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

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

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

然后最小的情况类似

 #include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
int tot,go[],next[],first[];
int n,f1[],f2[],pd[],son[],deep[];
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void insert(int x,int y){
tot++;
go[tot]=y;
next[tot]=first[x];
first[x]=tot;
}
void add(int x,int y){
insert(x,y);
insert(y,x);
}
void dfs(int x,int fa){
int pdd=;
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (pur==fa) continue;
pdd=;
deep[pur]=deep[x]+;
dfs(pur,x);
son[x]+=son[pur];
}
if (!pdd) son[x]=,pd[x]=;
}
void dfs1(int x,int fa){
if (pd[x]==) {
f1[x]=;
return;
}
if (deep[x]%){
f1[x]=0x7fffffff;
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (pur==fa) continue;
dfs1(pur,x);
f1[x]=std::min(f1[x],f1[pur]);
}
}else{
f1[x]=;
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (pur==fa) continue;
dfs1(pur,x);
f1[x]+=f1[pur];
}
}
}
void dfs2(int x,int fa){
if (pd[x]==) {
f2[x]=;
return;
}
if (deep[x]%){
f2[x]=;
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (pur==fa) continue;
dfs2(pur,x);
f2[x]+=f2[pur];
}
}else{
f2[x]=0x7fffffff;
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (pur==fa) continue;
dfs2(pur,x);
f2[x]=std::min(f2[x],f2[pur]);
}
}
}
int main(){
n=read();
for (int i=;i<n;i++){
int x=read(),y=read();
add(x,y);
}
deep[]=;
dfs(,);
dfs1(,);
printf("%d ",son[]-f1[]+);
dfs2(,);
printf("%d\n",f2[]);
return ;
}

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. BZOJ2079: [Poi2010]Guilds

    2079: [Poi2010]Guilds Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 237  Solved: 181[Submit][Statu ...

  2. Linux企业级项目实践之网络爬虫(28)——爬虫socket处理

    Socket是进程之间交换数据的机制.这些进程即可以是同一台机器上的,也可以是通过网络连接起来的不同机器.一旦一个Socket连接建立,那么数据就能够双向传输,直到其中一端关闭连接. 通常,请求数据的 ...

  3. 【转】VS2013中如何解决error C4996: 'fopen'问题

    原文网址:http://jingyan.baidu.com/article/ce436649fd61543773afd32e.html 今天编写控制台应用程序时出现如下错误 error C4996: ...

  4. SQL with as 替代临时表的用法

    原文地址:http://www.cnblogs.com/zerocc/archive/2011/11/28/2266180.html SQL中 WITH AS 的用法和注意事项 1.为什么使用with ...

  5. BNUOJ34990--Justice String (exkmp求最长公共前缀)

    Justice String Given two strings A and B, your task is to find a substring of A called justice strin ...

  6. (转)Apple Push Notification Services in iOS 6 Tutorial: Part 1/2

    转自:http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1 Upda ...

  7. [Angular 2] Transclusion in Angular 2

    Link: Blog Single transclude: <ng-content></ng-content> Multi-translcude: <ng-content ...

  8. 深入浅出 RPC - 深入篇

    <深入篇>我们主要围绕 RPC 的功能目标和实现考量去展开,一个基本的 RPC 框架应该提供什么功能,满足什么要求以及如何去实现它? RPC 功能目标 RPC 的主要功能目标是让构建分布式 ...

  9. textarea固定大小,不可拖动

    写前端,经常很多小东西容易忽略忘记,今天写页面碰到设定一个输入框大小,死活记不起怎么固定,故找了一下度娘,其实添加一个css属性就好了: resize: none; 随笔记一下!

  10. FreeMarker辅助

    /** * FreeMarker 辅助类 * @author Rubekid * */ public class FreeMarkerHelper { /** * 模板文件存放目录 */ privat ...