The Tag Game

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节点的路径就只有一条,那么追击者只能从1开始去追x,那么x有两种方向,一种是向着1跑,看看有什么岔路口可以走,二是背离x跑,一直跑到树的最深处。那么,如果我们能够计算以每个加点为根的树的最大深度,那么这个问题就解决了。

x所在树的深度已经求出,那么就看一下向着x方向走的路径有没有岔路口,使得以这个岔路口为树根的树的深度比之前的大,算出最大值*2就是我们要的答案。

具体代码如下

#include<bits/stdc++.h>
using namespace std;
vector<int>save[222222];
int maxdeep[222222],deep[222222],fa[222222];
void dfs(int now,int f,int d)
{
    deep[now]=maxdeep[now]=d;
    fa[now]=f;
   // printf("%d %d\n",now,fa[now]);
    int k=save[now].size();
    for(int i=0; i<k; i++)
    {
        int temp=save[now][i];
        if(temp!=f)
        {
            dfs(temp,now,d+1);
            maxdeep[now]=max(maxdeep[now],maxdeep[temp]);
        }
    }

}
int main()
{
    int n,x;

    while(scanf("%d%d",&n,&x)!=EOF)
    {
    memset(fa,0,sizeof(fa));
        for(int i=0; i<n-1; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            save[u].push_back(v);
            save[v].push_back(u);

        }
        dfs(1,1,0);
        int ans=2*maxdeep[x];
        for(int i=0,k=x; i<deep[x]; i++,k=fa[k])
        {
            if(i<deep[k])
            {
                ans=max(2*maxdeep[k],ans);
            }
            else
                break;
        }
        printf("%d\n",ans);
    }

}


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值