Problem p02834 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**5)
N,u,v=map(int,input().split())

du,dv=[0]*-~N,[0]*-~N
T=[list() for i in range(-~N)]

def dfs(p,t,v,d):
  d[v]=t
  for i in T[v]:
    if i!=p:
      dfs(v,t+1,i,d)

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

dfs(0,0,u,du)
dfs(0,0,v,dv)

c=0
for i in range(1,-~N):
  if len(T[i])==1 and i!=v:
    if du[i]<dv[i]:
      c=max(c,~-dv[i])

print(c)

Buggy submission (verdict: Runtime Error):
import sys
sys.recursionlimit(10**5)

N,u,v=map(int,input().split())

du,dv=[0]*-~N,[0]*-~N
T=[list() for i in range(-~N)]

def dfs(p,t,v,d):
  d[v]=t
  for i in T[v]:
    if i!=p:
      dfs(v,t+1,i,d)

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

dfs(0,0,u,du)
dfs(0,0,v,dv)

c=0
for i in range(1,-~N):
  if len(T[i])==1 and i!=v:
    if du[i]<dv[i]:
      c=max(c,~-dv[i])

print(c)

Buggy submission (verdict: Runtime Error):
N,u,v=map(int,input().split())

du,dv=[0]*-~N,[0]*-~N
T=[list() for i in range(-~N)]

def dfs(p,t,v,d):
  d[v]=t
  for i in T[v]:
    if i!=p:
      dfs(v,t+1,i,d)

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

dfs(0,0,u,du)
dfs(0,0,v,dv)

c=0
for i in range(1,-~N):
  if len(T[i])==1 and i!=v:
    if du[i]<dv[i]:
      c=max(c,~-dv[i])

print(c)

Buggy submission (verdict: Runtime Error):
N,u,v=map(int,input().split())

du,dv=[0]*-~N,[0]*-~N
T=[list() for i in range(-~N)]

def dsf(p,t,v,d):
  d[v]=t
  for i in T[v]:
    if i!=p:
      dsf(v,t+1,i,d)

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

dsf(0,0,u,du)
dsf(0,0,v,dv)

if du[v]==1 and len(T[v])==1:
  print(0)
else:
  m=0
  for i in range(1,~-N):
    if du[i] < dv[i]:
      if m<du[i]:
        v,m=i,du[i]

  if m!=0:
    print(du[v]+m-1)
  else:
    m=N+1
    for i in range(1,~-N):
      if du[i]==dv[i]:
        m=min(m,du[i])
    print(m)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10*5)

N,u,v=map(int,input().split())

du,dv=[0]*-~N,[0]*-~N
T=[list() for i in range(-~N)]

def dsf(p,t,v,d):
  d[v]=t
  for i in T[v]:
    if i!=p:
      dsf(v,t+1,i,d)

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

dsf(0,0,u,du)
dsf(0,0,v,dv)

if du[v]==1 and len(T[v])==1:
  print(0)
else:
  m=0
  for i in range(1,~-N):
    if du[i] < dv[i]:
      if m<du[i]:
        v,m=i,du[i]

  if m!=0:
    print(du[v]+(m if m%2 else m-1))
  else:
    m=N+1
    for i in range(1,~-N):
      if du[i]==dv[i]:
        m=min(m,du[i])
    print(m)

Buggy submission (verdict: Runtime Error):
N,u,v=map(int,input().split())

du,dv=[0]*-~N,[0]*-~N
T=[list() for i in range(-~N)]

def dsf(p,t,v,d):
  d[v]=t
  for i in T[v]:
    if i!=p:
      dsf(v,t+1,i,d)

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

dsf(0,0,u,du)
dsf(0,0,v,dv)

if du[v]==1 and len(T[v])==1:
  print(0)
else:
  m=0
  for i in range(1,~-N):
    if du[i] < dv[i]:
      if m<du[i]:
        v,m=i,du[i]

  if m!=0:
    print(du[v]+(m if m%2 else m-1))
  else:
    m=N+1
    for i in range(1,~-N):
      if du[i]==dv[i]:
        m=min(m,du[i])
    print(m)