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:

from collections import deque


def submit():
    n = int(input())
    edge = {i: [] for i in range(1, n + 1)}
    for _ in range(n - 1):
        a, b = map(int, input().split())
        edge[a].append(b)
        edge[b].append(a)

    # 1とNの最短路を求める
    def dfs(path):
        for e in edge[path[-1]]:
            if e == path[-2]:
                continue
            if e == n:
                path.append(n)
                return path
            path.append(e)
            ret = dfs(path)
            if ret:
                return ret
            path.pop()
        return None

    direct_path = dfs([0, 1])[1:]
    d = len(direct_path) 
    if d % 2:
        fennec = direct_path[:d // 2 + 1]
        snuke = direct_path[d // 2 + 1:]
    else:
        fennec = direct_path[:d // 2]
        snuke = direct_path[d // 2:]

    edge[fennec[-1]].remove(snuke[0])
    
    # fennecが取れる数を数える
    color = 0
    picked = [False for _ in range(n)]
    q = deque()
    q.append(1)
    picked[0] = True
    while len(q):
        p = q.pop()
        color += 1
        for e in edge[p]:
            if not picked[e - 1]:
                q.append(e)
                picked[e - 1] = True
    
    if color > n - color:
        print("Fennec")
    else:
        print("Snuke")


submit()

Submission status: Runtime Error

Solution code:
import collections
N = int(input())
G = [set() for i in range(N)]

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


F = [-1]*N
F[0] = 0
q = collections.deque()
q.append(0)
while(q):
    x = q.popleft()
    for y in G[x]:
        if F[y] < 0:
            F[y] = F[x] + 1
            q.append(y)

S = [-1]*N
S[N-1] = 0
q.append(N-1)
while(q):
    x = q.popleft()
    for y in G[x]:
        if S[y] < 0:
            S[y] = S[x] + 1
            q.append(y)
# print(F)
# print(S)
ans = 0
for i in range(N):
    if F[i] + S[i] == F[-1]:



if ans > N // 2:
    print("Fennec")
else:
    print("Snuke")

Submission status: Runtime Error

Solution code:
n = int(input())
ab = [tuple(map(int,input().split())) for i in range(n-1)]
es = [[] for i in range(n)]
for a,b in ab:
    a,b = a-1,b-1
    es[a].append(b)
    es[b].append(a)

def dfs(v, parent, dist):
    if dist[v] == -1:
        dist[v] = dist[parent]+1
    for next_v in es[v]:
        if dist[next_v] == -1:
            dfs(next_v,v,dist)

dist0 = [-1] * n
dist0[0] = 0
dfs(0,-1,dist0)

exit()


distn = [-1] * n
distn[n-1] = 0
dfs(n-1,-1,distn)

count_f = 0
for i in range(n):
    if dist0[i] <= distn[i]:
        count_f+=1

if count_f > n-count_f: print('Fennec')
else: print('Snuke')

Submission status: Runtime Error

Solution code:
n = int(input())
ab = [tuple(map(int,input().split())) for i in range(n-1)]
es = [[] for i in range(n)]
for a,b in ab:
    a,b = a-1,b-1
    es[a].append(b)
    es[b].append(a)

def dfs(v, parent, dist):
    if dist[v] == -1:
        dist[v] = dist[parent]+1
    for next_v in es[v]:
        if dist[next_v] == -1:
            dfs(next_v,v,dist)

dist0 = [-1] * n
dist0[0] = 0
dfs(0,-1,dist0)

return

distn = [-1] * n
distn[n-1] = 0
dfs(n-1,-1,distn)

count_f = 0
for i in range(n):
    if dist0[i] <= distn[i]:
        count_f+=1

if count_f > n-count_f: print('Fennec')
else: print('Snuke')

Submission status: Runtime Error

Solution code:
n = int(input())
ab = [tuple(map(int,input().split())) for i in range(n-1)]
es = [[] for i in range(n)]
for a,b in ab:
    a,b = a-1,b-1
    es[a].append(b)
    es[b].append(a)

def dfs(v, parent, dist):
    if dist[v] == -1:
        dist[v] = dist[parent]+1
    for next_v in es[v]:
        if dist[next_v] == -1:
            dfs(next_v,v,dist)

dist0 = [-1] * n
dist0[0] = 0
dfs(0,-1,dist0)

distn = [-1] * n
distn[n-1] = 0
dfs(n-1,-1,distn)

count_f = 0
for i in range(n):
    if dist0[i] <= distn[i]:
        count_f+=1

if count_f > n-count_f: print('Fennec')
else: print('Snuke')

Submission status: Runtime Error

Solution code:
n = int(input())
ab = [tuple(map(int,input().split())) for i in range(n-1)]
es = [[] for i in range(n)]
for a,b in ab:
    a,b = a-1,b-1
    es[a].append(b)
    es[b].append(a)
INF = 10**6
dist0 = [INF] * n
dist0[0] = 0

def dfs0(v, parent = -1):
    if dist0[v] == INF:
        dist0[v] = dist0[parent]+1
    for next_v in es[v]:
        if dist0[next_v] == INF:
            dfs0(next_v,v)
dfs0(0)

distn = [INF] * n
distn[n-1] = 0

def dfsn(v, parent = -1):
    if distn[v] == INF:
        distn[v] = distn[parent]+1
    for next_v in es[v]:
        if distn[next_v] == INF:
            dfsn(next_v,v)
dfsn(n-1)

count_f = 0
for i in range(n):
    if dist0[i] <= distn[i]:
        count_f+=1

if count_f > n-count_f: print('Fennec')
else: print('Snuke')

Submission status: Runtime Error

Solution code:
n = int(input())
ab = [tuple(map(int,input().split())) for i in range(n-1)]
es = [[] for i in range(n)]
for a,b in ab:
    a,b = a-1,b-1
    es[a].append(b)
    es[b].append(a)
INF = float('inf')
dist0 = [INF] * n
dist0[0] = 0
visited0 = [0]*n
def dfs0(v, parent = -1):
    if dist0[v] == INF:
        dist0[v] = dist0[parent]+1
    for next_v in es[v]:
        if dist0[next_v] == INF:
            dfs0(next_v,v)
dfs0(0)

distn = [INF] * n
distn[n-1] = 0
visitedn = [0]*n
def dfsn(v, parent = -1):
    if distn[v] == INF:
        distn[v] = distn[parent]+1
    for next_v in es[v]:
        if distn[next_v] == INF:
            dfsn(next_v,v)
dfsn(n-1)

count_f = 0
for i in range(n):
    if dist0[i] <= distn[i]:
        count_f+=1

if count_f > n-count_f: print('Fennec')
else: print('Snuke')

Submission status: Runtime Error

Solution code:
import sys
rl = sys.stdin.readline


def solve():
    N = int(rl())
    graph = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b = map(lambda x: int(x) - 1, rl().split())
        graph[a].append(b)
        graph[b].append(a)
    
    fen_dist, sun_dist = [0] * N, [0] * N
    
    def dfs1(node, parent, dist):
        fen_dist[node] = dist
        for child in graph[node]:
            if child != parent:
                dfs1(child, node, dist + 1)
    
    def dfs2(node, parent, dist):
        sun_dist[node] = dist
        for child in graph[node]:
            if child != parent:
                dfs2(child, node, dist + 1)
    
    dfs1(0, -1, 0)
    dfs2(N - 1, -1, 0)
    
    cnt_fen, cnt_sun = 0, 0
    for i in range(N):
        if fen_dist[i] <= sun_dist[i]:
            cnt_fen += 1
        else:
            cnt_sun += 1
    
    if cnt_fen < cnt_sun:
        print('Snuke')
    else:
        print('Fennec')


if __name__ == '__main__':
    solve()

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
INF = 10**20
MOD = 10**9 + 7
from itertools import permutations

def main():
    n = int(input())
    G = [[] for _ in range(n)]
    for _ in range(n - 1):
        a,b = map(int,input().split())
        a -= 1
        b -= 1
        G[a].append(b)
        G[b].append(a)
    
    depth = [-1] * n
    depth[0] = 0
    snuke = [0] * n
    snuke[-1] = 1
    children = [1] * n

    def dfs(i):
        if len(G[i]) == 0:
            return
        else:
            for e in G[i]:
                if depth[e] == -1:
                    depth[e] = depth[i] + 1
                    dfs(e)
                    snuke[i] |= snuke[e]
                    children[i] += children[e] 

    dfs(0)
    white = depth[-1]//2 + 1
    for i in range(n):
        if depth[i] == white and snuke[i]:
            sp = children[i]
            fp = n - sp
            break
    
    if  fp > sp:
        print('Fennec')
    else:
        print('Snuke')  
if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10 ** 5)

n = int(input())
edge = [tuple(map(int, input().split())) for _ in range(n-1)]
connect = [set() for _ in range(n)]
for a, b in edge:
    connect[a-1].add(b-1)
    connect[b-1].add(a-1)

d1 = [0] * n
dn = [0] * n

def dfs(v, ng, dis, d):
    d[v] = dis
    ng.add(v)
    for n in connect[v]:
        if n not in ng:
            dfs(n, ng, dis+1, d)

dfs(0, set(), 0, d1)
dfs(n-1, set(), 0, dn)

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

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10 ** 5)

n = int(input())
edge = [tuple(map(int, input().split())) for _ in range(n-1)]
connect = [set() for _ in range(n)]
for a, b in edge:
    connect[a-1].add(b-1)
    connect[b-1].add(a-1)

d1 = [0] * n
dn = [0] * n

def dfs(v, ng, dis, d):
    d[v] = dis
    ng.add(v)
    for n in connect[v]:
        if n not in ng:
            dfs(n, ng, dis+1, d)

dfs(0, set(), 0, d1)
dfs(n-1, set(), 0, dn)

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

Submission status: Runtime Error

Solution code:
n = int(input())
edge = [tuple(map(int, input().split())) for _ in range(n-1)]
connect = [set() for _ in range(n)]
for a, b in edge:
    connect[a-1].add(b-1)
    connect[b-1].add(a-1)

d1 = [0] * n
dn = [0] * n

def dfs(v, ng, dis, d):
    d[v] = dis
    ng.add(v)
    for n in connect[v]:
        if n not in ng:
            dfs(n, ng, dis+1, d)

dfs(0, set(), 0, d1)
dfs(n-1, set(), 0, dn)

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

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
from collections import deque
import sys
try:
    from typing import Deque, List
except ImportError:
    pass


def dfs(G: "List[List[int]]", a: int, b: int, par: int = -1):
    for t in G[a]:
        if t == par:
            continue
        if t == b:
            return [t]
        path = dfs(G, t, b, a)
        if path:
            return [t] + path
    return None


def bfs(G: "List[List[int]]", a: int, par: int = -1):
    q = deque()  # type: Deque[int]
    q.append(a)
    visited = {par, a}
    while q:
        s = q.popleft()
        for t in G[s]:
            if t in visited:
                continue
            q.append(t)
            visited.add(t)
    return len(visited) - 1


def solve(N: int, a: "List[int]", b: "List[int]"):
    G = [[] for i in range(N)]
    for ai, bi in zip(a, b):
        ai -= 1
        bi -= 1
        G[ai].append(bi)
        G[bi].append(ai)
    path = dfs(G, 0, N - 1)
    assert path
    path = [0] + path
    bb = path[(len(path) - 1) // 2]
    wb = path[(len(path) + 1) // 2]
    bn = bfs(G, bb, wb)
    wn = bfs(G, wb, bb)
    if bn > wn:
        print("Fennec")
    else:
        print("Snuke")


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

Solution code:
# coding: utf-8
# Your code here!
def killme(num,lis):
    visited[num]=1
    if num==N-1:
        lis.append(num)
        for index,item in enumerate(lis):
            highway[item]=index
    else:
        for next_n in way[num]:
            if visited[next_n]==0:
                killme(next_n,lis+[num])
    return 

def dfs(num,root):
    visited[num]=1
    cluster[root].append(num)
    for next_n in way[num]:
        if visited[next_n]==0 and highway[next_n]==-1:
            #print(next_n,root)
            dfs(next_n,root)
    return 

N=int(input())

way=[[] for i in range(N)]
visited=[0]*N
highway=[-1]*N

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

killme(0,[])

#print(way)
#print(visited)
#print(highway)

visited=[0]*N
cluster=[[] for i in range(highway[-1]+1)]

for index,item in enumerate(highway):
    if item!=-1:
        dfs(index,item)

F=0
S=0
for i in range(highway[-1]+1):
    if i<=highway[-1]//2:
        F+=len(cluster[i])
    else:
        S+=len(cluster[i])

if F>S:
    print("Fennec")
else:
    print("Snuke")
#print(F,S)

Submission status: Runtime Error

Solution code:
n=int(input())
edges=[[] for _ in range(n)]
dist=[0]*n
for i in range(n-1):
  u,v=map(int,input().split())
  u-=1
  v-=1
  edges[u].append(v)
  edges[v].append(u)
dist=[ [0]*n for _ in range(2)]
#do a dfs
def dfs(u,p,idd):#u=curr , p=par
  for nei in edges[u]:
    if nei==p:
      continue
    dist[idd][nei]=1+dist[idd][u]
    dfs(nei,u,idd)
dist[0][0]=0
dfs(0,0,0)
dist[1][n-1]=0
dfs(n-1,n-1,1)
cnt_f=cnt_s=0
for i in range(n):
  if dist[0][i]<=dist[1][i]:
    cnt_f+=1
  else:
    cnt_s+=1
print("Fennec") if cnt_f>cnt_s else print("Snuke")

Submission status: Runtime Error

Solution code:
#!/usr/bin/env pypy3
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
import queue

def main():
    mod=10**9+7
    N=I()
    adj=[[] for _ in range(N)]
    for i in range(N-1):
        a,b=MI()
        a-=1
        b-=1
        adj[a].append(b)
        adj[b].append(a)
    bit=[-1]*N#未確認,F,S    
    bit[0]=0
    bit[-1]=1
    pL=[0]*N
    dS=0
    
    q=queue.Queue()
    q.put([0,0,0])#v.pare,depth
    
    #深さを調べる
    while not q.empty():
        v,p,d=q.get()
        pL[v]=p
        if v==N-1:
            dS=d
            break
        for i in range(len(adj[v])):
            nv=adj[v][i]
            if nv!=p:
                q.put((nv,v,d+1))
    
    #主要なところを塗る            
    v=N-1            
    for _ in range((dS-1)//2):
        v=pL[v]
        bit[v]=1
        
        
    q.put([0,0])#v.pare
    while not q.empty():
        v,p=q.get()
        if bit[v]==-1:
            bit[v]=bit[p]

        for i in range(len(adj[v])):
            nv=adj[v][i]
            if nv!=p:
                q.put((nv,v))
        
        
                    
    s=sum(bit)#sunukeが塗れる
    if s>=N-s:
        print("Snuke")
    else:
        print("Fennec")
     
        
    
    

main()

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline


class UnionFind:
    def __init__(self, size):
        self.parent = list(range(size))
        self.rank = [0] * size

    def find(self, x):
        if self.parent[x] != x:
            self.parent[x] = self.find(self.parent[x])
        return self.parent[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
        else:
            self.parent[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

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


N = int(input())
G = [set() for _ in range(N)]
for _ in range(N-1):
    a, b = map(lambda x: int(x)-1, input().split())
    G[a].add(b)
    G[b].add(a)
visited = [False] * N
visited[0] = True


def dfs(v):
    if v == N-1:
        return [N-1]
    for u in G[v]:
        if visited[u]:
            continue
        visited[u] = True
        ret = dfs(u)
        visited[u] = False
        if ret:
            return [v] + ret
    return None


path = dfs(0)
l = len(path)
u, v = 0, 0
if l % 2 == 0:
    u = path[l//2-1]
    v = path[l//2]
else:
    u = path[l//2]
    v = path[l//2+1]
G[u].remove(v)
G[v].remove(u)

uf = UnionFind(N)
for u in range(N):
    for v in G[u]:
        uf.unite(u, v)
fen = 0
snu = 0
for v in range(N):
    if uf.same(v, 0):
        fen += 1
    else:
        snu += 1
if fen > snu:
    print('Fennec')
else:
    print('Snuke')

Submission status: Runtime Error

Solution code:
# https://atcoder.jp/contests/abc067/tasks/arc078_b

from collections import deque

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

def dist_from_X(X):
    dist = [-1] * N
    
    que = deque([X])
    dist[X] = 0
    while que:
        v = que.popleft()
        d = dist[v]
        for e in E[v]:
            if dist[e] > -1:
                continue
            dist[e] = d + 1
            que.append(e)
    
    return dist

Fennec = dist_from_X(0)
Sunuke = dist_from_X(N-1)

cnt = [0,0]
for i,j in zip(Fennec, Sunuke):
    if i <= j:
        cnt[0] += 1
    else:
        cnt[1] += 1

assert cnt[0] != cnt[1]
if cnt[0] > cnt[1]:
    print("Fennec")
else:
    print("Sunuke")

Submission status: Runtime Error

Solution code:
def dfs(v, d):
    for u in g[v]:
        if d[u] != init:
            continue
        d[u] = d[v] + 1
        dfs(u, d)


def main():
    import sys
    input = sys.stdin.readline

    global g, init

    n = int(input())

    g = tuple(set() for _ in range(n))
    for _ in range(n - 1):
        a, b = (int(x) - 1 for x in input().split())
        g[a].add(b)
        g[b].add(a)

    init = n

    dist_1 = [init] * n
    dist_1[0] = 0
    dfs(0, dist_1)

    dist_n = [init] * n
    dist_n[n - 1] = 0
    dfs(n - 1, dist_n)

    fennec = sum(dist_1[v] <= dist_n[v] for v in range(n))
    snuke = n - fennec

    if fennec > snuke:
        print('Fennec')
    else:
        print('Snuke')


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

def main():
  n = int(input())
  F = [[] for _ in range(n)]
  for _ in range(n-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    F[a].append(b)
    F[b].append(a)
  seenB = [False]*n
  seenW = [False]*n
  seenB[n-1] = True
  seenW[0] = True
  def dfs(v, seen):
    seen[v] = True
    for nv in F[v]:
      if seen[nv]:
        continue
      dfs(nv, seen)
  dfs(0, seenB)
  dfs(n-1, seenW)
  print(seenB, seenW)
  aplusc = seenB.count(True) - 2
  bplusc = seenW.count(True) - 2
  print(aplusc, bplusc)
  c = aplusc + bplusc + 2 - n
  a = aplusc - c
  b = bplusc - c
  print(a, b, c)
  if c%2 == 0:
    if a > b:
      print("Fennec")
    else:
      print("Snuke")
  else:
    if a >= b:
      print("Fennec")
    else:
      print("Snuke")
      
if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

def main():
  n = int(input())
  F = [[] for _ in range(n)]
  for _ in range(n-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    F[a].append(b)
    F[b].append(a)
  seenB = [False]*n
  seenW = [False]*n
  seenB[n-1] = True
  seenW[0] = True
  def dfs(v, seen):
    seen[v] = True
    rep = 1
    for nv in F[v]:
      if seen[nv]:
        continue
      rep += dfs(nv, seen)
    return rep
  aplusc = dfs(0, seenB) - 1
  bplusc = dfs(n-1, seenW) - 1
  print(aplusc, bplusc)
  c = aplusc + bplusc + 2 - n
  a = aplusc - c
  b = bplusc - c
  if c%2 == 0:
    if a > b:
      print("Fennec")
    else:
      print("Snuke")
  else:
    if a >= b:
      print("Fennec")
    else:
      print("Snuke")
      
if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

def main():
  n = int(input())
  F = [[] for _ in range(n)]
  for _ in range(n-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    F[a].append(b)
    F[b].append(a)
  seenB = [False]*n
  seenW = [False]*n
  seenB[n-1] = True
  seenW[0] = True
  def dfs(v, seen):
    seen[v] = True
    rep = 1
    for nv in F[v]:
      if seen[nv]:
        continue
      rep += dfs(nv, seen)
    return rep
  aplusc = dfs(0, seenB)
  bplusc = dfs(n-1, seenW)
  c = aplusc + bplusc + 2 - n
  a = aplusc - c
  b = bplusc - c
  if c%2 == 0:
    if a > b:
      print("Fennec")
    else:
      print("Snuke")
  else:
    if a >= b:
      print("Fennec")
    else:
      print("Snuke")
      
if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:
import sys
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import shortest_path
read = sys.stdin.read

N, *ab = map(int, read().split())

graph = csr_matrix(([1] * (N - 1), (ab[::2], ab[1::2])), shape=(N + 1, N + 1))
distance = shortest_path(graph, directed=False, indices=[1, N])
d1 = distance[0]
dn = distance[1]

Fennec = -1
for i, j in zip(d1, dn):
    if i <= j:
        Fennec += 1

if Fennec > N - Fennec:
    print('Fennec')
else:
    print('Sunuke')

Submission status: Runtime Error

Solution code:
def dfs_f(now, kyori):
    kyori_f[now] = kyori
    # 今いる頂点から行ける頂点を順に next に入れてループ
    for next in g[now]:
        # 行ったことがない、白く塗られていない
        if not memo_f[next] and next != n - 1:
            memo_f[next] = True
            dfs_f(next, kyori + 1)

def dfs_s(now, kyori):
    kyori_s[now] = kyori
    # 今いる頂点から行ける頂点を順に next に入れてループ
    for next in g[now]:
        # 行ったことがない、黒く塗られていない
        if not memo_s[next] and next != 0:
            memo_s[next] = True
            dfs_s(next, kyori + 1)


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

# フェネック
memo_f = [False for i in range(n)]  # 訪れたことがあるか
kyori_f = [float("inf") for i in range(n)]  # 始点からの距離
memo_f[0] = True
dfs_f(0, 0)

# すぬけ
memo_s = [False for i in range(n)]  # 訪れたことがあるか
kyori_s = [float("inf") for i in range(n)]  # 始点からの距離
memo_s[n - 1] = True
dfs_s(n - 1, 0)

black = 0
white = 0
for i in range(n):
    if kyori_f[i] < kyori_s[i]:
        black += 1
    else:
        white += 1

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

Submission status: Runtime Error

Solution code:
def dfs_f(now):
    # 今いる頂点から行ける頂点を順に next に入れてループ
    for next in g[now]:
        # 行ったことがない、白く塗られていない
        if not memo_f[next] and next != n - 1:
            memo_f[next] = True
            dfs_f(next)

def dfs_s(now):
    # 今いる頂点から行ける頂点を順に next に入れてループ
    for next in g[now]:
        # 行ったことがない、黒く塗られていない
        if not memo_s[next] and next != 0:
            memo_s[next] = True
            dfs_s(next)


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

# フェネック
# 訪れたことがあるか
memo_f = [False for i in range(n)]
memo_f[0] = True
dfs_f(0)

# すぬけ
# 訪れたことがあるか
memo_s = [False for i in range(n)]
memo_s[n - 1] = True
dfs_s(n - 1)

black = 0
white = 0
for i in range(n):
    if memo_f[i] and not memo_s[i]:  # フェネックだけ行ける
        black += 1
    elif not memo_f[i] and memo_s[i]:  # すぬけだけ行ける
        white += 1

if black > white:
    print("Fennec")
elif black < white:
    print("Snuke")
else:
    if (n - black - white) % 2:
        print("Fennec")
    else:
        print("Snuke")

Submission status: Runtime Error

Solution code:
def dfs_f(now):
    # 今いる頂点から行ける頂点を順に next に入れてループ
    for next in g[now]:
        # 行ったことがない、白く塗られていない
        if not memo_f[next] and next != n - 1:
            memo_f[next] = True
            dfs_f(next)

def dfs_s(now):
    # 今いる頂点から行ける頂点を順に next に入れてループ
    for next in g[now]:
        # 行ったことがない、黒く塗られていない
        if not memo_s[next] and next != 0:
            memo_s[next] = True
            dfs_s(next)


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

# フェネック
# 訪れたことがあるか
memo_f = [False for i in range(n)]
memo_f[0] = True
dfs_f(0)

# すぬけ
# 訪れたことがあるか
memo_s = [False for i in range(n)]
memo_s[n - 1] = True
dfs_s(n - 1)

if sum(memo_f) > sum(memo_s):
    print("Fennec")
else:
    print("Snuke")

Submission status: Runtime Error

Solution code:
def dfs_f(now, prev):
    # 今いる頂点から行ける頂点を順に next に入れてループ
    for next in g[now]:
        # 行ったことがない、白く塗られていない
        if not memo_f[next] and next != n - 1:
            memo_f[next] = True
            dfs_f(next, now)

def dfs_s(now, prev):
    # 今いる頂点から行ける頂点を順に next に入れてループ
    for next in g[now]:
        # 行ったことがない、白く塗られていない
        if not memo_s[next] and next != 0:
            memo_s[next] = True
            dfs_s(next, now)


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

# フェネック
# 訪れたことがあるか
memo_f = [False for i in range(n)]
memo_f[0] = True
dfs_f(0, -1)

# すぬけ
# 訪れたことがあるか
memo_s = [False for i in range(n)]
memo_s[n - 1] = True
dfs_s(n - 1, -1)

if sum(memo_f) > sum(memo_s):
    print("Fennec")
else:
    print("Snuke")

Submission status: Runtime Error

Solution code:
import sys
n = int(input())
graph = {i : [] for i in range(n)}
for i in range(n-1):
    a, b = [int(i) for i in sys.stdin.readline().split()]
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

stack = [0]
already = set()
parent = [i for i in range(n)]
flg = False
while len(stack) > 0:
    cur = stack.pop(-1)
    already.add(cur)
    for _next in graph[cur]:
        if _next not in already:
            stack.append(_next)
            parent[_next] = cur
            if _next == n - 1:
                flg = True
                break
    if flg:
        break

path = []
cur = n - 1
while cur != 0:
    path.append(cur)
    cur = parent[cur]
path.append(0)
path.reverse()
flg_2 = len(path) % 2 != 0
left = path[len(path) // 2 - 1 + flg_2]
right = path[len(path) // 2 + flg_2]

def dfs(x, last):
    res = 1
    for _next in graph[x]:
        if _next != last:
           res += dfs(_next, x)
    return res

left_res = dfs(left, right)
#print(left_res)
print("Fennec" if left_res > (n - left_res) else "Snuke")

Submission status: Runtime Error

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

Solution code:
#木
#お互いの始点を目指すのが最善
#import heapq
#heapじゃない
from collections import deque
n = int(input())
Ad = [[] for _ in range(n)]

for i in range(n-1):
    ai,bi = map(int, input().split( ))
    ai-=1
    bi-=1
    Ad[ai].append(bi)
    Ad[bi].append(ai)
#0(元々は1)を根とする

P = [-1 for i in range(n)]
D = [-1 for i in range(n)]
Q = deque()
P[0] = 0
D[0] = 0
Q.append(0)
while Q:
    v = Q.popleft()
    d = D[v]
    for u in Ad[v]:
        if P[u]<0:
            P[u] = v
            Q.append(u)
            D[u] = d+1
#test
up = 0
#now = n-1
nxt = n-1
while up<D[n-1]//2:
    now = nxt
    nxt = P[nxt]
    up += 1
r = now
d = D[r]
cnt = 0
Q = deque()
Q.append(r)
V = [False]*n
V[now] = True
while Q:
    v = Q.popleft()
    cnt += 1
    for u in Ad[v]:
        if not V[u] and D[u]>d:
            Q.append(u)
            V[u] = True

if cnt>=(n+1)//2:
    print("Snuke")
else:
    print("Fennec")

Submission status: Runtime Error

Solution code:
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=[]
    Q.append_left((0,point))
    
    while Q:
        c,p=Q.pop()
        for np in d[p]:
            if cost[np]==1e7:
                cost[np]=c+1
                Q.append_left((c+1,np))
    return cost

n=int(input())
d=[[]for i in range(n+1)]
for i in range(n-1):
    a,b=map(int,input().split())
    d[a].append(b)
    d[b].append(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: Runtime Error

Solution code:
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=[]
    Q.appendleft((0,point))
    
    while Q:
        c,p=Q.pop()
        for np in d[p]:
            if cost[np]==1e7:
                cost[np]=c+1
                heappush(Q,(c+1,np))
    return cost

n=int(input())
d=[deque()for i in range(n+1)]
for i in range(n-1):
    a,b=map(int,input().split())
    d[a].append(b)
    d[b].append(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")