【传送门】http://codeforces.com/problemset/problem/813/C

【题目大意】两个人玩游戏,一个人跑一个人追,轮流决策,可以走也可以不走。给你一棵树,想要从某个结点到达另一个结点当且仅当两个结点有边相连,A从根节点1号点出发,B从X号点出发,B要尽可能躲A,A要尽可能抓住A,最后两个人共决策了多少次。

【题解】可以知道这样一个事实,B一定会躲到离他最远的那个结点,然后A不管怎样决策B都躲在那里不动了。但是有一点需要注意,B需要比A先到达那个叶子结点。

所以问题转化成了分别求A,B到各个叶子结点的距离。选取一个A到达某一叶子结点的最远值,注意还必须满足这个最远值要大于B到达这个叶子结点的距离。

使用DFS对树进行搜索,直到叶子结点,每每遇见一个叶子结点就记下距离。起点分别设置为1和X,距离保存在两个数组中。

【AC代码】

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<vector>
#include<cstring>
#include<iomanip>
using namespace std;
typedef long long ll;
const int maxn = 2e5+;
vector<int> G[maxn];
ll d1[maxn];
ll d2[maxn]; void dfs(int st, int fr, ll step, ll d[]){
if(G[st].size() == && st != ){
d[st] = step;
//return ;//这里return会错误
} for(int i=; i<G[st].size(); i++){
if(G[st][i] != fr){
dfs(G[st][i] , st, step+, d);
}
}
} int main(){
int n,x;
int s,t;
while(scanf("%d%d",&n,&x) != EOF){
ll ans = ;
for(int i=; i<maxn; i++) G[i].clear();
memset(d1 , ,sizeof d1);
memset(d2 , ,sizeof d2);
while(scanf("%d%d",&s,&t) != EOF){
G[s].push_back(t);
G[t].push_back(s);
}
dfs(, -, , d1);
dfs(x, -, , d2); for(int i=; i<=n; i++){
if(d1[i] > d2[i] && G[i].size() == ){
ans = max(ans , *d1[i]);
}
}
printf("%lld\n",ans);
}
return ;
}

CodeForces - 813C The Tag Game (树的dfs遍历)的更多相关文章

  1. codeforces 813C The Tag Game 树+dfs追击问题

    C. The Tag Gametime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutpu ...

  2. Tree(树的还原以及树的dfs遍历)

    紫书:P155 uva  548   You are to determine the value of the leaf node in a given binary tree that is th ...

  3. CodeForces - 813C The Tag Game(拉格朗日乘数法,限制条件求最值)

    [传送门]http://codeforces.com/problemset/problem/813/C [题意]给定整数a,b,c,s,求使得  xa yb zc值最大的实数 x,y,z , 其中x ...

  4. Codeforces 813C The Tag Game (BFS最短路)

    <题目链接> 题目大意:A.B两人在一颗树上,A在根节点1上,B在节点x上,现在他们轮流走,每次只能走一步,或者不走.A以尽可能靠近B的方式行走,B以尽可能远离A的方式走,B先开始走.问你 ...

  5. codeforces div2_603 F. Economic Difficulties(树dfs预处理+dp)

    题目连接:http://codeforces.com/contest/1263/problem/F 题意:有n个设备,上和下分别连接着一颗树,上下两棵树每棵树的叶子节点连接一个设备,两棵树的根节点都是 ...

  6. CodeForces 343D 线段树维护dfs序

    给定一棵树,初始时树为空 操作1,往某个结点注水,那么该结点的子树都注满了水 操作2,将某个结点的水放空,那么该结点的父亲的水也就放空了 操作3,询问某个点是否有水 我们将树进行dfs, 生成in[u ...

  7. CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)

    The best programmers of Embezzland compete to develop a part of the project called "e-Governmen ...

  8. codeforces 1076E Vasya and a Tree 【dfs+树状数组】

    题目:戳这里 题意:给定有n个点的一棵树,顶点1为根.m次操作,每次都把以v为根,深度dep以内的子树中所有的顶点(包括v本身)加x.求出最后每个点的值为多少. 解题思路:考虑到每次都只对点及其子树操 ...

  9. CF877E Danil and a Part-time Job 线段树维护dfs序

    \(\color{#0066ff}{题目描述}\) 有一棵 n 个点的树,根结点为 1 号点,每个点的权值都是 1 或 0 共有 m 次操作,操作分为两种 get 询问一个点 x 的子树里有多少个 1 ...

随机推荐

  1. Pacman常用命令 文内搜索吧

    列出已经安装的软件包 https://wiki.archlinux.org/index.php/Pacman_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)  维基 pa ...

  2. java在线聊天项目 swt可视化窗口Design 重新设计好友列表窗口 增加菜单栏

    增加的菜单栏效果图如下: eclipse 中调整到 swt的design视图下 控件区域选择Menu Controls 将Menu Bar拖动到窗口标题栏 将Cascaded Menu拖动到Menu ...

  3. HashMap允许将null用作键 也允许将null作为值

    HashMap不能保证元素的顺序,HashMap能够将键设为null,也可以将值设为null. 与之对应的是Hashtable,(注意大小写:不是HashTable),Hashtable不能将键和值设 ...

  4. 简单的Redis数据迁移

    dump迁移 1.安装redis-dump工具 sudo apt-get install ruby rubygems ruby-devel -y gem sources --add http://ge ...

  5. ST3使用

    创建新的构建系统 Tools -> Build System -> New Build System... 输入类似的构建指令(首先清除当前的运行程序后再重新运行): { "sh ...

  6. js中小数精度问题

    js中小数的取值为近似值,可能比实际值大,也可能比实际值小,进行“四舍五入”得到的 例如:alert(0.1+0.2);值为0.300000004     alert(0.2+0.7);值为1.899 ...

  7. LeetCode(72) Edit Distance

    题目 Given two words word1 and word2, find the minimum number of steps required to convert word1 to wo ...

  8. ZOJ 3469 区间DP Food Delivery

    题解 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm ...

  9. JSTL 配置

    pache Tomcat安装JSTL 库步骤如下: 从Apache的标准标签库中下载的二进包(jakarta-taglibs-standard-current.zip). 官方下载地址:http:// ...

  10. Java异常架构图及面试题---https://www.cnblogs.com/gaoweixiao99/p/4905860.html

    https://www.cnblogs.com/gaoweixiao99/p/4905860.html 红色为检查异常,就是eclipse要提示你是try catch 还是throws. 非检查异常, ...