The Tag Game (Codeforces 813C)
Tags: Others, dfs and similar

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.

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 3A: go to vertex 2B: stay at vertex 3A: go to vertex 3In the second example the tree looks like this:  The moves in the optimal strategy are:B: go to vertex 3A: go to vertex 2B: go to vertex 4A: go to vertex 3B: stay at vertex 4A: go to vertex 4

Sample Inputs:
4 3
1 2
2 3
2 4

5 2
1 2
2 3
3 4
2 5

Sample Outputs:
4

6

Solution (Python):
from collections import deque
from sys import stdin

def BFS(s):
    if s == 0:
        distance = distance_Alice
    else:
        distance = distance_Bob
    distance[s] = 0
    q = deque()
    q.append(s)
    while len(q) > 0:
        v = q.popleft()
        for u in adjacents_list[v]:
            if distance[u] == -1:
                distance[u] = distance[v] + 1
                q.append(u)
(n, x) = map(int, stdin.readline().split())
distance_Alice = [-1 for i in range(n)]
distance_Bob = [-1 for i in range(n)]
adjacents_list = [[] for i in range(n)]
for i in range(n - 1):
    (v1, v2) = map(int, stdin.readline().split())
    adjacents_list[v1 - 1].append(v2 - 1)
    adjacents_list[v2 - 1].append(v1 - 1)
BFS(0)
BFS(x - 1)
max = 0
for i in range(n):
    if max < distance_Alice[i] and distance_Bob[i] < distance_Alice[i]:
        max = distance_Alice[i]
print(max * 2)

Solution (Python):
import sys

def li():
    return list(map(int, sys.stdin.readline().split()))
(n, x) = li()
x -= 1
g = [[] for i in range(n)]
for i in range(n - 1):
    (a, b) = li()
    a -= 1
    b -= 1
    g[a].append(b)
    g[b].append(a)

def dfs(r):
    d = [0] * n
    rem = [[r, -1]]
    while rem:
        (v, p) = rem.pop()
        for u in g[v]:
            if u != p:
                rem.append([u, v])
                d[u] = d[v] + 1
    return d
p = dfs(0)
q = dfs(x)
answer = 0
for i in range(n):
    if p[i] > q[i]:
        answer = max(answer, p[i] * 2)
print(answer)

Solution (Python):
import sys
input = sys.stdin.readline
from collections import deque

def solve(n, x, edges):
    valueFrom1 = {}
    valueFromX = {}
    q = deque([(1, 0)])
    vis = {1}
    while q:
        (node, dis) = q.popleft()
        valueFrom1[node] = dis
        for nei in edges[node]:
            if nei in vis:
                continue
            q.append((nei, dis + 1))
            vis.add(nei)
    q = deque([(x, 0)])
    vis = {x}
    while q:
        (node, dis) = q.popleft()
        valueFromX[node] = dis
        for nei in edges[node]:
            if nei in vis:
                continue
            q.append((nei, dis + 1))
            vis.add(nei)
    ans = -1
    for i in range(1, n + 1):
        if valueFrom1[i] > valueFromX[i]:
            ans = max(ans, valueFrom1[i] * 2)
    print(ans)
(n, x) = map(int, input().split())
edges = [[] for _ in range(n + 1)]
for _ in range(n - 1):
    (u, v) = map(int, input().split())
    edges[u].append(v)
    edges[v].append(u)
solve(n, x, edges)