题目链接

题目大意

给你一颗树,Alice在a点,Bob在b点,Alice最多走da步,Bob最多走db步,两人轮流走路。要你判断经过无数次追赶后,Alice是否可以追上Bob,两人进行的都是最优策略

题目思路

感觉也不特别像一个博弈,但是也不知道该划分到哪里了

这个题目没想象的那么难,首先如果a和b的距离小于da,那么肯定Alice胜,如果2da>=len,len代表树的直径,那么也是Alice胜。 前两个都是显然的。 还有一种如果2*da>=db,这个代表不存在A只差一步追上B的时候,B可以反向再跑一段很长的距离,使得A一步追不上B。那么也是Alice胜

代码

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#define fi first
#define se second
#define debug printf(" I am here\n");
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e5+5,inf=0x3f3f3f3f,mod=1e9+7;
const double eps=1e-10;
int n,a,b,da,db,dis[maxn];
int head[maxn],cnt;
struct node{
int to,next;
}e[maxn<<1];
void add(int u,int v){
e[++cnt]={v,head[u]};
head[u]=cnt;
}
void dfs(int son,int fa){
int cur=0;
for(int i=head[son];i;i=e[i].next){
if(e[i].to==fa) continue;
dis[e[i].to]=dis[son]+1;
dfs(e[i].to,son);
}
}
void init(){
cnt=0;
for(int i=1;i<=n;i++){
head[i]=dis[i]=0;
}
}
signed main(){
int _; scanf("%d", &_);
while(_--){
scanf("%d%d%d%d%d",&n,&a,&b,&da,&db);
init();
for(int i=1,u,v;i<=n-1;i++){
scanf("%d%d",&u,&v);
add(u,v),add(v,u);
}
dfs(a,-1);
bool flag=0;
if(da>=dis[b]){//第一次直接抓住Bob
flag=1;
}
int ma=0,v;
for(int i=1;i<=n;i++){//寻找最远的端点
if(dis[i]>ma){
ma=dis[i];
v=i;
}
}
for(int i=1;i<=n;i++){
dis[i]=0;
}
dfs(v,-1);
int len=0;
for(int i=1;i<=n;i++){
len=max(len,dis[i]);
//树的直径
}
if(2*da>=len||2*da>=db){
flag=1;
}
puts(flag?"Alice":"Bob");
}
return 0;
}

Codeforces Round #668 (Div. 2) D. Tree Tag 题解(博弈)的更多相关文章

  1. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  2. Codeforces Round #609 (Div. 2)前五题题解

    Codeforces Round #609 (Div. 2)前五题题解 补题补题…… C题写挂了好几个次,最后一题看了好久题解才懂……我太迟钝了…… 然后因为longlong调了半个小时…… A.Eq ...

  3. Codeforces Round #668 (Div. 2)【ABCD】

    比赛链接:https://codeforces.com/contest/1405 A. Permutation Forgery 题意 给出一个大小为 $n$ 的排列 $p$,定义 \begin{equ ...

  4. Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树

    题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...

  5. Codeforces Round #540 (Div. 3)--1118F1 - Tree Cutting (Easy Version)

    https://codeforces.com/contest/1118/problem/F1 #include<bits/stdc++.h> using namespace std; in ...

  6. Codeforces Round #353 (Div. 2) D. Tree Construction 模拟

    D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...

  7. Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】

    任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...

  8. Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost 【DFS换根 || 树形dp】

    传送门:http://codeforces.com/contest/1092/problem/F F. Tree with Maximum Cost time limit per test 2 sec ...

  9. 数据结构 - Codeforces Round #353 (Div. 2) D. Tree Construction

    Tree Construction Problem's Link ------------------------------------------------------------------- ...

随机推荐

  1. JUC---06线程间通信(二)

    二.线程间定制化调用通信 要使多线程之间按顺序调用,实现A->B->C按顺序输出,使用Lock锁实现,通过Lock锁创建三个Condition实例(三把钥匙),通过不同的条件,调用不同钥匙 ...

  2. Shell脚本实现----Kubernetes单集群二进制部署

     Shell脚本实现----Kubernetes单集群二进制部署   搭建Kubernetes集群环境有以下三种方式: 1. Minikube安装方式Minikube是一个工具,可以在本地快速运行一个 ...

  3. 直接理解转置卷积(Transposed convolution)的各种情况

    使用GAN生成图像必不可少的层就是上采样,其中最常用的就是转置卷积(Transposed Convolution).如果把卷积操作转换为矩阵乘法的形式,转置卷积实际上就是将其中的矩阵进行转置,从而产生 ...

  4. Java学习的第四十八天

    1.例8.4找出整形数组中的最大值 import java.util.Scanner; public class Cjava { public static void main(String[]arg ...

  5. linux查找大文件及详细问题

    查询大文件du -h --max-depth=1 查询指定目录下面的文件大小du -h --max-depth=1 /path 使用find命令查找大于200M文件 find / -type f -s ...

  6. iptables从入门到掌握

    防火墙简述 防火墙(firewall)一词本是建筑用于,本意是为了保护建筑物不受火灾侵害的.被借鉴到了在网络通信领域中,表示保护局域网或主机不受网络攻击的侵害. 防火墙: 工作在主机或者网络边缘,对于 ...

  7. SQL数据库表结构的修改(sql2005)

    一 .ALTER TABLE命令 ALTER TABLE 语句用于在已有的表中添加.修改或删除列. 二.添加列 语法 :ALTER TABLE table_name ADD column_name d ...

  8. Servlet基础使用总结

    Servlet通俗理解:主要功能在于交互式地浏览和生成数据,生成动态Web内容.Servlet运行于支持Java的应用服务器中.从原理上讲,Servlet可以响应任何类型的请求,但绝大多数情况下Ser ...

  9. 什么是麒麟(kylin)?查数据贼快的哟

    前言 微信搜[Java3y]关注这个有梦想的男人,点赞关注是对我最大的支持! 文本已收录至我的GitHub:https://github.com/ZhongFuCheng3y/3y,有300多篇原创文 ...

  10. VMware 虚拟机下CentOS 7连接网络

    查看主机网络配置 ipconfig 1.在centos打开终端命令 2. 输入命令 cd /etc/sysconfig/network-scripts/  进入文件 输入 ls查看此文件夹下的文件 3 ...