Codeforces Round #668 (Div. 2) D. Tree Tag 题解(博弈)
题目链接
题目大意
给你一颗树,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 题解(博弈)的更多相关文章
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #609 (Div. 2)前五题题解
Codeforces Round #609 (Div. 2)前五题题解 补题补题…… C题写挂了好几个次,最后一题看了好久题解才懂……我太迟钝了…… 然后因为longlong调了半个小时…… A.Eq ...
- Codeforces Round #668 (Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1405 A. Permutation Forgery 题意 给出一个大小为 $n$ 的排列 $p$,定义 \begin{equ ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树
题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...
- 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 ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 模拟
D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...
- 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 ...
- 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 ...
- 数据结构 - Codeforces Round #353 (Div. 2) D. Tree Construction
Tree Construction Problem's Link ------------------------------------------------------------------- ...
随机推荐
- Spring入门-----------------属性注入和对象注入
属性注入即通过setter方法注入bean的属性或依赖对象. 属性注入使用<property>元素,使用name属性指定bean的属性的名称,value属性或<value>子节 ...
- CentOS 7 搭建 Ceph 集群(nautilus 版本)
搭建 Ceph 分布式集群( nautilus 版本 ) 一.服务器环境说明 主机名 角色 IP地址 ceph-admin ceph-deploy 192.168.92.21 ceph-node1 m ...
- 给 Mac 添加右键菜单「使用 VSCode 打开」
最终的实现效果是在文件 / 文件夹上右击时,会出现菜单项「用 VSCode 打开」,点击后会启动 Visual Studio Code 打开对应的文件 / 文件夹. 实现步骤 打开「自动操作.app」 ...
- 云计算管理平台之OpenStack计算服务nova
一.nova简介 nova是openstack中的计算服务,其主要作用是帮助我们在计算节点上管理虚拟机的核心服务:这里的计算节点就是指用于提供运行虚拟机实例的主机,通常像这种计算节点有很多台,那么虚拟 ...
- [Luogu P2824] [HEOI2016/TJOI2016]排序 (线段树+二分答案)
题面 传送门:https://www.luogu.org/problemnew/show/P2824 Solution 这题极其巧妙. 首先,如果直接做m次排序,显然会T得起飞. 注意一点:我们只需要 ...
- windows下redis的PHP扩展安装
1.查看已安装PHP的信息,打印phpinfo(); 主要看三个信息:PHP版本,是否线程安全(TS或NTS),系统是x64还是x86.用以确定扩展文件的版本. 2.需要php_redis.dll这个 ...
- (六)HTTP和HTTPS(转)
一.HTTP和HTTPS的基本概念 HTTP:用于从WWW服务器传输超文本到本地浏览器的传输协议,它可以使浏览器更加高效,使网络传输减少. HTTPS:是以安全为目标的HTTP通道,简单讲是HTTP的 ...
- 835. Image Overlap —— weekly contest 84
Image Overlap Two images A and B are given, represented as binary, square matrices of the same size. ...
- Docker(11)- docker ps 命令详解
如果你还想从头学起 Docker,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1870863.html 作用 列出容器 语法格式 doc ...
- 【Python】如何结束退出 py 脚本
需求 当你运行脚本,在判断条件满足时,就退出脚本,结束本次执行. 方法 使用 sys.exit(),直接退出程序,但是会引发一个 SystemExit 异常: 该方法包含一个 status 参数 sy ...