codeforces 813C The Tag Game

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

题目大意是:两个小孩追着玩,一个人从树根节点开始追另一个,他想尽快追上另一个小孩,而另一个小孩不太想让他追上。他们两个人轮流走一格,求出最后两个人相遇时经过几次轮流。

用了两次广搜做出来的,第一次先让追的那个人把树遍历一遍记录下经过每个位置需要的步数,第二次广搜可以求出被追的人可以走那些格子,最后找一个用时最长的乘以2就是答案。

#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
const int INF = 99999999;
const int M = 2e5 + 5;
int vis[M];
bool bo[M];
int ex, ey;
vector<int> adjt[M];
vector<int> can;
struct Node
{
    int x, s;
};
int n, k;
void bfs1()
{
    queue<Node> q;
    for(int i=1;i<=n;i++)
        vis[i] = INF;
    int xt;
    Node tmp, next;
    tmp.x = 1;
    tmp.s = 0;
    vis[1] = 0;
    q.push(tmp);
    while(!q.empty())
    {
        tmp = q.front();
        q.pop();
        vector<int>::iterator it;
        for(it=adjt[tmp.x].begin();it!=adjt[tmp.x].end();it++)
        {
            xt = *it;
            next.s = tmp.s + 1;
            if(vis[xt]>next.s)
            {
                next.x = xt;
                vis[xt] = next.s;
                q.push(next);
            }
        }
    }
}
void bfs()
{
    memset(bo, false, sizeof(bo));
    queue<Node> q;
    Node tmp, next;
    tmp.x = k;
    tmp.s = 0;
    int xt;
    bo[xt] = true;
    q.push(tmp);
    while(!q.empty())
    {
        tmp = q.front();
        q.pop();
        can.push_back(tmp.x);
        vector<int>::iterator it;
        for(it=adjt[tmp.x].begin();it!=adjt[tmp.x].end();it++)
        {
            xt = *it;
            next.s = tmp.s + 1;
            if(!bo[xt]&&vis[xt]>next.s)
            {
                next.x = xt;
                bo[xt] = true;
                q.push(next);
            }
        }
    }
}
int main()
{
    int u, v;
    scanf("%d%d", &n, &k);
    for(int i=1;i<n;i++)
    {
        scanf("%d%d", &u, &v);
        adjt[u].push_back(v);
        adjt[v].push_back(u);
    }
    bfs1();
    bfs();
    int ans = -1;
    vector<int>::iterator it=can.begin();
    for(;it!=can.end();it++)
        ans = max(ans, vis[*it]);
    printf("%d", ans*2);
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值