The Tag Game(bfs)

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·1052 ≤ 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.

Example
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 :先让Alice先广搜一遍,记录它到每个点的最短时间。

    2 :在1的限制下,因为bob不能被赶上,所以在Alice 时间限制下记录bob可以到达的所有位置。

    3 :最终结果就是bob确定范围内Alice 的最长时间的二倍。


#include <cstdio>
#include <queue>
#include <map>
#include <stack>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

const int maxn = 2e5+10;
vector <int> vc[maxn];
int a[maxn];
int mk[maxn];
int b[maxn];
int T;
void bfs(int x)
{
    queue <int> q;
    q.push(x);
    mk[x] = 1;
    a[x] = 0;
    while(!q.empty())
    {
         int tmp = q.front();
         q.pop();
         for(int i = 0; i < vc[tmp].size(); i++)
         {
            int x_ =  vc[tmp][i];
            if(mk[x_]) continue;
            mk[x_] = 1;
            a[x_] = a[tmp] + 1;
            q.push(x_);
         }
    }
}
void Bfs(int x)
{
    queue <int> q;
    q.push(x);
    mk[x] = 1;
    T = max(T,a[x]);
    b[x] = 0;
    while(!q.empty())
    {
         int tmp = q.front();
         q.pop();
         for(int i = 0; i < vc[tmp].size(); i++)
         {
            int x_ =  vc[tmp][i];
            if(mk[x_]) continue;
            if(b[tmp]+1 >= a[x_]) continue;
            mk[x_] = 1;
            T = max(T,a[x_]);
            b[x_] = b[tmp] + 1;
            q.push(x_);
         }
    }
}
int main()
{
    int m,n;
    scanf("%d%d",&m,&n);
    while(--m)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        vc[x].push_back(y);
        vc[y].push_back(x);
    }
    memset(mk,0,sizeof(mk));
    bfs(1);
    memset(mk,0,sizeof(mk));
    Bfs(n);
    printf("%d\n",T*2);
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值