The Tag Game

链接   http://codeforces.com/problemset/problem/813/C

题目: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·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

 

题意 :

       就是说Alice和Bob做游戏,游戏刚开始是时候Alice在节点为1的地方,Bob在节点为x的地方,他们两个人的游戏目的不相同,Alice想尽快的完成游戏,Bob想尽可能使这个游戏持续的时间更长,游戏开始时Bob先走,当Alice走到Bob所在的地方时,游戏结束。通俗点说就是Alice想走尽可能多的步数,Bob想走尽可能少的步数(Bob原地不动也算是一步)。

 思路:

        先将他们走路的地图存起来(其实就是一个树状图),然后找到一个叶节点,输出该叶节点与1节点(即Alice最初所在的位置)路程乘以2。该叶节点需满足两个条件:

1:Bob到该叶节点的距离小于Alice到该叶节点距离。

2:该叶节点必须是在满足1的条件下,离Alice最远的那个叶节点。

解决方法:

     分别用2个bfs搜索,分别记录Alice与Bob到各个叶节点的距离,(用一个2为数组进行记录数据,其中数组第一个下标为0时存储的是Alice相对于各节点数据,为1是则是关于Bob的数据)。最后直接一层for循环遍历数组,更新最大值即可。

#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#define MAX 200002
using namespace std;
vector<int>mp[MAX];
int dp[2][MAX];
int dis[MAX];
int n,x;
int flag;
void dfs1(int u,int w)
{
    flag=1;
    for(int i=0; i<mp[u].size(); i++)
    {
        if(!dis[mp[u][i]])
        {
            dis[mp[u][i]]=1;
            dfs1(mp[u][i],w+1);
            dis[mp[u][i]]=0;
        }
    }
    if(flag)
    {
        dp[0][u]=w;
        return;
    }
}
void dfs2(int u,int w)
{
    flag=1;
    for(int i=0; i<mp[u].size(); i++)
    {
        if(!dis[mp[u][i]])
        {
            dis[mp[u][i]]=1;
            dfs2(mp[u][i],w+1);
            dis[mp[u][i]]=0;
        }
    }
    if(flag)
    {
        dp[1][u]=w;
        return;
    }
}
int main()
{
    int st,en;
    cin>>n>>x;
    for(int i=0; i<n-1; i++)
    {
        cin>>st>>en;
        mp[st].push_back(en);
        mp[en].push_back(st);
    }
    memset(dis,0,sizeof(dis));
    dis[1]=1;
    dfs1(1,0);
    memset(dis,0,sizeof(dis));
    dis[x]=1;
    dfs2(x,0);
    int maxx=-1;
    for(int i=1;i<=n;i++)
    {
        if(dp[1][i]<dp[0][i]&&dp[0][i]>maxx)
           maxx=dp[0][i];
    }
    printf("%d\n",2*maxx);
    return 0;
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值