Codeforces 813C The Tag Game【思维+Dfs】

224 篇文章 2 订阅

C. The Tag Game
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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


题目大意:


一个人在节点1处后手走。另一个人在节点x处先手走。

第一个人想要追到第二个人,第二个人希望第一个人追不到。

每一个人可以选择在一个回合中移动或者原地不动。

问第一个人追到第二个人的最短时间。


思路:


我们先将第一个人Dfs一次预处理出来一个dist【0】【i】表示从节点1到节点i的距离。

那么接下来Dfs第二个人,如果遇到了dist【1】【i】==dist【0】【i】的节点,那么没有必要继续跑下去,同时更新答案output=max(output,dist【1】【i】);

反之如果存在dist【1】【i】<dist【0】【i】,那么有必要继续跑下去,同时更新答案output=max(output,dist【1】【i】*2);


Ac代码:

#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
int output;
vector<int >mp[222222];
int dist[2][2222222];
void Dfs(int u,int from,int d,int sum)
{
    dist[d][u]=sum;
    if(d==1&&sum==dist[0][u])
    {
        output=max(output,sum);
        return ;
    }
    if(d==1&&sum<dist[0][u])
    {
        output=max(output,dist[0][u]*2);
    }
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(v==from)continue;
        Dfs(v,u,d,sum+1);
    }
}
int main()
{
    int n,x;
    while(~scanf("%d%d",&n,&x))
    {
        output=0;
        for(int i=1;i<=n;i++)mp[i].clear();
        for(int i=0;i<n-1;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            mp[x].push_back(y);
            mp[y].push_back(x);
        }
        Dfs(1,-1,0,0);
        Dfs(x,-1,1,0);
        printf("%d\n",output);
    }
}














  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值