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: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a, b = LI()
    if not (a % 3 and b % 3 and (a + b) % 3):
        print("Possible")
    else:
        print("Impossible")
    return

#B
def B():
    _, n = LI()
    l = LI()
    l.sort(reverse = True)
    print(sum(l[:n]))
    return

#C
def C():
    n = II()
    a = LI()
    for i in range(n-1):
        a[i + 1] += a[i]
    maxa = a[-1]
    ans = inf
    for i in range(n-1):
        ans = min(ans, abs(2 * a[i] - maxa))
    print(ans)
    return

#D
def D():
    n = II()
    edg = [[] for i in range(n)]
    for _ in range(n - 1):
        a, b = LI_()
        edg[a].append(b)
        edg[b].append(a)
    rank = [0] * n
    q = deque()
    q.append(0)
    rank[0] = 1
    while q:
        p = q.pop()
        for i in edg[p]:
            if rank[i]:
                continue
            rank[i] = rank[p] + 1
            q.appendleft(i)
    k = rank[n - 1] - (rank[n - 1] - 2) // 2
    q = deque()
    q.append(n - 1)
    while q:
        p = q.pop()
        for i in edg[p]:
            if rank[i] < rank[p]:
                if rank[i] == k:
                    a = i
                    break
                q.append(i)
    ans1 = 0
    ans2 = 0
    check = [True] * n
    q = deque()
    q.append(a)
    check[a] = False
    while q:
        p = q.pop()
        for i in edg[p]:
            if rank[i] > rank[p]:
                q.append(i)
                ans1 += 1
                check[i] = False
    q = deque()
    q.append(0)
    check[0] = False
    while q:
        p = q.pop()
        for i in edg[p]:
            if check[i]:
                q.append(i)
                ans2 += 1
                check[i] = False
    print(["Fennec", "Snuke"][ans1 <= ans2])
    return

#Solve
if __name__ == '__main__':
    D()

Submission status: Wrong Answer

Solution code:

from collections import deque

# BFSで、B/Wそれぞれのスタート地点から拡張点への距離を取得
def bfs(s, distance):
    visited = [False] * N
    q = [(s, 1)]
    visited[s] = True

    while q:
        u, dist = q.pop()
        for v in graph[u]:
            if not visited[v]:
                visited[v] = True
                distance[v] = dist
                q.append((v, dist+1))

#    print(distance)


N = int(input())
graph = [deque() for i in range(N)]
distance = [[0] * N for i in range(2)]

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

#print(graph)

bfs(0, distance[0])
bfs(N-1, distance[1])

# 各頂点について、より近いほうが塗れたことに
b = w = 0
for i in range(1, N):
    if distance[0][i] <= distance[1][i]:
        b += 1
    else:
        w += 1
if b <= w:
    print('Snuke')
else:
    print('Fennec')

Submission status: Wrong Answer

Solution code:

from collections import deque

# BFSで、B/Wそれぞれのスタート地点から拡張点への距離を取得
def bfs(s, distance):
    visited = [False] * N
    q = [(s, 1)]
    visited[s] = True

    while q:
        u, dist = q.pop()
        for v in graph[u]:
            if not visited[v]:
                visited[v] = True
                distance[v] = dist
                q.append((v, dist+1))
#    print(distance)


N = int(input())
graph = [deque() for i in range(N)]
distance = [[0] * (N) for i in range(2)]

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

#print(graph)

bfs(0, distance[0])
bfs(N-1, distance[1])

# 各頂点について、より近いほうが塗れたことに
b = w = 0
for i in range(1, N):
    if distance[0][i] <= distance[1][i]:
        b += 1
    else:
        w += 1
if b <= w:
    print('Snuke')
else:
    print('Fennec')

Submission status: Wrong Answer

Solution code:

from collections import deque

def bfs(s, dist):
    visited = [False] * (N+1)
    q = [(s, 1)]
    visited[s] = True

    while q:
        u, c = q.pop()
        for v in graph[u]:
            if not visited[v]:
                visited[v] = True
                dist[v] = c
                q.append((v, c+1))


N = int(input())
graph = [deque() for i in range(N+1)]
dist = [[0] * (N+1) for i in range(2)]

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

#print(graph)

bfs(0, dist[0])
bfs(N-1, dist[1])

b = w = 0
for i in range(2, N):
    if dist[0][i] <= dist[1][i]:
        b += 1
    else:
        w += 1
if b <= w:
    print('Snuke')
else:
    print('Fennec')

Submission status: Wrong Answer

Solution code:

from collections import deque

def bfs(s, dist):
    visited = [False] * (N+1)
    q = [(s, 1)]
    visited[s] = True

    while q:
        u, c = q.pop()
        for v in graph[u]:
            if not visited[v]:
                visited[v] = True
                dist[v] = c
                q.append((v, c+1))


N = int(input())
graph = [deque() for i in range(N+1)]
dist = [[0] * (N+1) for i in range(2)]

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

print(graph)

bfs(0, dist[0])
bfs(N-1, dist[1])

b = w = 0
for i in range(2, N):
    if dist[0][i] <= dist[1][i]:
        b += 1
    else:
        w += 1
if b <= w:
    print('Snuke')
else:
    print('Fennec')

Submission status: Wrong Answer

Solution code:

from collections import deque

def bfs(s, dist):
    visited = [False] * (N+1)
    q = [(s, 1)]
    visited[s] = True

    while q:
        u, c = q.pop()
        for v in graph[u]:
            if not visited[v]:
                visited[v] = True
                dist[v] = c
                q.append((v, c+1))


N = int(input())
graph = [deque() for i in range(N)]
dist = [[0] * (N+1) for i in range(2)]

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

#print(graph)

bfs(0, dist[0])
bfs(N-1, dist[1])

b = w = 0
for i in range(2, N):
    if dist[0][i] <= dist[1][i]:
        b += 1
    else:
        w += 1
if b <= w:
    print('Snuke')
else:
    print('Fennec')

Submission status: Wrong Answer

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
##############################

N = int(input())
tree = [ [0 for _ in range(N)] for _ in range(N) ]
colors = [False] * N

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

colors[0] = 'B'
colors[N-1] = 'W'

def bfs(black, white, turn):
    while True:
        if turn == 0:
            char = 'B'
            q = bq
        else:
            char = 'W'
            q = wq

#        print(colors)

        idx = q.pop(0)
        colors[idx] = char

        # キューに入れる
        for i in range(N):
            if tree[idx][i] == 0: continue
            if colors[i] != False: continue

            q.append(i)

        if len(q) == 0:
            if turn == 0:
                print('Snuke')
            else:
                print('Fennec')
            exit()

        turn ^= 1

# 0 = 先手, 1 = 後手
turn = 0
bq = [0]
wq = [N-1]
bfs(0, N-1, turn)

Submission status: Wrong Answer

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
##############################

N = int(input())
tree = [ [0 for _ in range(N)] for _ in range(N) ]
colors = [False] * N

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

colors[0] = 'B'
colors[N-1] = 'W'

def bfs(black, white, turn):
    while True:
        if turn == 0:
            char = 'B'
            q = bq
        else:
            char = 'W'
            q = wq

#        print(colors)

        if len(q) == 0:
            if turn == 0:
                print('Snuke')
            else:
                print('Fennec')
            exit()

        idx = q.pop(0)
        colors[idx] = char

        # キューに入れる
        for i in range(N):
            if tree[idx][i] == 0: continue
            if colors[i] != False: continue

            q.append(i)

        turn ^= 1

# 0 = 先手, 1 = 後手
turn = 0
bq = [0]
wq = [N-1]
bfs(0, N-1, turn)

Submission status: Wrong Answer

Solution code:
from collections import deque

def bfs(s, dist):
    visited = [False] * (N+1)
    q = [(s, 1)]
    visited[s] = True

    while q:
        u, c = q.pop()
        for v in graph[u]:
            if not visited[v]:
                visited[v] = True
                dist[v] = c
                q.append((v, c+1))

def main():
    dist = [[0] * (N+1) for i in range(2)]
    
    for i in range(N-1):
        a, b = map(int, input().split())
        graph[a].append(b)
        graph[b].append(a)
        
    bfs(1, dist[0])
    bfs(N, dist[1])
    
    b = w = 0
    for i in range(1, N):
        if dist[0][i] <= dist[1][i]:
            b += 1
        else:
            w += 1
    if b <= w:
        print('Snuke')
    else:
        print('Fennec')

if __name__ == '__main__':
    N = int(input())
    graph = [deque() for i in range(N+1)]

    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

def bfs(s, dist):
    visited = [False] * (N+1)
    q = [(s, 1)]
    visited[s] = True

    while q:
        u, c = q.pop()
        for v in graph[u]:
            if not visited[v]:
                visited[v] = True
                dist[v] = c
                q.append((v, c+1))

def main():
    dist = [[0] * (N+1) for i in range(2)]
    
    for i in range(N-1):
        a, b = map(int, input().split())
        graph[a].append(b)
        graph[b].append(a)
        
    bfs(1, dist[0])
    bfs(N, dist[1])
    
    b = w = 0
    for i in range(1, N):
        if dist[0][i] <= dist[1][i]:
            b += 1
        else:
            w += 1
    if b < w:
        print('Snuke')
    else:
        print('Fennec')

if __name__ == '__main__':
    N = int(input())
    graph = [deque() for i in range(N+1)]
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
g = [[] for _ in range(n)]

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

from collections import deque
f = deque()
s = deque()

f.append(0)
s.append(n-1)

visit = [-1]*n
visit[0] = 1
visit[n-1] = 0

def bfs(queue, c, competitor):
  if queue:
    x = queue.popleft()
    if all(visit[new_x] != -1 for new_x in g[x]):
      bfs(queue, c, competitor)
    else:
      for new_x in g[x]:
        if visit[new_x] == -1:
          visit[new_x] = c
          queue.append(new_x)
        else:
          continue
  else:
    print(competitor)
    exit()

while f and s:
  bfs(f, 1, 'Snuke')
  bfs(s, 0, 'Fennec')
  
if len(f) == 0:
  print('Snuke')
else:
  print('Fennec')

Submission status: Wrong Answer

Solution code:
n = int(input())
g = [[] for _ in range(n)]

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

from collections import deque
f = deque()
s = deque()

f.append(0)
s.append(n-1)

visit = [-1]*n
visit[0] = 1
visit[n-1] = 0

while f and s:
  b = f.popleft()
  for new_b in g[b]:
    if visit[new_b] == -1:
      visit[new_b] = 1
      f.append(new_b)
    else:
      continue
  w = s.popleft()
  for new_w in g[w]:
    if visit[new_w] == -1:
      visit[new_w] = 0
      s.append(new_w)
    else:
      continue 
if len(f) == 0:
  print('Snuke')
else:
  print('Fennec')

Submission status: Wrong Answer

Solution code:
from heapq import *
n=int(input())
s=[[]for i in range(n+1)]
for i in range(n-1):
    a,b=map(int,input().split())
    s[a].append(b)
    s[b].append(a)
def f(a):
    p=[float("INF")for i in range(n+1)]
    p[a],q=0,[[0,a]]
    while q:
        i,j=heappop(q)
        for w in s[j]:
            if p[w]>i+1:
                p[w]=i+1
                heappush(q,[i+1,w])
    else:
        return p[1:]
s1,t1=f(1),f(n)
a,b=0,0
for i in range(n):
    if s1[i]<=t1[i]:
        a+=1
    else:
        b+=1
print("fennec" if a>b else "snuke")

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[[]for i in range(n+1)]
for i in range(n-1):
    a,b=map(int,input().split())
    s[a].append(b)
    s[b].append(a)
def f(a,b):
    c,flag,ans=[[a,[0,a]]],False,[]
    while c or flag==False:
        p,q=c.pop()
        for i in s[p]:
            if i==b:
                ans=q[1:]+[b]
                flag=True
            else:
                if q[-2]!=i:
                    c.append([i,q+[i]])
    else:
        return ans
def g(x,l):
    c=0
    p,d=[x],[0]*(n+1)
    while p:
        a=p.pop()
        if a not in l or a==x:
            c+=1
            d[a]=1
            for i in s[a]:
                if d[i]==0:
                    p.append(i)
    else:
        return c
t=f(a,b)
t=[g(i,t)for i in t]
print("Fennec"if sum(t[:-len(t)//2])+len(t)-len(t)//2>sum(t[-len(t)//2:])+len(t)//2 else"Snuke")

Submission status: Wrong Answer

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 = 0
    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: Wrong Answer

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 pre[nxt] == cur:
                continue
            pre[nxt] = cur
            que.append(nxt)
    cnt = sum([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: Wrong Answer

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()
    que.append((start, [start]))
    visited = [0] * n
    visited[start] = 1
    while que:
        cur, path = que.popleft()
        for nxt in graph[cur]:
            if nxt == goal:
                path += [nxt]
                break
            if visited[nxt] != 1:
                que.append((nxt, path + [nxt]))
                visited[nxt] = 1
    return path


path = dfs(graph, 0, n-1)
limit_num = (len(path) - 1) // 2
black_node = path[limit_num]
white_node = path[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: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
import math
from collections import defaultdict, deque
import heapq

INF = float("inf")


class Graph(object):
    """グラフを扱うオブジェクト
    """

    def __init__(self, N):
        self.N = N
        self.edges = defaultdict(list)
        pass

    def add_edge(self, from_, to_, weight=1):
        """必ず0インデックスで入力すること。
        """
        self.edges[from_].append([to_, weight])
        self.edges[to_].append([from_, weight])

    def remove_edge(self, from_, to_):
        for t_, w_ in self.edges[from_]:
            if t_ == to_:
                break
        self.edges[from_].remove([t_, w_])
        self.edges[t_].remove([from_, w_])


def shortestPath(g: Graph, s: int):
    """ グラフgにおいて、始点sから各頂点への最短路を求める
    引数
    g: グラフ, s: 始点
    返り値
    dist: 始点からの距離が格納されたリスト
    prev: 始点から最短経路で移動する場合、各頂点に至る前の頂点のリスト
    """
    dist = [INF]*g.N
    dist[s] = 0

    prev = [None]*g.N
    Q = []
    heapq.heappush(Q, (dist[s], s))

    while len(Q) > 0:
        _, u = heapq.heappop(Q)
        for v, w in g.edges[u]:
            if dist[v] > dist[u] + w:
                dist[v] = dist[u] + w
                prev[v] = u
                heapq.heappush(Q, (dist[v], v))
    return dist, prev


class UnionFind(object):
    """UnionFind木
    """

    def __init__(self, N):
        self.tree = list(range(N))

    def root(self, i):
        if self.tree[i] == i:
            return i
        else:
            self.tree[i] = self.root(self.tree[i])
            return self.tree[i]

    def same(self, x, y):
        return self.root(x) == self.root(y)

    def unite(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x != y:
            self.tree[x] = y


def solve(N: int, a: "List[int]", b: "List[int]"):
    g = Graph(N)
    for x, y in zip(a, b):
        g.add_edge(x-1, y-1, 1)

    dist, prev = shortestPath(g, 0)

    depth = dist[N-1]
    boundary = N-1
    for _ in range((depth + 1) // 2):
        boundary = prev[boundary]
    g.remove_edge(boundary, prev[boundary])

    visited = [False]*N
    countA = 0
    q = deque()
    q.append(0)
    while len(q) > 0:
        curr = q.popleft()
        visited[curr] = True
        countA += 1
        for t_, w_ in g.edges[curr]:
            if visited[t_] == False:
                q.append(t_)
    countB = 0
    q = deque()
    q.append(N-1)
    while len(q) > 0:
        curr = q.popleft()
        visited[curr] = True
        countB += 1
        for t_, w_ in g.edges[curr]:
            if visited[t_] == False:
                q.append(t_)
    if countA > countB:
        print("Fennec")
    else:
        print("Snuke")
    # print(countA, countB)

    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: Wrong Answer

Solution code:
import collections

n = int(input())
nxts = collections.defaultdict(list)
for i in range(n - 1):
    a, b = map(int, input().split())
    nxts[a - 1].append(b - 1)
    nxts[b - 1].append(a - 1)

def f(first):
    d = [-1] * n
    d[first] = 0
    stack = [first]
    while stack:
        current = stack.pop()
        for nxt in nxts[current]:
            if d[nxt] == -1:
                d[nxt] = d[current] + 1
                stack.append(nxt)
    return d

d1 = f(0)
d2 = f(n - 1)
c1 = sum(1 for x1, x2 in zip(d1, d2) if x1 <= x2)
c2 = n - c1


if c1 >= c2:
    print('Fennec')
else:
    print('Snuke')

Submission status: Wrong Answer

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
d = dijkstra(0,n,w)
if d[n-1] % 2:
    print('Snuke')
else:
    print('Fennec')

Submission status: Wrong Answer

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

def dfs(v):
  if len(g[v])==1:
    return 0
  ret=0
  for u in g[v]:
    if checked[u]==0:
      checked[u]=1
      ret+=dfs(u)+1
  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
elif l%2==0:
  fg=path[l//2]
  sg=path[l//2-1]
else:
  fg=path[l//2+1]
  sg=path[l//2]
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: Wrong Answer

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

def dfs(v):
  if len(g[v])==1:
    return 0
  ret=0
  for u in g[v]:
    if checked[u]==0:
      checked[u]=1
      ret+=dfs(u)+1
  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
      q.append((u,p+[u]))
l=len(path)
if l==1:
  fg=n
  sg=1
elif l%2==0:
  fg=path[l//2]
  sg=path[l//2-1]
else:
  fg=path[l//2+1]
  sg=path[l//2]
checked=[0]*(n+1)
checked[1]=1
checked[fg]=1
ans1=dfs(1)
checked=[0]*(n+1)
checked[sg]=1
checked[n]=1
ans2=dfs(n)
if ans1>ans2:
  print('Fennec')
else:
  print('Snuke')

Submission status: Wrong Answer

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

def dfs(v):
  if len(g[v])==1:
    return 0
  ret=0
  for u in g[v]:
    if checked[u]==0:
      checked[u]=1
      ret+=dfs(u)+1
  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
      q.append((u,p+[u]))
l=len(path)
if l==1:
  fg=n
  sg=1
elif l%2==0:
  fg=path[l//2]
  sg=path[l//2-1]
else:
  fg=path[l//2+1]
  sg=path[l//2]
checked=[0]*(n+1)
checked[1]=1
checked[fg]=1
ans1=dfs(1)
checked=[0]*(n+1)
checked[sg]=1
checked[n]=1
ans2=dfs(n)
if ans1>=ans2-1:
  print('Fennec')
else:
  print('Snuke')

Submission status: Wrong Answer

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: Wrong Answer

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: Wrong Answer

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 set(E[cur]) - set([pre]) - set(f2s):
        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: Wrong Answer

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 set(E[cur]) - set([pre]) - set(f2s):
        dfs2(cur, e)

dfs1(-1, 0, [0])

score = []

for e in f2s:
    c = 0
    dfs2(-1, e)
    score.append(c)

t_snuke = len(f2s) // 2
s_score =  sum(score[t_snuke:])
f_score = sum(score[:t_snuke])

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

Submission status: Wrong Answer

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 set(E[cur]) - set([pre]) - set(f2s):
        dfs2(cur, e)

dfs1(-1, 0, [0])

score = []

for e in f2s:
    c = 0
    dfs2(-1, e)
    score.append(c)

s_score =  sum(score[-len(f2s) // 2:])
f_score = sum(score) - s_score

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

Submission status: Wrong Answer

Solution code:
def bfs(lst):
    tmp = []
    while lst:
        n = lst.pop(0)
        for r in graph[n]:
            if not visited[r]:
                visited[r] = True
                tmp.append(r)
    return tmp

N = int(input())
b, w = 1, N
b_c = w_c = 1

graph = [[] for i in range(N+1)]
visited = [True] + [False for i in range(N-1)] + [True]

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

b_n, w_n = [1], [N]

while b_n and w_n:
    b_n = bfs(b_n)
    b_c += len(b_n)

    w_n = bfs(w_n)
    w_c += len(w_n)

if b_c > w_c:
    print('Fennec')
else:
    print('Snuke')