【传送门】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. Python基础篇 -- 小数据池和再谈编码

    小数据池 1. id() 通过id()可以查看到一个变量表示的值在内存中的地址 s = "Agoni" print(id(s)) # 2410961093272 2. is 和 = ...

  2. java在线聊天项目0.4版本 制作服务端接收连接,客户端连接功能 新增客户端窗口打开时光标指向下边文本域功能,使用WindowListener监听WindowAdapter

    建一个服务端类ChatServer,用于设置端口接收连接 package com.swift; import java.io.IOException; import java.net.ServerSo ...

  3. 选择法数组排序参考(Java)

    package com.swift; public class Xuanze { public static void main(String[] args) { int[] arr= {28,2,3 ...

  4. MySQL 查询优化之 Index Merge

    MySQL 查询优化之 Index Merge Index Merge Intersection 访问算法 Index Merge Union 访问算法 Index Merge Sort-Union ...

  5. Linux菜鸟起飞之路【四】绝对路径、相对路径及常用目录

    一.绝对路径与相对路径 Linux操作系统中存在着两种路径:绝对路径和相对路径.我们在访问文件或文件夹的时候,其实都是通过路径来操作的.两种路径在实际操作中能起到同等的作用. 在开始具体介绍之前,我们 ...

  6. pycharm添加wordcloud模块时报错:error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools

    windows 7 32bit python3.6.3 32bit pycharm2018社区版 32bit 问题说明: 添加wordcloud模块时报错:error: Microsoft Visua ...

  7. LeetCode(9)Palindrome Number

    题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...

  8. 推荐SQL Server Management Studio以及Visual Studio下的免费的插件 ApexSQL Complete

    SQL Server 并没有代码格式化的工具,对于处理他人编写的长SQL需要手工的格式化是一件麻烦的事情. 推荐SQL Server Management Studio以及Visual Studio下 ...

  9. MFC中Picture控件显示图像

    图片显示在picture控件中,整个软件最小化后图片消失问题. 解决方案:OpenCV学习笔记(9)利用MFC的Picture控件显示图像+播放视频和捕获摄像头画面 - CSDN博客  http:// ...

  10. PYDay10&11&12&13-常用模块:time|datetime|os|sys|pickle|json|xml|shutil|logging|paramiko、configparser、字符串格式化、py自动全局变量、生成器迭代器

    1.py文件自动创建的全局变量 print(vars()) 返回值:{'__name__': '__main__', '__package__': None, '__loader__': <_f ...