Educational Codeforces Round 22 C. 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·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.

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为树根的无向树,小a在1,小b在m,小a总想最快靠近小b,小b总想最晚被接近,他们两个各走一步。问你最大的回合数(每回合一次)

 

思路:每个人都走最优,是不会走回头路的(B来回走可以看作在一点不动),那么我们可以先求下A,B分别到所有节点的最短时间,对于每个节点如果B走的时间小于A走的时间那B就可以走到这个。所以要在先求了从根节点到其他节点的max距离之后,再逆序遍历其x位置走的距离。 因为B走的位置必须在A之前走。 就是说可能相遇,得排除相遇这种情况。

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+1;
int fa[N],dep[N],len[N];
vector<int>adj[N];
int n,x;
int dfs(int cur,int father,int lens)//dfs遍历所有点
{
     dep[cur]=lens;
     len[cur]=0;
     fa[cur]=father;
     for(int i=0;i<adj[cur].size();i++)
     {
         int ne=adj[cur][i];
         if(ne==father)continue;
         
         len[cur]=max(len[cur],dfs(ne,cur,lens+1));
     }
     return len[cur]+1;
}
int main()
{//freopen("t.txt","r",stdin);
     scanf("%d%d",&n,&x);
     int a,b;
     for(int i=0;i<n;i++)
     {
         scanf("%d%d",&a,&b);
         adj[a].push_back(b);
         adj[b].push_back(a);
     }
     fa[1]=0;
     dfs(1,0,0);
     int ll=0;
     int ans=dep[x]+len[x];
     while((x=fa[x])!=0)//逆序遍历
     {
         ll++;
         if(dep[x]<=ll)break;
         ans=max(ans,dep[x]+len[x]);
     }
     printf("%d\n",ans*2);
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值