C. The Tag Game
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Alice got tired of playing the tag game by the usual rules so she offered Bob a little modification to it. Now the game should be played on an undirected rooted tree of n vertices. Vertex 1 is the root of the tree.

Alice starts at vertex 1 and Bob starts at vertex x (x ≠ 1). The moves are made in turns, Bob goes first. In one move one can either stay at the current vertex or travel to the neighbouring one.

The game ends when Alice goes to the same vertex where Bob is standing. Alice wants to minimize the total number of moves and Bob wants to maximize it.

You should write a program which will determine how many moves will the game last.

Input
The first line contains two integer numbers n and x (2 ≤ n ≤ 2·105, 2 ≤ x ≤ n).

Each of the next n - 1 lines contains two integer numbers a and b (1 ≤ a, b ≤ n) — edges of the tree. It is guaranteed that the edges form a valid tree.

Output
Print the total number of moves Alice and Bob will make.

Examples
input
4 3
1 2
2 3
2 4
output
4
input
5 2
1 2
2 3
3 4
2 5
output

6

Note

In the first example the tree looks like this:

The red vertex is Alice's starting position, the blue one is Bob's. Bob will make the game run the longest by standing at the vertex 3 during all the game. So here are the moves:

B: stay at vertex 3

A: go to vertex 2

B: stay at vertex 3

A: go to vertex 3

In the second example the tree looks like this:

The moves in the optimal strategy are:

B: go to vertex 3

A: go to vertex 2

B: go to vertex 4

A: go to vertex 3

B: stay at vertex 4

A: go to vertex 4

题意:一颗无向有根树,A在根节点1,B在非根节点x。B先走然后再A走(如此下去),每一步要么向相连点移动要么不动。A要以尽可能减少移动步数的走法,B以尽可能增加步数的走法。当A与B相遇时游戏结束。求最大的移动步数。

分析:B肯定在A的子树里 所以A走法就是不走多余的节点直接去接近B,而B可以找到他能走到的离根节点1距离最大的点这样就能使最后步数最大。

所以求出刚开始A·B的路径上的节点,找到此路径上深度最大的节点且B能走到的节点Y,ans =( 1到Y+Y的能到的最深深度)*2.

暂时没有想到更简单的想法和代码的写法,直接贴上代码:

    1. #include <iostream>
    2. #include<stdio.h>
    3. #include<string.h>
    4. #include<algorithm>
    5. #include<vector>
    6. #define siz 201015
    7. using namespace std;
    8. int n,x,cot;
    9. int d[siz],path[siz],vis[siz],dep[siz];
    10. vector<int>G[siz];
    11. int dfs(int u,int fa)//求深度
    12. {
    13. int len = G[u].size(),Max = 0;
    14. for(int i=0; i<len; i++)
    15. {
    16. int v = G[u][i];
    17. if(v == fa) continue;
    18. dep[v] = dep[u] + 1;
    19. Max = max(dfs(v,u),Max);
    20. }
    21. return d[u] = Max+1;
    22. }
    23. void pdfs(int u,int fa,int t)//求出路径
    24. {
    25. if(cot) return;
    26. path[t] = u;
    27. if(u == x)
    28. {
    29. cot = t;
    30. return;
    31. }
    32. int len = G[u].size();
    33. for(int i=0; i<len; i++)
    34. {
    35. int v = G[u][i];
    36. if(v == fa) continue;
    37. pdfs(v,u,t+1);
    38. }
    39. }
    40. void solve()
    41. {
    42. int ans = 0;
    43. memset(d,0,sizeof(d));
    44. memset(vis,0,sizeof(vis));
    45. cot = 0,dep[1] = 0;
    46. dfs(1,-1);
    47. pdfs(1,-1,1);
    48. for(int i=cot,j=1; i; i--,j++)//B先走 然后再A走,vis[i] 表示A所在的位置。
    49. {
    50. vis[j] = 1;
    51. if(!vis[i]) ans = max(ans,d[path[i]]+dep[path[i]]-1);
    52. }
    53. cout<<ans*2<<endl;
    54. }
    55. int main()
    56. {
    57. int u,v;
    58. while(~scanf("%d %d",&n,&x))
    59. {
    60. for(int i=1; i<=n; i++) G[i].clear();
    61. for(int i=1; i<n; i++)
    62. {
    63. scanf("%d %d",&u,&v);
    64. G[u].push_back(v);
    65. G[v].push_back(u);
    66. }
    67. solve();
    68. }
    69. return 0;
    70. }

codeforces 813C The Tag Game 树+dfs追击问题的更多相关文章

  1. CodeForces - 813C The Tag Game (树的dfs遍历)

    [传送门]http://codeforces.com/problemset/problem/813/C [题目大意]两个人玩游戏,一个人跑一个人追,轮流决策,可以走也可以不走.给你一棵树,想要从某个结 ...

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

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

  3. CodeForces 384E Propagating tree (线段树+dfs)

    题意:题意很简单么,给定n个点,m个询问的无向树(1为根),每个点的权值,有两种操作, 第一种:1 x v,表示把 x 结点加上v,然后把 x 的的子结点加上 -v,再把 x 的子结点的子结点加上 - ...

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

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

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

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

  6. HDU 5692 线段树+dfs序

    Snacks Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  7. Tsinsen A1505. 树(张闻涛) 倍增LCA,可持久化线段树,DFS序

    题目:http://www.tsinsen.com/A1505 A1505. 树(张闻涛) 时间限制:1.0s   内存限制:512.0MB    总提交次数:196   AC次数:65   平均分: ...

  8. 51 nod 1681 公共祖先 (主席树+dfs序)

    1681 公共祖先 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题   有一个庞大的家族,共n人.已知这n个人的祖辈关系正好形成树形结构(即父亲向儿子连边). 在另 ...

  9. BZOJ_3252_攻略_线段树+dfs序

    BZOJ_3252_攻略_线段树+dfs序 Description 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏.今天他得到了一款新游戏< ...

随机推荐

  1. Java8新特性(一)概览

    最近看了好几段Java代码和以往的风格很不一样,都有点不太适应了,后来一查原来是Java8的新特性. 为了保持对技术的敏感性(面试...),这里我们一起来学习下Java8的新特性. 如果从技术角度来看 ...

  2. 基于LRU Cache的简单缓存

    package com.test.testCache; import java.util.Map; import org.json.JSONArray; import org.json.JSONExc ...

  3. elk 日志分析系统Logstash+ElasticSearch+Kibana4

    elk 日志分析系统 Logstash+ElasticSearch+Kibana4 logstash 管理日志和事件的工具 ElasticSearch 搜索 Kibana4 功能强大的数据显示clie ...

  4. Javascript学习之Function对象详解

    JavaScript中的Function对象,就是我们常说的函数对象.在JS中,所有的函数也是以对象的形式存在的. 语法 充当Function对象的构造函数使用,用于结合new关键字构造一个新的Fun ...

  5. mongoose基于mongodb的数据评论设计

    var CommentSchema = { data:{type: ObjectId, ref:'Data'}, //Data数据表,此处存数据id from:{type: ObjectId, ref ...

  6. Boost 库编译总结

    1. 下载boost库源码,解压缩. 2. 打开vs2010 工具栏tools 下的visual studio command prompt,运行源码目录下的bootstrap.bat,生成bjam. ...

  7. EF 编程经验

    http://blog.csdn.net/itmaxin/article/details/47662151 这篇文章里有一下东西可以参考,但是弟二个方法明显是不可行的,因为我做了实验直接attach ...

  8. 手把手原生js轮播图

    在团队带人,突然被人问到轮播图如何实现,进入前端领域有一年多了,但很久没自己写过,一直是用大牛写的插件,今天就写个简单的适合入门者学习的小教程.当然,轮播图的实现原理与设计模式有很多种,我这里讲的是用 ...

  9. php设计模式之--组合模式

    php组合模式主要用于上下级关系,可以新增叶子和树枝,分析如下代码即可明白组合模式的含义: <?php header('Content-Type:text/html;charset=utf-8' ...

  10. 查看ubuntu磁盘空间占用及占用空间大的文件

    最近老是收到 ecs上有台服务器的磁盘利用率高 终于有一天 ssh登不上去了 http://blog.csdn.net/aaashen/article/details/50685988 清除相关大文件 ...