Codeforces 538E Demiurges Play Again(博弈DP)
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)的更多相关文章
- Codeforces Round #222 (Div. 1) 博弈 + dp
一般这种要倒着来. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #def ...
- HDU 5623 KK's Number (博弈DP)
KK's Number 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/K Description Our lovely KK h ...
- 博弈dp 以I Love this Game! POJ - 1678 为例
写在前面的话 知识基础:一些基础的博弈论的方法,动态规划的一些知识 前言:博弈论就是一些关于策略或者游戏之间的最优解,动态规划就是对于一些状态之间转移的一些递推式(or 递归),dp分为很多很多种,比 ...
- 博弈dp入门 POJ - 1678 HDU - 4597
本来博弈还没怎么搞懂,又和dp搞上了,哇,这真是冰火两重天,爽哉妙哉. 我自己的理解就是,博弈dp有点像对抗搜索的意思,但并不是对抗搜索,因为它是像博弈一样,大多数以当前的操作者来dp,光想是想不通的 ...
- [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)
[Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...
- [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】
[CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...
- [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT)
[Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT) 题面 给出一个\(n\)个点\(m\)条边的有向图(可能有环),走每条边需要支付一个价格\(c_i ...
- Codeforces 455B A Lot of Games:博弈dp【多局游戏】
题目链接:http://codeforces.com/problemset/problem/455/B 题意: 给你n个字符串,然后进行k局游戏. 每局游戏开始有一个空串,然后双方轮流给这个串的末尾添 ...
- 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 ...
随机推荐
- 【HDOJ】3184 All Can Do
简单数学题. #include <cstdio> #include <cstring> #include <cstdlib> int main() { int t; ...
- virtualbox端口转发
端口转发:setting->network->adapter:attached to NAT.port forwarding rules->name protocol ...
- HDU_1230——火星A+B,加法进制问题
Problem Description 读入两个不超过25位的火星正整数A和B,计算A+B.需要注意的是:在火星上,整数不是单一进制的,第n位的进制就是第n个素数.例如:地球上的10进制数2,在火星上 ...
- HDU_2553——n皇后问题,作弊
在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上.你的任务是,对于给定的N,求出有多少种合法的放置方法. Inp ...
- RMI入门教程
一.什么是RMI Java远程方法调用,即Java RMI(Java Remote Method Invocation)是Java编程语言里,一种用于实现远程过程调用的应用程序编程接口.它使客户机上运 ...
- javascript算法挑战
1.翻转字符串算法挑战: 先把字符串转化成数组,再借助数组的reverse方法翻转数组顺序,最后把数组转化成字符串. 你的结果必须得是一个字符串 function reverseString(str) ...
- Java IntelliJ IDEA 不能显示项目里的文件结构解决办法
按下列步骤操作: 1. 关闭IDEA, 2.然后删除项目文件夹下的.idea文件夹 3.重新用IDEA工具打开项目
- Appium依据xpath获取控件实例随笔
如文章<Appium基于安卓的各种FindElement的控件定位方法实践>所述,Appium拥有众多获取控件的方法.当中一种就是依据控件所在页面的XPATH来定位控件. 本文就是尝试通过 ...
- android键盘事件
在main.xml文件中代码如下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...
- [Angular 2] Refactoring mutations to enforce immutable data in Angular 2
When a Todo property updates, you still must create a new Array of Todos and assign a new reference. ...