Educational Codeforces Round 22 C. The Tag Game【Greedy+dfs】 This article~ Commemorating my first 1A graph topic

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
Copy
4 3
1 2
2 3
2 4
output
Copy
4
input
Copy
5 2
1 2
2 3
3 4
2 5
output
Copy
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出发的人追赶在x的人,问最多有多少个turn才被抓到


思路:

1.贪心,x先往上爬,爬到不能爬为止

2.再dfs求出每个点到最底层还能走多少深度,树形DP --> dp[u]=max(dp[u],dp[v]+1) v是u的儿子


#include<bits/stdc++.h>
#define PI acos(-1.0)
#define pb push_back
using namespace std;
typedef long long ll;

const int N=2e5+5;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;

vector<int> edge[N];
int d[N],parent[N],mx[N];
int n,x,root=1;

void dfs(int u,int dep,int par){
//    cout <<"debg"<<endl;
    d[u]=dep;
    parent[u]=par;
    for(auto v:edge[u]){
        if(v!=par){
            dfs(v,dep+1,u);
        }
    }
}

int dfs1(int u,int par){
    for(auto v : edge[u]){
        if(v==par)  continue;
        dfs1(v,u);
        mx[u]=max(mx[u],mx[v]+1);
    }
}

int main(void){

    cin >>n>>x;
    for(int i=1;i<=n-1;i++){
        int u,v;scanf("%d%d",&u,&v);
        edge[u].pb(v);
        edge[v].pb(u);
    }
    dfs(1,1,-1);
    dfs1 (1, -1);
//    for(int i=1;i<=n;i++)   printf("mx[%d]=%d\n",i,mx[i]);
    int cnt=d[x]-2;
    if(cnt%2==0){
        for(int i=1;i<=cnt/2;i++)   x=parent[x];
//        cout <<"!"<<mx[x]<<endl;
        int y=parent[x];
        int ans=cnt/2+cnt/2+(mx[x]+1)*2;
        cout <<ans << endl;
    }
    else if(cnt%2==1){
        for(int i=1;i<=cnt/2;i++)   x=parent[x];
        int ans=cnt/2+cnt/2+(mx[x]+2)*2;
        cout <<ans << endl;
    }

    return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325814030&siteId=291194637