Problem:
 Score : 800 points    Problem Statement Takahashi and Aoki will play a game on a tree.
The tree has N vertices numbered 1 to N , and the i -th of the N-1 edges connects Vertex a_i and Vertex b_i .  At the beginning of the game, each vertex contains a coin.
Starting from Takahashi, he and Aoki will alternately perform the following operation:   Choose a vertex v that contains one or more coins, and remove all the coins from v .  Then, move each coin remaining on the tree to the vertex that is nearest to v among the adjacent vertices of the coin's current vertex.   The player who becomes unable to play, loses the game.
That is, the player who takes his turn when there is no coin remaining on the tree, loses the game.
Determine the winner of the game when both players play optimally.      Constraints  1 \leq N \leq 2 \times 10^5  1 \leq a_i, b_i \leq N  a_i \neq b_i  The graph given as input is a tree.         Input Input is given from Standard Input in the following format:  N  a_1  b_1  a_2  b_2  :  a_{N-1}  b_{N-1}       Output Print First if Takahashi will win, and print Second if Aoki will win.        Sample Input 1 3
1 2
2 3      Sample Output 1 First  Here is one possible progress of the game:   Takahashi removes the coin from Vertex 1 . Now, Vertex 1 and Vertex 2 contain one coin each.  Aoki removes the coin from Vertex 2 . Now, Vertex 2 contains one coin.  Takahashi removes the coin from Vertex 2 . Now, there is no coin remaining on the tree.  Aoki takes his turn when there is no coin on the tree and loses.        Sample Input 2 6
1 2
2 3
2 4
4 6
5 6      Sample Output 2 Second       Sample Input 3 7
1 7
7 4
3 4
7 5
6 3
2 1      Sample Output 3 First

Solution (Python):
N = int(input())
if N == 1:
  print("First")
  exit()
ab = [list(map(int, input().split())) for _ in range(N - 1)]

R = {}
for a, b in ab:
  if a - 1 in R:
    R[a - 1].append(b - 1)
  else:
    R[a - 1] = [b - 1]
  if b - 1 in R:
    R[b - 1].append(a - 1)
  else:
    R[b - 1] = [a - 1]

L = [0] * N
L[0] = 1
F = []
RL = {}
RL[1] = [0]
RL[2] = []
for i in R[0]:
  F.append(i)
  L[i] = 2
  RL[2].append(i)

k = 2
while F != []:
  k += 1
  Ft = []
  RL[k] = []
  for i in F:
    for j in R[i]:
      if L[j] == 0:
        L[j] = k
        Ft.append(j)
        RL[k].append(j)
  F = Ft

d = 0
D = [0] * N
for i in range(k - 1, 0, -1):
  for j in RL[i]:
    for k in R[j]:
      if L[k] == i - 1:
        if D[k] == 0:
          D[k] = D[j] + 1
          d = max(d, D[j] + 1)
        else:
          d = max(d, D[j] + D[k] + 1)
          D[k] = max(D[k], D[j] + 1)

if d % 3 == 1:
  print("Second")
else:
  print("First")

Solution (Python):
N = int(input())
Edge = [[] for _ in range(N+1)]
for _ in range(N-1):
    a, b = map(int, input().split())
    Edge[a].append(b)
    Edge[b].append(a)
stack = [1]
dist = [0]*(N+1)
visited = set([1])
while stack:
    vs = stack.pop()
    for e in Edge[vs]:
        if e in visited:
            continue
        visited.add(e)
        dist[e] = 1 + dist[vs]
        stack.append(e)
s = dist.index(max(dist))
stack = [s]
dist = [0]*(N+1)
visited = set([s])
while stack:
    vs = stack.pop()
    for e in Edge[vs]:
        if e in visited:
            continue
        visited.add(e)
        dist[e] = 1 + dist[vs]
        stack.append(e)
dia = max(dist) + 1
f = 'First'
s = 'Second'
if dia == 0:
    print(s)
elif dia == 1:
    print(f)
elif dia == 2:
    print(s)
else:
    if dia % 3 == 2:
        print(s)
    else:
        print(f)

Solution (Python):
N = int(input())
ab = [list(map(int,input().split())) for _ in range(N-1)]

connect = [[] for _ in range(N)]
for a,b in ab:
    connect[a-1].append(b-1)
    connect[b-1].append(a-1)

q = [0]
k = 0
d = [-1] * N
d[0] = 1
while k < len(q):
    i = q[k]
    di = d[i]
    for j in connect[i]:
        if d[j] == -1:
            d[j] = di + 1
            q.append(j)
    k += 1
q = [d.index(max(d))]
k = 0
d = [-1] * N
d[q[0]] = 1
while k < len(q):
    i = q[k]
    di = d[i]
    for j in connect[i]:
        if d[j] == -1:
            d[j] = di + 1
            q.append(j)
    k += 1

size = max(d)
if size == 1:
    print("First")
elif size == 2:
    print("Second")
elif size%3 == 2:
    print("Second")
else:
    print("First")

Solution (Python):


import sys
from collections import deque, defaultdict

INF = float('inf')
MOD = 10 ** 9 + 7
dy, dx = (0, -1, 0, 1), (1, 0, -1, 0)

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    n = II()
    G = defaultdict(list)
    for _ in range(n - 1):
        a, b = LI_()
        G[a].append(b)
        G[b].append(a)

    def DFS(start):
        stack = deque([start])
        dist = [INF] * n
        dist[start] = 0
        max_dist = 0
        farthest_v = start
        while stack:
            curr = stack.pop()

            for next in G[curr]:
                if dist[next] != INF:
                    continue
                dist[next] = dist[curr] + 1
                stack.append(next)
                if dist[next] > max_dist:
                    max_dist = dist[next]
                    farthest_v = next

        return farthest_v, max_dist

    v, d = DFS(0)
    u, d = DFS(v)

    
    if d == 1: d = 0
    print('First' if d % 3 != 1 else 'Second')
    

if __name__ == '__main__':
    main()

Solution (Python):
import sys
input=sys.stdin.readline
from collections import deque
n=int(input())
Edges=[[] for _ in range(n)]
for _ in range(n-1):
    a,b=map(int,input().split())
    a-=1; b-=1
    Edges[a].append(b)
    Edges[b].append(a)

def bfs(v):
    Visited=[False]*n
    q=deque()
    q.append((v,0))
    Visited[v]=True
    ret=(v,0)
    while q:
        fr,dist=q.popleft()
        if ret[1]<dist:
            ret=(fr,dist)
        for to in Edges[fr]:
            if not Visited[to]:
                Visited[to]=True
                q.append((to,dist+1))
    return ret

(v,dist)=bfs(0)

(v,dist)=bfs(v)

print('Second' if dist%3==1 else 'First')

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

N=int(input())
EDGE=[list(map(int,input().split())) for i in range(N-1)]

EDGELIST=[[] for i in range(N+1)]

for x,y in EDGE:
    EDGELIST[x].append(y)
    EDGELIST[y].append(x)

from collections import deque
H=[-1]*(N+1)
H[1]=0

QUE=deque([1])

while QUE:
    x=QUE.pop()
    for to in EDGELIST[x]:
        if H[to]==-1:
            H[to]=H[x]+1
            QUE.append(to)


QUE=deque([H.index(max(H))])

H=[-1]*(N+1)
H[QUE[0]]=0

while QUE:
    x=QUE.pop()
    for to in EDGELIST[x]:
        if H[to]==-1:
            H[to]=H[x]+1
            QUE.append(to)

HEIGHT=max(H)

if HEIGHT%3!=1:
    print("First")
else:
    print("Second")

Solution (Python):
from collections import deque


N = int(input())
G = dict()
for i in range(N):
    G[i] = dict()

for _ in range(N - 1):
    a, b = map(int, input().split())
    G[a-1][b-1] = 1
    G[b-1][a-1] = 1



queue1 = deque([0])
d1 = [1000000] * N
d1[0] = 0

while len(queue1) > 0:
    p = queue1.popleft()
    for q in G[p].keys():
        if d1[q] > d1[p] + 1:
            d1[q] = d1[p] + 1
            queue1.append(q)

far = 0
dd = 0
for i in range(N):
    if d1[i] > dd:
        far = i
        dd = d1[i]

queue2 = deque([far])
d2 = [1000000] * N
d2[far] = 0

while len(queue2) > 0:
    p = queue2.popleft()
    for q in G[p].keys():
        if d2[q] > d2[p] + 1:
            d2[q] = d2[p] + 1
            queue2.append(q)
diam = max(d2)

if diam % 3 == 1:
    print("Second")
else:
    print("First")

Solution (Python):
import sys
sys.setrecursionlimit(300000)
def input():
	return sys.stdin.readline()[:-1]

n = int(input())
adj = [[] for _ in range(n)]
for _ in range(n-1):
	a, b = map(int, input().split())
	adj[a-1].append(b-1)
	adj[b-1].append(a-1)

vis = [-1 for _ in range(n)]
def dfs(start):
	s = [start]
	while s:
		l = s.pop()
		for c in adj[l]:
			if vis[c] < 0:
				s.append(c)
				vis[c] = vis[l] + 1
	return

vis[0] = 0
dfs(0)

dia = -1
dia_ind = -1
for i in range(n):
	if vis[i] > dia:
		dia = vis[i]
		dia_ind = i

vis = [-1 for _ in range(n)]
vis[dia_ind] = 0
dfs(dia_ind)

dia = -1
dia_ind = -1
for i in range(n):
	if vis[i] > dia:
		dia = vis[i]
		dia_ind = i

if dia%3 == 1:
	print("Second")
else:
	print("First")

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    h,w = LI()
    a = [sys.stdin.readline() for i in range(h)]
    s = 0
    p = [None for i in range(w*h)]
    q = [None for j in range(w*h)]
    l = 0
    for i in range(h):
        for j in range(w):
            if a[i][j] == "
                p[s] = (i,j)
                s += 1
            else:
                q[l] = (i,j)
                l += 1
    if s == h*w:
        print(0)
        quit()
    ans = 0
    M = w*h
    for i,j in q[:h*w-s]:
        m = M
        for x,y in p[:s]:
            if abs(i-x)+abs(j-y) < m:
                m = abs(i-x)+abs(j-y)
        if m > ans:
            ans = m
    print(ans)
    return


def B():
    h,w,n = LI()
    sy,sx = LI()
    sy -= 1
    sx -= 1
    s = S()
    t = S()
    ks = list("LRUD")
    kt = list("RLDU")
    ds = [[0,-1],[0,1],[-1,0],[1,0]]
    dt = [[0,1],[0,-1],[1,0],[-1,0]]
    for i in range(4):
        y,x = [sy,sx]
        for j in range(n):
            if s[j] == ks[i]:
                y+=ds[i][0]
                x+=ds[i][1]
                if y < 0 or y >= h or x < 0 or x >= w:
                    print("NO")
                    quit()
            if t[j] == kt[i]:
                y += dt[i][0]
                x += dt[i][1]
                if y < 0:y = 0
                elif y >= h:y = h-1
                elif x < 0:x=0
                elif x >= w:x=w-1
    print("YES")
    return


def C():
    def dijkstra(s):
        d = [float("inf") for i in range(n)]
        d[s] = 0
        q=[[0,s]]
        while q:
            dx,x = heappop(q)
            for y in v[x]:
                if dx+1 < d[y]:
                    d[y] = dx+1
                    heappush(q,[d[y],y])
        return d
    n = I()
    v = [[] for i in range(n)]
    for i in range(n-1):
        a,b = LI()
        a -= 1
        b -= 1
        v[a].append(b)
        v[b].append(a)
    d = dijkstra(0)
    l = d.index(max(d))
    d = dijkstra(l)
    m = max(d)+1
    if m%3 == 2:
        print("Second")
    else:
        print("First")
    return


def D():
    n = I()
    return


def E():
    n = I()
    return


def F():
    n = I()
    return


if __name__ == "__main__":
    C()

Solution (Python):
from collections import deque
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
N = int(readline())
if N == 1:
    print("First")
    exit(0)
G = [[] for i in range(N)]
for i in range(N-1):
    a, b = map(int, readline().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
def bfs(s):
    dist = [None]*N
    que = deque([s])
    dist[s] = 0
    while que:
        v = que.popleft()
        d = dist[v]
        for w in G[v]:
            if dist[w] is not None:
                continue
            dist[w] = d + 1
            que.append(w)
    d = max(dist)
    return dist.index(d), d
u, _ = bfs(0)
v, d = bfs(u)

memo = [-1]*(d+2)
memo[0] = 0
memo[1] = 1
memo[2] = 0
for i in range(3, d+2):
    if memo[i-1] == 0 or memo[i-2] == 0:
        memo[i] = 1
    else:
        memo[i] = 0
print("First" if memo[d+1] else "Second")

Solution (Python):
N = int(input())
E = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    a-=1
    b-=1
    E[a].append(b)
    E[b].append(a)

from collections import deque
def bfs(s):
    dist = [None]*N
    que = deque([s])
    dist[s] = 0
    while que:
        v = que.popleft()
        d = dist[v]
        for w in E[v]:
            if dist[w] is not None:
                continue
            dist[w] = d + 1
            que.append(w)
    d = max(dist)
    return dist.index(d), d
u, _ = bfs(0)
v, d = bfs(u)

if d%3==1:
    print("Second")
else:
    print("First")

Solution (Python):
from sys import stdin
def main():
    n = int(stdin.readline())
    e = [[] for _ in xrange(n)]
    for line in stdin:
        x, y = map(int, line.split())
        x -= 1
        y -= 1
        e[x].append(y)
        e[y].append(x)
    d = [None] * n
    st = []
    pu = st.append
    po = st.pop
    pu(0)
    d[0] = 0
    while st:
        x = po()
        for y in e[x]:
            if d[y] is None:
                d[y] = d[x] + 1
                pu(y)
    y = -1
    for i, x in enumerate(d):
        if y < x:
            y = x
            j = i
    d = [None] * n
    pu(j)
    d[j] = 0
    while st:
        x = po()
        for y in e[x]:
            if d[y] is None:
                d[y] = d[x] + 1
                pu(y)
    x = max(d)
    print 'Second' if x % 3 == 1 else 'First'
main()

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)

def main():
    n = I()
    aa = LLI()
    e = collections.defaultdict(set)
    for a,b in aa:
        e[a].add(b)
        e[b].add(a)


    def search(s):
        d = collections.defaultdict(lambda: inf)
        d[s] = 0
        q = []
        heapq.heappush(q, (0, s))
        v = collections.defaultdict(bool)
        while len(q):
            k, u = heapq.heappop(q)
            if v[u]:
                continue
            v[u] = True

            for uv in e[u]:
                if v[uv]:
                    continue
                vd = k + 1
                if d[uv] > vd:
                    d[uv] = vd
                    heapq.heappush(q, (vd, uv))

        return d

    d1 = search(1)
    mv = -1
    mk = 1
    for k,v in d1.items():
        if mv < v:
            mv = v
            mk = k
    d2 = search(mk)
    mv = max(d2.values())

    if (mv + 1) % 3 == 2:
        return 'Second'

    return 'First'


print(main())

Solution (Python):
import sys

sys.setrecursionlimit(2 * 10 ** 5 + 5)


def dfs(v, p, links):
    farthest_d = 0
    farthest_v = v

    for u in links[v]:
        if u == p:
            continue
        res_d, res_v = dfs(u, v, links)
        if res_d > farthest_d:
            farthest_d = res_d
            farthest_v = res_v
    return farthest_d + 1, farthest_v


def solve(n, links):
    if n == 1:
        return True

    d, v = dfs(0, -1, links)
    d, v = dfs(v, -1, links)
    if (d + 1) % 3 == 0:
        return False
    return True


n = int(input())
links = [set() for _ in [0] * n]
for line in sys.stdin:
    a, b = map(int, line.split())
    a -= 1
    b -= 1
    links[a].add(b)
    links[b].add(a)
print('First' if solve(n, links) else 'Second')