Problem:


Score : 400 points

Problem Statement

Fennec and Snuke are playing a board game.

On the board, there are N cells numbered 1 through N, and N-1 roads, each connecting two cells. Cell a_i is adjacent to Cell b_i through the i-th road. Every cell can be reached from every other cell by repeatedly traveling to an adjacent cell. In terms of graph theory, the graph formed by the cells and the roads is a tree.

Initially, Cell 1 is painted black, and Cell N is painted white. The other cells are not yet colored.
Fennec (who goes first) and Snuke (who goes second) alternately paint an uncolored cell.
More specifically, each player performs the following action in her/his turn:

Fennec: selects an uncolored cell that is adjacent to a black cell, and paints it black.

Snuke: selects an uncolored cell that is adjacent to a white cell, and paints it white.

A player loses when she/he cannot paint a cell. Determine the winner of the game when Fennec and Snuke play optimally.

Constraints

2 \leq N \leq 10^5

1 \leq a_i, b_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

If Fennec wins, print Fennec; if Snuke wins, print Snuke.

Sample Input 1

7
3 6
1 2
3 1
7 4
5 7
1 4

Sample Output 1

Fennec

For example, if Fennec first paints Cell 2 black, she will win regardless of Snuke's moves.

Sample Input 2

4
1 4
4 2
2 3

Sample Output 2

Snuke



Sample input:
7
3 6
1 2
3 1
7 4
5 7
1 4


Sample output:
Fennec

Submission status: Time Limit Exceeded

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

# 1からNの中間点より手前に、より多くのマスへの分岐を持っていれば勝ち
# 幅優先で、最短ルートを記録する。
from collections import deque
que=deque()

import copy

# [番号, 0からのルート, 親]
que.append([0,[],-1])
Nhalf=None
while que:
  node=que.popleft()
  v=node[0]
  route=node[1]
  parent=node[2]
  if v==N-1:
    # Nへの到達ルートの中間点
    route.append(v)
#    print(route)
    if len(route)%2==0:
      Nhalf=route[len(route)//2-1]
      NhalfNext=route[len(route)//2]
    else:
      Nhalf=route[len(route)//2]
      NhalfNext=route[len(route)//2+1]
      
#    print(Nhalf,E[Nhalf],"remove:",NhalfNext)
#    print("bef:",E)
    E[Nhalf].remove(NhalfNext)
#    print("after:",E[Nhalf])
#    print(NhalfNext,E[NhalfNext],"remove:",Nhalf)
    E[NhalfNext].remove(Nhalf)
#    print("af:",E)
    break
  route.append(v)
  childs=E[v]
  for child in childs:
    if child==parent:
      continue
    newroute=copy.copy(route)
    que.append([child,newroute,v])

# この木でもう一回数え直す。行ける場所が(N-2)の半数以上であれば勝ち
stack=[]
# 自分、親
stack.append([0,-1])
count=0
while stack:
  node=stack.pop()
  v=node[0]
  parent=node[1]
#  print("reached:",v)
  count+=1
  childs=E[v]
  if childs==None:
    continue
  for child in childs:
    if child==parent:
      continue
    stack.append([child,v])
  
#print(count)
if count>N-count:
  print("Fennec")
else:
  print("Snuke")

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000)
N=int(input())
ab=[[] for _ in range(N)]
for _ in range(N-1):
  a,b=map(int,input().split())
  ab[a-1].append(b)
  ab[b-1].append(a)


def dfs(pre,p,pa):
  global path
  if p==N:
    path=pa
  else:
    for i in ab[p-1]:
      if i!=pre:
        dfs(p,i,pa+[i])

dfs(-1,1,[1])
spath=set(path)

def dfs2(pre,p):
  a=0
  for i in ab[p-1]:
    if i in spath:
      pass
    elif i==pre:
      pass
    else:
      a+=dfs2(p,i)
  return 1+a

ff=0
sf=0
L=len(path)
for i in range(L):
  if i<=(L-1)//2:
    ff+=dfs2(-1,path[i])
  else:
    sf+=dfs2(-1,path[i])
    
if ff>sf:
  print("Fennec")
else:
  print("Snuke")

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000)
N=int(input())
ab=[[] for _ in range(N)]
for _ in range(N-1):
  a,b=map(int,input().split())
  ab[a-1].append(b)
  ab[b-1].append(a)


def dfs(p,pa):
  global path
  if p==N:
    path=pa
  else:
    for i in ab[p-1]:
      if i not in pa:
        dfs(i,pa+[i])

dfs(1,[1])

def dfs2(p):
  a=0
  seen.append(p)
  for i in ab[p-1]:
    if i in path:
      pass
    elif i in seen:
      pass
    else:
      a+=dfs2(i)
  return 1+a

ff=0
sf=0
L=len(path)
for i in range(L):
  seen=[]
  if i<=(L-1)//2:
    ff+=dfs2(path[i])
  else:
    sf+=dfs2(path[i])
    
if ff>sf:
  print("Fennec")
else:
  print("Snuke")

Submission status: Time Limit Exceeded

Solution code:
#高速化heapqダイクストラ
from collections import *
from heapq import*
import sys
input=lambda:sys.stdin.readline()


def BFS(point,d):
    cost=[1e7]*(n+1)
    cost[point]=0
    Q=deque([(0,point)])
    
    while Q:
        c,p = Q.popleft()
        for np in d[p]:
            if cost[np]==1e7:
                cost[np]=c+1
                Q.append((c+1,np))
    return cost

n=int(input())
d={}
for i in range(n-1):
    a,b=map(int,input().split())
    if a in d.keys():
        d[a].append(b)
    else:
        d[a]=[b]
    if b in d.keys():
        d[b].append(a)
    else:
        d[b]=[a]
        
x=BFS(1,d)[1:]
y=BFS(n,d)[1:]

#print(x,y)

F=0
S=0

for s,t in zip(x,y):
    if s<=t:
        F+=1
    else:
        S+=1

if F<=S:
    print("Snuke")
else:
    print("Fennec")

Submission status: Time Limit Exceeded

Solution code:
#高速化heapqダイクストラ
from collections import *
from heapq import*
import sys
input=lambda:sys.stdin.readline()


def BFS(point,d):
    cost=[1e7]*(n+1)
    cost[point]=0
    Q=[]
    heappush(Q,(0,point))
    
    while Q:
        c,p=heappop(Q)
        for np in d[p]:
            if cost[np]==1e7:
                cost[np]=c+1
                heappush(Q,(c+1,np))
    return cost

n=int(input())
d={}
for i in range(n-1):
    a,b=map(int,input().split())
    if a in d.keys():
        d[a].append(b)
    else:
        d[a]=[b]
    if b in d.keys():
        d[b].append(a)
    else:
        d[b]=[a]
        
x=BFS(1,d)[1:]
y=BFS(n,d)[1:]

#print(x,y)

F=0
S=0

for s,t in zip(x,y):
    if s<=t:
        F+=1
    else:
        S+=1

if F<=S:
    print("Snuke")
else:
    print("Fennec")

Submission status: Time Limit Exceeded

Solution code:
#高速化heapqダイクストラ
from collections import *
from heapq import*
import sys
input=lambda:sys.stdin.readline()


def DIJKSTRA(point,d):
    cost      = [float("inf")]*(n+1)
    Q         = []
    cost[point]=0
    heappush(Q,(point,cost[point]))
    while Q:
        prefnd,tmpC=heappop(Q)
        if cost[prefnd]<tmpC:
            continue
        for node,c in d[prefnd]:
            altC=c+tmpC
            if cost[node]>altC:
                cost[node]=altC
                heappush(Q,(node,altC))
    return cost

n=int(input())
d={}
for i in range(n-1):
    a,b=map(int,input().split())
    if a in d.keys():
        d[a].append([b,1])
    else:
        d[a]=[[b,1]]
    if b in d.keys():
        d[b].append([a,1])
    else:
        d[b]=[[a,1]]
        
x=DIJKSTRA(1,d)[1:]
y=DIJKSTRA(n,d)[1:]
#print(x)
#print(y)
F=0
S=0
for i in range(n):
    if x[i]<=y[i]:
        F+=1
    else:
        S+=1
if F<=S:
    print("Snuke")
else:
    print("Fennec")

Submission status: Time Limit Exceeded

Solution code:
#高速化heapqダイクストラ
from collections import *
from heapq import*
import sys
input=lambda:sys.stdin.readline()


def DIJKSTRA(point,d):
    cost      = [float("inf")]*(n+1)
    Q         = []
    cost[point]=0
    heappush(Q,(point,cost[point]))
    while Q:
        prefnd,tmpC=heappop(Q)
        if cost[prefnd]<tmpC:
            continue
        for node,c in d[prefnd]:
            altC=c+tmpC
            if cost[node]>altC:
                cost[node]=altC
                heappush(Q,(node,altC))
    return cost

n=int(input())
d={}
for i in range(n-1):
    a,b=map(int,input().split())
    if a in d.keys():
        d[a].append([b,1])
    else:
        d[a]=[[b,1]]
    if b in d.keys():
        d[b].append([a,1])
    else:
        d[b]=[[a,1]]
        
x=DIJKSTRA(1,d)[1:]
y=DIJKSTRA(n,d)[1:]
#print(x)
#print(y)
F=0
S=0
for s,t in zip(x,y):
    if s<=t:
        F+=1
    else:
        S+=1
if F<=S:
    print("Snuke")
else:
    print("Fennec")

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

N = int(input())

neighbor = [[] for _ in range(N)]

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

# 黒を0、白を1、未着色を-1で表す
black_white = [0] + [-1]*(N-2) + [1]

# 点iを与えると、点iからの最短距離のリストを返す
def shortest_distance(home_point):
    shortest_distance_list = [-1]*N
    shortest_distance_list[home_point] = 0
    visited = [0]*N
    visited[home_point] = 1
    next_points = deque([home_point])

    while len(next_points) > 0:
        p = next_points.popleft()
        visited[p] = 1
        for idx in neighbor[p]:
            if visited[idx] == 0:
                shortest_distance_list[idx] = shortest_distance_list[p]+1
                if idx not in next_points:
                    next_points.append(idx)

    return shortest_distance_list

shortest_distance_from_Fennec = shortest_distance(0)
shortest_distance_from_Snuke = shortest_distance(N-1)

for idx in range(1,N-1):
    if shortest_distance_from_Fennec[idx] <= shortest_distance_from_Snuke[idx]:
        black_white[idx] = 0
    else:
        black_white[idx] = 1

print("Fennec" if black_white.count(0) > black_white.count(1) else "Snuke")

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

N = int(input())

neighbor = [[0]*N for _ in range(N)]

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

# 黒を0、白を1、未着色を-1で表す
black_white = [0] + [-1]*(N-2) + [1]

# 点iを与えると、点iからの最短距離のリストを返す
def shortest_distance(home_point):
    shortest_distance_list = [-1]*N
    shortest_distance_list[home_point] = 0
    visited = [0]*N
    visited[home_point] = 1
    next_points = deque([home_point])

    while len(next_points) > 0:
        p = next_points.popleft()
        visited[p] = 1
        for idx in range(N):
            if neighbor[p][idx] > 0 and visited[idx] == 0:
                shortest_distance_list[idx] = shortest_distance_list[p]+1
                if idx not in next_points:
                    next_points.append(idx)

    return shortest_distance_list

shortest_distance_from_Fennec = shortest_distance(0)
shortest_distance_from_Snuke = shortest_distance(N-1)

for idx in range(1,N-1):
    if shortest_distance_from_Fennec[idx] <= shortest_distance_from_Snuke[idx]:
        black_white[idx] = 0
    else:
        black_white[idx] = 1

print("Fennec" if black_white.count(0) > black_white.count(1) else "Snuke")

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

N = int(input())

neighbor = [[0]*N for _ in range(N)]

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

# 黒を0、白を1、未着色を-1で表す
black_white = [0] + [-1]*(N-2) + [1]

# 点iを与えると、点iからの最短距離のリストを返す
def shortest_distance(home_point):
    shortest_distance_list = [-1]*N
    shortest_distance_list[home_point] = 0
    visited = [0]*N
    next_points = deque([home_point])

    while len(next_points) > 0:
        p = next_points.popleft()
        visited[p] = 1
        for idx in range(N):
            if neighbor[p][idx] > 0 and visited[idx] == 0:
                next_points.append(idx)
                shortest_distance_list[idx] = shortest_distance_list[p]+1

    return shortest_distance_list

shortest_distance_from_Fennec = shortest_distance(0)
shortest_distance_from_Snuke = shortest_distance(N-1)

for idx in range(1,N-1):
    if shortest_distance_from_Fennec[idx] <= shortest_distance_from_Snuke[idx]:
        black_white[idx] = 0
    else:
        black_white[idx] = 1

print("Fennec" if black_white.count(0) > black_white.count(1) else "Snuke")

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

N = int(input())

neighbor = [[0]*N for _ in range(N)]
edge = []

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

# 黒を0、白を1、未着色を-1で表す
black_white = [0] + [-1]*(N-2) + [1]

# 点iを与えると、点iからの最短距離のリストを返す
def shortest_distance(home_point):
    shortest_distance_list = [-1]*N
    shortest_distance_list[home_point] = 0
    visited = [0]*N
    next_points = deque([home_point])

    while len(next_points) > 0:
        p = next_points.popleft()
        visited[p] = 1
        extend_points = [idx for idx in range(len(neighbor[p])) if neighbor[p][idx] > 0 and visited[idx] == 0]
        for q in extend_points:
            shortest_distance_list[q] = shortest_distance_list[p]+1
        next_points.extend(extend_points)

    return shortest_distance_list

shortest_distance_from_Fennec = shortest_distance(0)
shortest_distance_from_Snuke = shortest_distance(N-1)

for idx in range(1,N-1):
    if shortest_distance_from_Fennec[idx] <= shortest_distance_from_Snuke[idx]:
        black_white[idx] = 0
    else:
        black_white[idx] = 1

print("Fennec" if black_white.count(0) > black_white.count(1) else "Snuke")

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


n = I()
graph = [[] for i in range(n)]
for a, b in LIRM(n - 1):
    graph[a] += [b]
    graph[b] += [a]



def dfs(graph, start, goal):
    que = deque([start])
    pre = [-1] * n
    while que:
        cur = que.popleft()
        for nxt in graph[cur]:
            if nxt == pre[cur]:
                continue
            pre[nxt] = cur
            if nxt == goal:
                break
            que.append(nxt)
    return pre



pre = dfs(graph, 0, n - 1)
route = [n - 1]
while route[0] != 0:
    route = [pre[route[0]]] + route

limit_num = (len(route) - 1) // 2
black_node = route[limit_num]
white_node = route[limit_num + 1]
graph[black_node].remove(white_node)


def dfs_cnt(graph, start):
    cnt = 1
    que = deque([start])
    pre = [-1] * n
    while que:
        cur = que.popleft()
        for nxt in graph[cur]:
            if nxt == pre[cur]:
                continue
            pre[nxt] = cur
            cnt += 1
            que.append(nxt)
    return cnt


black_cnt = dfs_cnt(graph, 0)
white_cnt = n - black_cnt

if black_cnt > white_cnt:
    print('Fennec')
else:
    print('Snuke')

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2000000)
def input():
    return sys.stdin.readline()[:-1]
n=int(input())
ab=[list(map(int,input().split())) for i in range(n-1)]
ki=[[] for i in range(n+1)]
for i in range(n-1):
    ki[ab[i][0]].append(ab[i][1])
    ki[ab[i][1]].append(ab[i][0])
# print(ki)
kyori=[[0,0] for i in range(n+1)]
def tansaku(m, dis, hito):
    if not ki[m]:
        kyori[m][hito]=dis
        return
    elif kyori[m][hito]!=0:
        return
    else:
        kyori[m][hito]=dis
        for i in range(len(ki[m])):
            tansaku(ki[m][i], dis+1, hito)
tansaku(1,1,0)
tansaku(n,1,1)
fe,su=0,0

for i in range(1,n+1):
    if kyori[i][0]<=kyori[i][1]:
        fe+=1
    else:
        su+=1

if fe>su:
    print("Fennec")
else:
    print("Snuke")

Submission status: Time Limit Exceeded

Solution code:
def dijkstra(s,n,w,cost):
    #始点sから各頂点への最短距離
    #n:頂点数,　w:辺の数, cost[u][v] : 辺uvのコスト(存在しないときはinf)
    d = [float("inf")] * n
    used = [False] * n
    d[s] = 0
    
    while True:
        v = -1
        #まだ使われてない頂点の中から最小の距離のものを探す
        for i in range(n):
            if (not used[i]) and (v == -1):
               v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
               break
        used[v] = True
               
        for j in range(n):
               d[j] = min(d[j],d[v]+cost[v][j])
    return d

################################
n=int(input())
cost = [[float("inf") for i in range(n)] for i in range(n)] 
#cost[u][v] : 辺uvのコスト(存在しないときはinf この場合は10**10)
for i in range(n-1):
    x,y = map(int,input().split())
    x-=1
    y-=1
    cost[x][y] = 1
    cost[y][x] = 1
l=dijkstra(0,n,n-1,cost)
ll=dijkstra(n-1,n,n-1,cost)
s,f=0,0
for i in range(n):
    if l[i]<=ll[i]:
        f+=1
    else:
        s+=1
if f<=s:
    print("Snuke")
else:
    print("Fennec")

Submission status: Time Limit Exceeded

Solution code:
from collections import deque


n = int(input())
graph = [[] for i in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    graph[a - 1] += [b - 1]
    graph[b - 1] += [a - 1]



def dfs(graph, start, goal):
    que = deque([start])
    pre = [-1] * n
    while que:
        cur = que.popleft()
        for nxt in graph[cur]:
            if nxt == pre[cur]:
                continue
            pre[nxt] = cur
            if nxt == goal:
                break
            que.append(nxt)
    return pre




pre = dfs(graph, 0, n-1)
route = [n-1]
while route[0]!=0:
    route = [pre[route[0]]] + route

limit_num = (len(route) - 1) // 2
black_node = route[limit_num]
white_node = route[limit_num + 1]
graph[black_node].remove(white_node)


def dfs_cnt(graph, start):
    cnt = 1
    que = deque([start])
    pre = [-1] * n
    while que:
        cur = que.popleft()
        for nxt in graph[cur]:
            if nxt == pre[cur]:
                continue
            pre[nxt] = cur
            cnt += 1
            que.append(nxt)
    return cnt


black_cnt = dfs_cnt(graph, 0)
white_cnt = n - black_cnt


if black_cnt > white_cnt:
    print('Fennec')
else:
    print('Snuke')

Submission status: Time Limit Exceeded

Solution code:
from collections import deque


n = int(input())
graph = [[] for i in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    graph[a - 1] += [b - 1]
    graph[b - 1] += [a - 1]



def dfs(graph, start, goal):
    que = deque([start])
    pre = [-1] * n
    while que:
        cur = que.popleft()
        for nxt in graph[cur]:
            if nxt == pre[cur]:
                continue
            pre[nxt] = cur
            if nxt == goal:
                break
            que.append(nxt)
    return pre




pre = dfs(graph, 0, n-1)
route = [n-1]
while route[0]!=0:
    route = [pre[route[0]]] + route

limit_num = (len(route) - 1) // 2
black_node = route[limit_num]
white_node = route[limit_num + 1]
graph[black_node].remove(white_node)


def dfs_cnt(graph, start):
    que = deque([start])
    pre = [-1] * n
    while que:
        cur = que.popleft()
        for nxt in graph[cur]:
            if nxt == pre[cur]:
                continue
            pre[nxt] = cur
            que.append(nxt)
    cnt = len([i for i in pre if i!=-1])+1
    return cnt


black_cnt = dfs_cnt(graph, 0)
white_cnt = n - black_cnt


if black_cnt > white_cnt:
    print('Fennec')
else:
    print('Snuke')

Submission status: Time Limit Exceeded

Solution code:
from collections import deque


n = int(input())
graph = [[] for i in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    graph[a - 1] += [b - 1]
    graph[b - 1] += [a - 1]



def dfs(graph, start, goal):
    que = deque([start])
    pre = [-1] * n
    while que:
        cur = que.popleft()
        for nxt in graph[cur]:
            if nxt == pre[cur]:
                continue
            pre[nxt] = cur
            if nxt == goal:
                break
            que.append(nxt)
    return pre




pre = dfs(graph, 0, n-1)
route = [n-1]
while route[0]!=0:
    route = [pre[route[0]]] + route

limit_num = (len(route) - 1) // 2
black_node = route[limit_num]
white_node = route[limit_num + 1]
graph[black_node].remove(white_node)



def dfs_cnt(graph, start):
    que = deque([start])
    visited = [0] * n
    visited[start] = 1
    while que:
        cur = que.popleft()
        for nxt in graph[cur]:
            if visited[nxt] != 1:
                que.append(nxt)
                visited[nxt] = 1
    return sum(visited)


black_cnt = dfs_cnt(graph, 0)
white_cnt = n - black_cnt


if black_cnt > white_cnt:
    print('Fennec')
else:
    print('Snuke')

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
#ABC67 D

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7

n = int(input())
g = [[] for _ in range(n)]
w = [[float('inf')]*n for _ in range(n)]
for _ in range(n-1):
    a,b = map(int,input().split())
    g[a-1].append(b-1)
    g[b-1].append(a-1)
    w[a-1][b-1] = 1
    w[b-1][a-1] = 1

def dijkstra(s,n,w):
    dist = [float('inf')]*n
    used = [False]*n
    dist[s] = 0
    while 1:
        v = -1
        for i in range(n):
            if not used[i] and v == -1:
                v = i
            elif not used[i] and dist[i] < dist[v]:
                v = i
        if v == -1:
            break
        used[v] = True

        for j in range(n):
            dist[j] = min(dist[j],dist[v] + w[v][j])
    return dist

white,black = 0,0
d1 = dijkstra(0,n,w)
d2 = dijkstra(n-1,n,w)
for i in range(n):
    if d1[i] <= d2[i]:
        black += 1
    else:
        white += 1
if black > white:
    print('Fennec')
else:
    print('Snuke')

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
#ABC67 D

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7

n = int(input())
g = [[] for _ in range(n)]
w = [[float('inf')]*n for _ in range(n)]
for _ in range(n-1):
    a,b = map(int,input().split())
    g[a-1].append(b-1)
    g[b-1].append(a-1)
    w[a-1][b-1] = 1
    w[b-1][a-1] = 1

def dijkstra(s,n,w):
    dist = [float('inf')]*n
    used = [False]*n
    dist[s] = 0
    while 1:
        v = -1
        for i in range(n):
            if not used[i] and v == -1:
                v = i
            elif not used[i] and dist[i] < dist[v]:
                v = i
        if v == -1:
            break
        used[v] = True

        for j in range(n):
            dist[j] = min(dist[j],dist[v] + w[v][j])
    return dist

white,black = 0,0
for i in range(n):
    d = dijkstra(i,n,w)
    if d[0] <= d[n-1]:
        black += 1
    else:
        white += 1
if black > white:
    print('Fennec')
else:
    print('Snuke')

Submission status: Time Limit Exceeded

Solution code:
import collections
import sys
sys.setrecursionlimit(10**9)

def dfs(v):
  ret=0
  for u in g[v]:
    if checked[u]==0:
      checked[u]=1
      ret+=1+dfs(u)
  return ret

n=int(input())
g=[[] for _ in range(n+1)]
for _ in range(n-1):
  u,v=map(int,input().split())
  g[u].append(v)
  g[v].append(u)
q=collections.deque()
path=[]
checked=[0]*(n+1)
checked[1]=1
q.append((1,[]))
while 1:
  v,p=q.popleft()
  if v==n:
    path=p
    break
  for u in g[v]:
    if checked[u]==0:
      checked[u]=1
      p.append(u)
      q.append((u,p))
      p=p[:-1]
l=len(path)
if l==1:
  fg=n
  sg=1
else:
  fg=path[l//2]
  sg=path[l//2-1]
checked=[0]*(n+1)
checked[1]=1
checked[fg]=1
ans1=dfs(1)
ans2=(n-2)-ans1
if ans1>ans2:
  print('Fennec')
else:
  print('Snuke')

Submission status: Time Limit Exceeded

Solution code:
#recurtion limit extend
import sys
sys.setrecursionlimit(10**9)

n = int(input())

#get edges
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)

f2s = []

def dfs1(pre, cur, route):
    if cur == n - 1:
        global f2s
        f2s  = route
    else:
        for e in E[cur]:
            if e != pre:
                dfs1(cur, e, route[:] + [e])

def dfs2(pre, cur):
    global c
    c += 1
    for e in E[cur]:
        if e not in f2s and e != pre:
            dfs2(cur, e)

dfs1(-1, 0, [0])

t_snuke = len(f2s) // 2
s_score = 0

for e in f2s[-t_snuke:]:
    c = 0
    dfs2(-1, e)
    s_score += c

f_score = n - s_score

if f_score > s_score:
    print("Fennec")
else:
    print("Snuke")

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)


def solve(N: int, a: "List[int]", b: "List[int]"):
    conn = [[] for _ in range(N)]
    for i in range(N - 1):
        conn[a[i] - 1].append(b[i] - 1)
        conn[b[i] - 1].append(a[i] - 1)

    def find_path(idx, target, conn, visited):
        visited[idx] = True
        if idx == target:
            return [target]
        for nex in conn[idx]:
            if not visited[nex]:
                path = find_path(nex, target, conn, visited)
                if path is not None:
                    return [idx] + path
        return None

    visited = [False] * N
    path_to_n = find_path(0, N - 1, conn, visited)
    #print(path_to_n)

    colors = [None] * N
    for i, node in enumerate(path_to_n):
        if i < (len(path_to_n) + 1) // 2:
            colors[node] = 1
        else:
            colors[node] = 0

    def dfs(idx, conn, colors, visited):
        visited[idx] = True
        colors[idx] = 1
        for nex in conn[idx]:
            if not visited[nex] and (colors[nex] is None or colors[nex] == 1):
                dfs(nex, conn, colors, visited)
        return

    visited = [False] * N
    dfs(0, conn, colors, visited)
    #print(colors)

    cnt = 0
    for c in colors:
        if c == 1:
            cnt += 1
    if cnt > (N - cnt):
        ret = 'Fennec'
    else:
        ret = 'Snuke'
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int()] * (N-1)  # type: "List[int]" 
    b = [int()] * (N-1)  # type: "List[int]" 
    for i in range(N-1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    solve(N, a, b)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)


def solve(N: int, a: "List[int]", b: "List[int]"):
    conn = [[] for _ in range(N)]
    for i in range(N - 1):
        conn[a[i] - 1].append(b[i] - 1)
        conn[b[i] - 1].append(a[i] - 1)
    def find_path(idx, target, conn, visited):
        visited[idx] = True
        if idx == target:
            return [target]
        for nex in conn[idx]:
            if not visited[nex]:
                path = find_path(nex, target, conn, visited)
                if path is not None:
                    return [idx] + path
        return None

    visited = [False] * N
    path_to_n = find_path(0, N - 1, conn, visited)
    #print(path_to_n)

    colors = [None] * N
    for i, node in enumerate(path_to_n):
        if i < (len(path_to_n) + 1) // 2:
            colors[node] = 1
        else:
            colors[node] = 0

    def dfs(idx, conn, colors, visited):
        visited[idx] = True
        colors[idx] = 1
        for nex in conn[idx]:
            if not visited[nex] and (colors[nex] is None or colors[nex] == 1):
                dfs(nex, conn, colors, visited)
        return

    visited = [False] * N
    dfs(0, conn, colors, visited)
    #print(colors)

    cnt = 0
    for c in colors:
        if c == 1:
            cnt += 1
    if cnt > (N - cnt):
        ret = 'Fennec'
    else:
        ret = 'Snuke'
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int()] * (N-1)  # type: "List[int]" 
    b = [int()] * (N-1)  # type: "List[int]" 
    for i in range(N-1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    solve(N, a, b)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
input=sys.stdin.readline
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [input() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N = I()
    edge = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b = MI_()
        edge[a].append(b)
        edge[b].append(a)

    set_A = set()
    set_B = set()
    a_stack = [0]
    b_stack = [N - 1]
    for _ in range(N - 1):
        tmp_a = []
        tmp_b = []
        while a_stack:
            from_ = a_stack.pop()
            for to in edge[from_]:
                if to not in set_B:
                    tmp_a.append(to)
                    set_A.add(to)
        while b_stack:
            from_ = b_stack.pop()
            for to in edge[from_]:
                if to not in set_A:
                    tmp_b.append(to)
                    set_B.add(to)
        a_stack = tmp_a
        b_stack = tmp_b
    if len(set_A) > len(set_B):
        print("Fennec")
    else:
        print("Snuke")


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

import numpy as np

sys.setrecursionlimit(2 ** 31 - 1)
INF = float('inf')

N = int(sys.stdin.readline())
AB = [list(map(int, sys.stdin.readline().split())) for _ in range(N - 1)]

graph = [set() for _ in range(N + 1)]
for a, b in AB:
    graph[a].add(b)
    graph[b].add(a)


def dfs(to, path):
    if path[-1] == to:
        return path
    v = path[-1]
    prev = path[-2] if len(path) >= 2 else None

    for u in graph[v]:
        if u != prev:
            ret = dfs(to, path + [u])
            if ret:
                return ret
    return None


# 1 から N までのパス
path = dfs(N, [1])
cut = (len(path) - 1) // 2
fennec = path[:cut + 1]
snuke = path[cut + 1:]

fixed = np.zeros(N + 1, dtype=str)
fixed[fennec] = 'f'
fixed[snuke] = 's'

while fennec:
    v = fennec.pop()
    for u in graph[v]:
        if not fixed[u]:
            fixed[u] = 'f'
            fennec.append(u)
while snuke:
    v = snuke.pop()
    for u in graph[v]:
        if not fixed[u]:
            fixed[u] = 's'
            snuke.append(u)

if sum(fixed == 'f') > sum(fixed == 's'):
    print('Fennec')
else:
    print('Snuke')