CodeForces

来源:互联网 发布:怀化干部教育网络 编辑:程序博客网 时间:2024/06/05 00:07

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 31 22 32 4
output
4
input
5 21 22 33 42 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 3during 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在1节点,Bob在x节点,两人交替操作,每一次可以选择移动到相邻的一个节点或者不动,Bob希望两人越晚相遇越好,Alice希望越早越好,Bob先操作,问两人最后会在一共经历多少次操作后相遇

思路:相遇一定在某个叶节点,Bob和Alice都往该叶节点移动,Bob到之后一直不动,所以我们从1和x分别做一次dfs,记录到每个叶节点所用的时间,取Alice比Bob晚到且与Alice距离最长的叶节点,答案就是Alice到这个叶节点距离的2倍


#include <iostream>#include <queue>#include <cstdio>#include <vector>#define Max 200010using namespace std;int n,x,time[Max],bt[Max];vector<int>v[Max];queue<int>q;bool vis[Max],vb[Max];void bfs(){time[1]=0;vis[1]=true;q.push(1);while(q.size()){int i,u=q.front();q.pop();for(i=0;i<v[u].size();i++){if(i!=v[u][i]&&vis[v[u][i]]==false){q.push(v[u][i]);vis[v[u][i]]=true;time[v[u][i]]=time[u]+1;}}}q.push(x);vb[x]=true;bt[x]=0;while(q.size()){int i,u=q.front();q.pop();for(i=0;i<v[u].size();i++){if(i!=v[u][i]&&vb[v[u][i]]==false){q.push(v[u][i]);vb[v[u][i]]=true;bt[v[u][i]]=bt[u]+1;}}}}int main(){cin>>n>>x;int i;for(i=1;i<n;i++){int a,b;cin>>a>>b;v[a].push_back(b);v[b].push_back(a);}bfs();int ans=0;for(i=1;i<=n;i++){if(time[i]>bt[i]){ans=max(ans,time[i]*2);}}cout<<ans<<endl;}

附大佬深搜代码

#include <cstdio>#include <vector>#include <algorithm>using namespace std;vector<int> mp[200001];int n,x,a,b,ans,f;int d1[200001],d2[200001];void dfs(int p,int from,int step,int *d){    if(mp[p].size()==1&&p!=1)        d[p]=step;    for(int i=0;i<mp[p].size();i++)        if(mp[p][i]!=from)            dfs(mp[p][i],p,step+1,d);    return;}int main(){    scanf("%d%d",&n,&x);    for(int i=0;i<n-1;i++)    {        scanf("%d%d",&a,&b);        mp[a].push_back(b);        mp[b].push_back(a);    }    dfs(1,-1,0,d1);    dfs(x,-1,0,d2);    ans=0;    for(int i=2;i<=n;i++)        if(mp[i].size()==1&&d1[i]>d2[i])            ans=max(ans,d1[i]*2);    printf("%d\n",ans);    return 0;}