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: Runtime Error

Solution code:
N = int(input())

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

col = [ -1 for i in range(N)]
col[0] = 0
def dfs(n,c):

    for v in e[n]:
        if col[v] == -1:
            col[v] = c + 1
            dfs(v,c+1)

dfs(0,0)

colf = [ -1 for i in range(N)]
colf[0] = 0
def dfsf(n,c):

    for v in e[n]:
        if v == N - 1:
            #colf[v] = c + 1
            continue
        elif colf[v] == -1:
            colf[v] = c + 1
            dfsf(v,c+1)
dfsf(0,0)

cols = [ -1 for i in range(N)]
cols[N-1] = 0
def dfss(n,c):

    for v in e[n]:
        if v == 0:
            #cols[v] = c + 1
            continue
        elif cols[v] == -1:
            cols[v] = c + 1
            dfss(v,c+1)
dfss(N-1,0)

fennec,snuke = 0,0

for i in range(N):
    if colf[i] != -1:
        fennec += 1
    if cols[i] != -1:
        snuke += 1

t = col[N-1] - 1
if t % 2 == 0:
    fennec += (t//2)
    snuke += (t//2)
else:
    fennec += (t//2) + 1
    snuke += (t//2)
    
if fennec > snuke:
    print("Fennec")
else:
    print("Snuke")

Submission status: Runtime Error

Solution code:
N = int(input())

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

col = [ -1 for i in range(N)]
col[0] = 0
def dfs(n,c):

    for v in e[n]:
        if col[v] == -1:
            col[v] = c + 1
            dfs(v,c+1)

dfs(0,0)

colf = [ -1 for i in range(N)]
colf[0] = 0
def dfsf(n,c):

    for v in e[n]:
        if v == N - 1:
            colf[v] = c + 1
            continue
        elif colf[v] == -1:
            colf[v] = c + 1
            dfsf(v,c+1)
dfsf(0,0)

cols = [ -1 for i in range(N)]
cols[N-1] = 0
def dfss(n,c):

    for v in e[n]:
        if v == 0:
            cols[v] = c + 1
            continue
        elif cols[v] == -1:
            cols[v] = c + 1
            dfss(v,c+1)
dfss(N-1,0)

#print(col)
#print(colf)
#print(cols)

for i in range(N):
    if colf[i] ==-1:
        colf[i] = 0
    if cols[i] == -1:
        cols[i] = 0

fenec,snuke = 0,0
fenec = sum(colf) - col[N-1]
snuke = sum(cols) - col[0]
if fenec > snuke:
    print("Fennec")
else:
    print("Snuke")

Submission status: Runtime Error

Solution code:
N = int(input())
edges = [[] for _ in range(N)]
for _ in range(N-1):
    a,b = map(int,input().split())
    edges[a-1].append(b-1)
    edges[b-1].append(a-1)
# visited = [False for _ in range(N)]
def dfs(start,stop,visitedArray):
    visitedArray[start] = True
    if start == stop:
        return
    else:
        for term in edges[start]:
            if visitedArray[term]:
                continue
            dfs(term,stop,visitedArray)
fennec = [False for _ in range(N)]
snuke = [False for _ in range(N)]
dfs(0,N-1,fennec)
dfs(N-1,0,snuke)
# print(fennec)
# print(snuke)
F = S = C = 0
for i in range(1,N-1):
    if fennec[i] and snuke[i]:
        C += 1
    else:
        if fennec[i]:
            F += 1
        else:
            S += 1
# F = fennec + common//2 + common%2
# S = snuke + common//2
# print(F,S,C)
F += C//2 + C%2
S += C//2
if F > S:
    print("Fennec")
else:
    print("Snuke")

Submission status: Runtime Error

Solution code:
N = int(input())
edges = [[] for _ in range(N)]
for _ in range(N-1):
    a,b = map(int,input().split())
    edges[a-1].append(b-1)
    edges[b-1].append(a-1)
visited = [False for _ in range(N)]
def dfs(start,stop):
    visited[start] = True
    if start == stop:
        return
    else:
        for term in edges[start]:
            if visited[term]: continue
            dfs(term,stop)
dfs(0,N-1)
import copy 
fromFennec = copy.deepcopy(visited)
visited = [False for _ in range(N)]
dfs(N-1,0)
fennec = common = snuke = 0
# print(fromFennec)
# print(visited)
for i in range(1,N-1):
    if fromFennec[i] and visited[i]:
        common += 1
    else:
        if fromFennec[i]:
            fennec += 1
        else:
            snuke += 1
F = fennec + common//2 + common%2
S = snuke + common//2
if F > S:
    print("Fennec")
else:
    print("Snuke")

Submission status: Runtime Error

Solution code:
N = int(input())
edges = [[] for _ in range(N)]
for _ in range(N-1):
    a,b = map(int,input().split())
    edges[a-1].append(b-1)
    edges[b-1].append(a-1)
visited = [False for _ in range(N)]
def dfs(start,stop):
    visited[start] = True
    if start == stop:
        return
    else:
        for term in edges[start]:
            if visited[term]: continue
            dfs(term,stop)
dfs(0,N-1)
import copy 
fromFennec = copy.deepcopy(visited)
visited = [False for _ in range(N)]
fennec = common = snuke = 0
for i in range(1,N-1):
    if fromFennec[i] and visited[i]:
        common += 1
    else:
        if fromFennec[i]:
            fennec += 1
        else:
            snuke += 1
F = fennec + common//2 + common%2
S = snuke + common//2
if F > S:
    print("Fennec")
else:
    print("Snuke")

Submission status: Runtime Error

Solution code:
from scipy.sparse import*
f=lambda*z:list(map(int,input().split()))
n,=f()
c=[1]*~-n
print('FSennunkeec'[sum(g>h for g,h in zip(*csgraph.dijkstra(csr_matrix((c,zip(*map(f,c))),[n+1]*2),0,[1,n])))*2>=n::2])

Submission status: Runtime Error

Solution code:
N=int(input())
import sys
ab=[[int(j) for j in sys.stdin.readline()] for _ in range (N-1)]
con=[[] for _ in range(N+1)]

for a,b in ab :
  con[a].append(b)
  con[b].append(a)

#black Fennec
pc=[0]*(N+1)
q=[(1,0)]
vis=[False]*(N+1)
vis[1]=True
Fcnt=N
while q:
  n,c=q.pop()
  pc[n]=c
  for ch in con[n]:
    if vis[ch]==True:
      continue
    q.append((ch,c+1))
    
q=[(N,0)]
vis=[False]*(N+1)
vis[1]=True
while q:
  n,c=q.pop()
  if pc[n]>c:
    Fcnt-=1
  for ch in con[n]:
    if vis[ch]==True:
      continue
    q.append((ch,c+1))
    
if Fcnt>N-Fcnt:
  print("Fennec")
else:
  print("Snuke")

Submission status: Runtime Error

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

prev = [-1]*N
dp = [0]*(N+1)
def DFS(start):
    res = 0
    for u in G[start]:
        if u != prev[start]:
            prev[u] = start
            res += DFS(u)
    dp[start] = res+1
    return res+1

DFS(0)
start = N-1
path = [N-1]
while True:
    path.append(prev[start])
    start = prev[start]
    if start == 0:
        break
path.reverse()
num = len(path)
c, d = (path[(num+1)//2-1], path[(num+1)//2])
G[c].remove(d)
G[d].remove(c)
if DFS(0) > DFS(N-1):
    print("Fennec")
else:
    print("Snuke")

Submission status: Runtime Error

Solution code:
def getN():
    return int(input())

def getMN():
    a = input().split()
    b = [int(i) for i in a]
    return b[0],b[1]

def getlist():
    a = input().split()
    b = [int(i) for i in a]
    return b

n = getN()
edge = [[] for i in range(n)]
for i in range(n-1):
    a,b = getlist()
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)

memo_b = [0] * n
#memo_b[0] = 1000000
memo_w = [0] * n

import queue
q = queue.Queue()

def bfs(v, distance, memo_target):
    q = queue.Queue()
    print("current vertex:{}".format(v+1))
    for vnext in edge[v]:
        if memo_target[vnext] == 0:
            print("find: {}, distance = {}".format(str(vnext+1),str(distance)))
            memo_target[vnext] = distance
            q.put(vnext)

    while not q.empty():
        bfs(q.get(), distance+1, memo_target)

bfs(0,1, memo_b)
bfs(n-1, 1, memo_w)
print(memo_b,memo_w)
ans = 0
for i, j in zip(memo_b, memo_w):
    if i <= j:
        ans += 1
    else:
        ans -= 1

print("Fennec") if ans > 0 else print("Snuke")

Submission status: Runtime Error

Solution code:
from scipy.sparse import*;f=lambda*z:map(int,input().split());n,=f();g,h=csgraph.dijkstra(csr_matrix(([1]*~-n,zip(*map(f,[0]*~-n))),[n+1]*2),0,[1,n]);print('FSennunkeec'[sum(g[i]>h[i]for i in range(n+1))*2>=n::2])

Submission status: Runtime Error

Solution code:
from scipy.sparse import*;f=lambda*z:map(int,input().split());n,=f();g,h=csgraph.dijkstra(csr_matrix(([1]*~-n,zip(*map(f,[0]*~-n))),[n+1]*2),0,[1,n]);print('FSennunkeec'[sum(g[i]>h[i]for i in range(n+1))*2/n::2])

Submission status: Runtime Error

Solution code:
from scipy.sparse import*;f=lambda*z:map(int,input().split());n,=f();a,b=zip(*map(f,[0]*~-n));g,h=csgraph.dijkstra(csr_matrix(([1]*~-n,(a,b)),[n+1]*2),0,[1,n]);print('FSennunkeec'[sum(g[i]>h[i]for i in range(n+1))*2/n::2])

Submission status: Runtime Error

Solution code:
import sys

def main():
    sys.setrecursionlimit(100001) # recursion limit hack
    buf = input()
    N = int(buf)
    a = []
    b = []
    for i in range(N-1):
        buf = input()
        buflist = buf.split()
        a.append(int(buflist[0]) - 1)
        b.append(int(buflist[1]) - 1)
    graph = []
    for i in range(N):
        graph.append([])
    for i in range(N-1):
        graph[a[i]].append(b[i])
        graph[b[i]].append(a[i])
    path = find_path(graph, 0, N-1, [])
    split_point = (len(path) + 1) // 2
    fennec_base = path[0:split_point]
    snuke_base = path[split_point:len(path)]
    fennec_vertex_count = len(fennec_base)
    snuke_vertex_count = len(snuke_base)
    for i, v in enumerate(fennec_base):
        for j in graph[v]:
            if i != 0:
                if j == fennec_base[i-1]:
                    continue
            if i != len(fennec_base) - 1:
                if j == fennec_base[i+1]:
                    continue
            else:
                if j == snuke_base[0]:
                    continue
            fennec_vertex_count += get_vertex_count(graph, v, j)
    for i, v in enumerate(snuke_base):
        for j in graph[v]:
            if i != 0:
                if j == snuke_base[i-1]:
                    continue
            else:
                if j == fennec_base[-1]:
                    continue
            if i != len(snuke_base) - 1:
                if j == snuke_base[i+1]:
                    continue
            snuke_vertex_count += get_vertex_count(graph, v, j)
    if fennec_vertex_count > snuke_vertex_count:
        print("Fennec")
    else:
        print("Snuke")

def find_path(graph, current, target, path):
    path.append(current)
    if current == target:
        return path
    for i in graph[current]:
        if len(path) >= 2:
            if i == path[-2]:
                continue
        result = find_path(graph, i, target, path)
        if result:
            return path
    path.pop()
    return None

def get_vertex_count(graph, base, next):
    count = 1
    for i in graph[next]:
        if i == base:
            continue
        count += get_vertex_count(graph, next, i)
    return count

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:

def main():
    buf = input()
    N = int(buf)
    a = []
    b = []
    for i in range(N-1):
        buf = input()
        buflist = buf.split()
        a.append(int(buflist[0]) - 1)
        b.append(int(buflist[1]) - 1)
    graph = []
    for i in range(N):
        graph.append([])
    for i in range(N-1):
        graph[a[i]].append(b[i])
        graph[b[i]].append(a[i])
    path = find_path(graph, 0, N-1, [])
    split_point = (len(path) + 1) // 2
    fennec_base = path[0:split_point]
    snuke_base = path[split_point:len(path)]
    fennec_vertex_count = len(fennec_base)
    snuke_vertex_count = len(snuke_base)
    for i, v in enumerate(fennec_base):
        for j in graph[v]:
            if i != 0:
                if j == fennec_base[i-1]:
                    continue
            if i != len(fennec_base) - 1:
                if j == fennec_base[i+1]:
                    continue
            else:
                if j == snuke_base[0]:
                    continue
            fennec_vertex_count += get_vertex_count(graph, v, j)
    for i, v in enumerate(snuke_base):
        for j in graph[v]:
            if i != 0:
                if j == snuke_base[i-1]:
                    continue
            else:
                if j == fennec_base[-1]:
                    continue
            if i != len(snuke_base) - 1:
                if j == snuke_base[i+1]:
                    continue
            snuke_vertex_count += get_vertex_count(graph, v, j)
    if fennec_vertex_count > snuke_vertex_count:
        print("Fennec")
    else:
        print("Snuke")

def find_path(graph, current, target, path):
    path.append(current)
    if current == target:
        return path
    for i in graph[current]:
        if len(path) >= 2:
            if i == path[-2]:
                continue
        result = find_path(graph, i, target, path)
        if result:
            return path
    path.pop()
    return None

def get_vertex_count(graph, base, next):
    count = 1
    for i in graph[next]:
        if i == base:
            continue
        count += get_vertex_count(graph, next, i)
    return count

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
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)

inf = 10**5

def bfs(visited, s):
    for e in edge[s]:
        if visited[e]==inf:
            visited[e] = visited[s]+1
            bfs(visited, e)


fvisited = [inf for _ in range(n+1)]
fvisited[n] = 0
bfs(fvisited, n)

svisited = [inf for _ in range(n+1)]
svisited[1] = 0
bfs(svisited, 1)

# print(fvisited)
# print(svisited)
fcnt = 0
scnt = 0
for i in range(1,n+1):
    if fvisited[i]<svisited[i]:
        fcnt += 1
    else:
        scnt += 1

if fcnt<scnt:
    print("Fennec")
else:
    print("Snuke")

Submission status: Runtime Error

Solution code:
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import *
from bisect import *

def read():
  return int(input())
 
def reads():
  return [int(x) for x in input().split()]

N = read()
edges = [[] for _ in range(N)]
for _ in range(N-1):
  a, b = reads()
  a, b = a-1, b-1
  edges[a].append(b)
  edges[b].append(a)

def dist(u, d = None):
  if d == None:
    d = [-1] * N
  for v in edges[u]:
    if d[v] >= 0:
      continue
    d[v] = d[u] + 1
    dist(v, d)
  return d

distf = dist(0)
dists = dist(N-1)

numf = sum(distf[i] >= dists[i] for i in range(N))
nums = N - numf

if numf >= nums:
  print("Fennec")
else:
  print("Snuke")

Submission status: Runtime Error

Solution code:
from collections import deque

def bfs(start):
    queue = deque([start])
    visited = []

    while queue:
        label = queue.pop()

       visited.append(label)

        for v in d[label]:
            if tekazu[v] == float("inf"):
                tekazu[v] = tekazu[label] + 1
                queue.appendleft(v)

    return

n = int(input())
d = [[] for _ in range(n)]

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

tekazu = [float("inf") for _ in range(n)]
tekazu[0] = 0
bfs(0)
tekazu_Fennec = tekazu

tekazu = [float("inf") for _ in range(n)]
tekazu[-1] = 0
bfs(n - 1)
tekazu_Snuke = tekazu

Fennec = 0
Snuke = 0

for i in range(n):
    if tekazu_Fennec[i] <= tekazu_Snuke[i]:
        Fennec += 1
    else:
        Snuke += 1

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

Submission status: Runtime Error

Solution code:
n = int(input())
p = []
for i in range(n-1):
    p.append(list(map(int, input().split()))+ [1])
l = [[] for i in range(n+1)]
for pi in p: 
    l[pi[0]].append([pi[0],pi[1]])
    l[pi[1]].append([pi[1],pi[0]])
kyo = [float("inf")]*(n+1)
kyo[1] = 0
memo = [0] * (n+1)
memo[1] = 1
ruto = []
def dfs(start,ruto):
  ruto.extend(l[start])
  memo[start] = 1
  while ruto:
    a = ruto.pop()
    if memo[a[1]] == 1:
      continue
    kyo[a[1]] = kyo[a[0]] + 1
    dfs(a[1],ruto)
  return
dfs(1,ruto)

nkyo = [float("inf")]*(n+1)
nkyo[n] = 0
memo = [0] * (n+1)
memo[n] = 1
ruto = []
def dfs(start,ruto):
  ruto.extend(l[start])
  memo[start] = 1
  while ruto:
    a = ruto.pop()
    if memo[a[1]] == 1:
      continue
    nkyo[a[1]] = nkyo[a[0]] + 1
    dfs(a[1],ruto)
  return
dfs(n,ruto)
iti = 0
nko = 0
for i in range(1,n+1):
  if kyo[i] <= nkyo[i]:
    iti += 1
  else:
    nko += 1
if iti <= nko:
  print("Snuke")
else:
  print("Fennec")

Submission status: Runtime Error

Solution code:
n = int(input())
p = []
for i in range(n-1):
    p.append(list(map(int, input().split()))+ [1])
lw = [[] for i in range(n+1)]
for path in p:
    lw[path[0]].append((path[1],path[2]))
    lw[path[1]].append((path[0],path[2]))
kyo = [float("inf")]*(n+1)
kyo[1] = 0
memo = [0] * (n+1)
memo[1] = 1
ruto = []
def dfs(start,ruto):
  ruto.extend(lw[start])
  memo[start] = 1
  while ruto:
    a = ruto.pop()
    if memo[a[0]] == 1:
      continue
    kyo[a[0]] = kyo[start] + 1
    dfs(a[0],ruto)
  return
dfs(1,ruto)

nkyo = [float("inf")]*(n+1)
nkyo[n] = 0
memo = [0] * (n+1)
memo[n] = 1
ruto = []
def dfs(start,ruto):
  ruto.extend(lw[start])
  memo[start] = 1
  while ruto:
    a = ruto.pop()
    if memo[a[0]] == 1:
      continue
    nkyo[a[0]] = nkyo[start] + 1
    dfs(a[0],ruto)
  return
dfs(n,ruto)
iti = 0
nko = 0
for i in range(n):
  if kyo[i] <= nkyo[i]:
    iti += 1
  else:
    nko += 1
if iti <= nko:
  print("Snuke")
else:
  print("Fennec")

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
#import sys
N=int(input())

edge=[[] for i in range(N)]

for i in range(N-1):
        a,b=[int(j)-1 for j in input().split()]
        edge[a].append(b)
        edge[b].append(a)
#入力終了

step=[999999999 for i in range(N)]

def paint(here,dist):
        step[here]=dist
        for to in edge[here]:
                if step[to]>dist+1:
                        paint(to,dist+1)
paint(0,0)
#ここまでは正しい
#stepはindex=0からの距離です
print("Snuke")

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
#import sys
N=int(input())

edge=[[] for i in range(N)]

for i in range(N-1):
        a,b=[int(j)-1 for j in input().split()]
        edge[a].append(b)
        edge[b].append(a)
#入力終了

step=[999999999 for i in range(N)]

def paint(here,dist):
        step[here]=dist
        for to in edge[here]:
                if step[to]>dist+1:
                        paint(to,dist+1)
paint(0,0)
#ここまでは正しい
#stepはindex=0からの距離です

nowL=N-1
dist=int((step[nowL]-1)/2)
if dist>N:
        print("Snuke")
else:
        while dist>0:
                for to in edge[nowL]:
                        if step[to]==step[nowL]-1:
                                nowL=to
                                break
                dist-=1
        print("Snuke")
#Nから0の方へ戻っていきます

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
#import sys
N=int(input())

edge=[[] for i in range(N)]

for i in range(N-1):
        a,b=[int(j)-1 for j in input().split()]
        edge[a].append(b)
        edge[b].append(a)
#入力終了

step=[999999999 for i in range(N)]

def paint(here,dist):
        step[here]=dist
        for to in edge[here]:
                if step[to]>dist+1:
                        paint(to,dist+1)
paint(0,0)
#ここまでは正しい
#stepはindex=0からの距離です

nowL=N-1
dist=int((step[-1]-1)/2)
while dist>0:
        count=0
        for to in edge[nowL]:
                if step[to]<step[nowL]:
                        nowL=to
                        count+=1
        if count!=1:
                print("Error")
                sys.exit()
        dist-=1
#Nから0の方へ戻っていきます
print("Fennec")

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
#import sys
N=int(input())

edge=[[] for i in range(N)]

for i in range(N-1):
        a,b=[int(j)-1 for j in input().split()]
        edge[a].append(b)
        edge[b].append(a)
#入力終了

step=[999999999 for i in range(N)]

def paint(here,dist):
        step[here]=dist
        for to in edge[here]:
                if step[to]>dist+1:
                        paint(to,dist+1)
paint(0,0)
#ここまでは正しい
#stepはindex=0からの距離です

nowL=N-1
dist=int((step[-1]-1)/2)
while dist>0:
        count=0
        for to in edge[nowL]:
                if step[to]<step[nowL]:
                        nowL=to
                        count+=1
        if count!=1:
                print("Error")
                sys.exit()
        dist-=1
#Nから0の方へ戻っていきます

nextL=-1
count=0
for to in edge[nowL]:
        if step[to]<step[nowL]:
                nextL=to
                count+=1
if count!=1:
        print("Error")
        sys.exit()
if nextL<0 or N-1<nextL:
        print("What!?")
else:
        if nextL in edge[nowL]:
                edge[nowL].remove(nextL)
        else:
                print("Error")
                sys.exit()
        if nowL in edge[nextL]:
                edge[nextL].remove(nowL)
        else:
                print("Error")
                sys.exit()
        #連結を一か所切りましょう！

        used=[False for i in range(N)]

        def search(place):
                used[place]=True
                ans=1
                for to in edge[place]:
                        if not used[to]:
                                ans+=(search(to))
                return ans
        cnt0=search(0)
        cntN=search(N-1)

        if cnt0>cntN:
                print("Fennec")
        else:
                print("Snuke")

Submission status: Runtime Error

Solution code:
    #相手のパスを潰したい
    # こちら側に来てほしくない場合,最短経路を潰すしか無い
    # 最適を簡単に表せない A - B - C - D
    #                EF-|   |-GH...
    # のとき、A→EFとやっていると、Bを取られて死ぬ
    # でもこれをいちいち計算できない
    # 「相手のパスを潰す」、というのは、最短経路がより短いほうが勝つと考える?
    # でも相手にパスが潰されるかどうかはわからない
    # とりあえずやってみる
    n = int(input())
    INF = 10**6
    graph = [[float("inf")]*n for i in range(n)]
    for i in range(n - 1):
        a, b = map(int, input().split())
        graph[a - 1][b -1] = 1
        graph[b - 1][a - 1] = 1
     
    #ダイクストラで0, n-1からの最短距離を求める
     
     
    import heapq
     
    class Dijkstra(object):
        def dijkstra(self, adj, start, goal=None):
            '''
            ダイクストラアルゴリズムによる最短経路を求めるメソッド
            入力
            adj: adj[i][j]の値が頂点iから頂点jまでの距離(頂点iから頂点jに枝がない場合，値はfloat('inf'))となるような2次元リスト(正方行列)
            start: 始点のID
            goal: オプション引数．終点のID
            出力
            goalを引数に持つ場合，startからgoalまでの最短経路を格納したリストを返す
            持たない場合は，startから各頂点までの最短距離を格納したリストを返す
            >>> d = Dijkstra()
            >>> d.dijkstra([[float('inf'), 2, 4, float('inf'), float('inf')], [2, float('inf'), 3, 5, float('inf')], [4, 3, float('inf'), 1, 4], [float('inf'), 5, 1, float('inf'), 3], [float('inf'), float('inf'), 4, 3, float('inf')]], 0)
            [0, 2, 4, 5, 8] # 例えば，始点0から頂点3までの最短距離は5となる
            >>> d.dijkstra([[float('inf'), 2, 4, float('inf'), float('inf')], [2, float('inf'), 3, 5, float('inf')], [4, 3, float('inf'), 1, 4], [float('inf'), 5, 1, float('inf'), 3], [float('inf'), float('inf'), 4, 3, float('inf')]], 0, goal=4)
            [0, 2, 4] # 頂点0から頂点4までの最短経路は0 -> 2 -> 4となる
            '''
            num = len(adj)          # グラフのノード数
            dist = [float('inf') for i in range(num)] # 始点から各頂点までの最短距離を格納する
            prev = [float('inf') for i in range(num)] # 最短経路における，その頂点の前の頂点のIDを格納する
            
            dist[start] = 0
            q = []                  # プライオリティキュー．各要素は，(startからある頂点vまでの仮の距離, 頂点vのID)からなるタプル
            heapq.heappush(q, (0, start)) # 始点をpush
     
            while len(q) != 0:
                prov_cost, src = heapq.heappop(q) # pop
                
                # プライオリティキューに格納されている最短距離が，現在計算できている最短距離より大きければ，distの更新をする必要はない
                if dist[src] < prov_cost:
                    continue
     
                # 他の頂点の探索
                for dest in range(num):
                    cost = adj[src][dest]
                    if cost != float('inf') and dist[dest] > dist[src] + cost:
                        dist[dest] = dist[src] + cost # distの更新
                        heapq.heappush(q, (dist[dest], dest)) # キューに新たな仮の距離の情報をpush
                        prev[dest] = src                      # 前の頂点を記録
                        
            if goal is not None:
                return self.get_path(goal, prev)
            else:
                return dist
     
        def get_path(self, goal, prev):
            '''
            始点startから終点goalまでの最短経路を求める
            '''
            path = [goal]           # 最短経路
            dest = goal
     
            # 終点から最短経路を逆順に辿る
            while prev[dest] != float('inf'):
                path.append(prev[dest])
                dest = prev[dest]
     
            # 経路をreverseして出力
            return list(reversed(path))
    d = Dijkstra()
    #print(d.dijkstra(graph, start=0))
    #print(d.dijkstra(graph, start=n-1))
    cnt = 0
    for i, j in zip(d.dijkstra(graph, start=0,), d.dijkstra(graph, start=n-1)):
        if i <= j:
            cnt += 1
    print("Fennec" if cnt > n//2 else "Snuke")

Submission status: Runtime Error

Solution code:
N=int(input())

edge=[[] for i in range(N)]

for i in range(N-1):
        a,b=[int(i)-1 for i in input().split()]
        edge[a].append(b)
        edge[b].append(a)
#入力終了

step=[999999999 for i in range(N)]
step[0]=0

def paint(here,dist):
        for to in edge[here]:
                if step[to]>dist+1:
                        step[to]=dist+1
                        paint(to,dist+1)
paint(0,0)
#ここまでは正しい
#stepはindex=0からの距離です

nowL=N-1
dist=int((step[-1]-1)/2)
while dist>0:
        for to in edge[nowL]:
                if step[to]<step[nowL]:
                        nowL=to
                        break
        dist-=1
#Nから0の方へ戻っていきます

nextL=-1
for to in edge[nowL]:
        if step[to]<step[nowL]:
                nextL=to
                break
if nextL==-1:
        print("What!?")
else:
        edge[nowL].remove(nextL)
        edge[nextL].remove(nowL)
        #連結を一か所切りましょう！

        used=[False for i in range(N)]

        def search(place):
                ans=1
                for to in edge[place]:
                        if not used[to]:
                                used[to]=True
                                ans+=(search(to))
                return ans
        used[0]=True
        used[N-1]=True
        cnt0=search(0)
        cntN=search(N-1)

        if cnt0>cntN:
                print("Fennec")
        else:
                print("Snuke")

Submission status: Runtime Error

Solution code:
N=int(input())

edge=[[] for i in range(N)]

for i in range(N-1):
        a,b=[int(i)-1 for i in input().split()]
        edge[a].append(b)
        edge[b].append(a)
#入力終了

step=[999999999 for i in range(N)]
step[0]=0

def paint(here,dist):
        for to in edge[here]:
                if step[to]>dist+1:
                        step[to]=dist+1
                        paint(to,dist+1)
paint(0,0)
#ここまでは正しい
#stepはindex=0からの距離です

nowL=N-1
dist=int((step[-1]-1)/2)
while dist>0:
        for to in edge[nowL]:
                if step[to]<step[nowL]:
                        nowL=to
                        break
        dist-=1
#Nから0の方へ戻っていきます

nextL=-1
for to in edge[nowL]:
        if step[to]<step[nowL]:
                nextL=to
                break
edge[nowL].remove(nextL)
edge[nextL].remove(nowL)
#連結を一か所切りましょう！

used=[False for i in range(N)]

def search(place):
        ans=0
        for to in edge[place]:
                if not used[to]:
                        used[to]=True
                        ans+=(search(to)+1)
        return ans
cnt0=search(0)
cntN=search(N-1)

if cnt0>cntN:
        print("Fennec")
else:
        print("Snuke")

Submission status: Runtime Error

Solution code:
#!usr/bim/python
#! -*- coding: utf-8 -*-

import numpy as np

n = raw_input()
n = int(n)

tree = np.zeros([n,n],dtype = np.uint8)

i = 0
while i < n-1:
    ab = raw_input()
    ab = ab.split()
    ab = map(int,ab)
    tree[ab[0]-1,ab[1]-1]=1
    tree[ab[1]-1,ab[0]-1]=1
    i += 1

black = np.tile([n],n)
white = np.tile([n],n)

black[0] = 0
white[n-1] = 0

#black_before_list = []
#white_before_list = []

#black_tree = np.array(tree,dtype=np.uint8)
#white_tree = np.array(tree,dtype=np.uint8)

#white_tree = np.array(black_tree,dtype=np.uint8)

i = 0
#while np.amax(black) >= n or np.amax(white) >= n:
#    #print i
#    if i >= 1:
#        black_before_list = np.where(black == i-1)
#        white_before_list = np.where(white == i-1)
#        black_before_list = black_before_list[0]
#        white_before_list = white_before_list[0]
#    black_i_list = np.where(black == i)
#    white_i_list = np.where(white == i)
#    black_i_list = black_i_list[0]
#    white_i_list = white_i_list[0]
#    #black_tree[:,black_before_list] = 0
#    #white_tree[:,white_before_list] = 0
#    temp_tree = tree[black_i_list]
#    temp_tree[:,black_before_list] = 0
#    black_next_list = np.where(temp_tree == 1)
#    temp_tree = tree[black_i_list]
#    temp_tree[:,black_before_list] = 0
#    white_next_list = np.where(white_tree[white_i_list] == 1)
#    black_next_list = black_next_list[1]
#    white_next_list = white_next_list[1]
#    black[black_next_list] = i + 1
#    white[white_next_list] = i + 1
#    #    print "black\t", black
#    #    print "white\t", white
#
#    i += 1

#black

before_list = []
i = 0
while np.amax(black) >= n:
    #print i
    if i >= 1:
        before_list = np.where(black == i-1)[0]
    i_list = np.where(black == i)[0]
    new_tree = tree[i_list]
    new_tree[:,before_list] = 0
    next_list = np.where(new_tree == 1)[1]
    black[next_list] = i + 1
    i += 1

before_list = []
i = 0
while np.amax(white) >= n:
    #print i
    if i >= 1:
        before_list = np.where(white == i-1)[0]
    i_list = np.where(white == i)[0]
    new_tree = tree[i_list]
    new_tree[:,before_list] = 0
    next_list = np.where(new_tree == 1)[1]
    white[next_list] = i + 1
    i += 1

status = 0

i = 0
while i < n:
    if black[i] <= white[i]:
        status += 1
    else:
        status -= 1
    i += 1

if status > 0:
    print "Fennec"
else:
    print "Snuke"