Problem p02834 (python)

Fixed submission:
import sys
input = sys.stdin.readline

# 再帰上限を引き上げる
sys.setrecursionlimit(10**6)

# G:graph, cr:current node, seen:is seen?, dist:out;each distance
def dfs(G,cr,seen,dist):
	seen[cr] = 1
	for i in G[cr]: #G[cr]から行ける各頂点へ
		if seen[i] == 0: #まだ通っていなかったら
			dist[i] = dist[cr]+1
			dfs(G,i,seen,dist)


n,u,v = (int(x) for x in input().split())

u,v = u-1,v-1

a = [[] for i in range(n)]

for i in range(n-1):
	# 配列indexは0開始だからinputは-1
	l,m = (int(x)-1 for x in input().split())
	a[l].append(m)
	a[m].append(l)

# v視点のdistを調べる
dist_u = [0]*n
dist_v = [0]*n

dfs(a,u,[0]*n,dist_u)
# u視点のdistを調べる
dfs(a,v,[0]*n,dist_v)

for i in range(n):
	if dist_u[i] >= dist_v[i]: # vが追いつける距離なら
		dist_v[i] = -1

print(max(dist_v) - 1)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

# 再帰上限を引き上げる
sys.setrecursionlimit(10**6)




n,u,v = (int(x) for x in input().split())

u,v = u-1,v-1

a = [[] for i in range(n)]

for i in range(n-1):
	# 配列indexは0開始だからinputは-1
	l,m = (int(x)-1 for x in input().split())
	a[l].append(m)
	a[m].append(l)

# v視点のdistを調べる
dist_u = [0]*n
dist_v = [0]*n

# G:graph, cr:current node, seen:is seen?, dist:out;each distance
def dfs_u(cr,seen):
	seen[cr] = 1
	for i in G[cr]: #G[cr]から行ける各頂点へ
		if seen[i] == 0: #まだ通っていなかったら
			dist_u[i] = dist_u[cr]+1
			dfs_u(i,seen)
# G:graph, cr:current node, seen:is seen?, dist:out;each distance
def dfs_v(cr,seen):
	seen[cr] = 1
	for i in G[cr]: #G[cr]から行ける各頂点へ
		if seen[i] == 0: #まだ通っていなかったら
			dist_v[i] = dist_v[cr]+1
			dfs_v(i,seen)

dfs_u(u,[0]*n)
# u視点のdistを調べる
dfs_v(v,[0]*n)

for i in range(n):
	if dist_u[i] >= dist_v[i]: # vが追いつける距離なら
		dist_u[i] = -1

print(dist_v[dist_u.index(max(dist_u))] - 1)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

# G:graph, cr:current node, seen:is seen?, dist:out;each distance
def dfs(G,cr,seen,dist):
	seen[cr] = 1
	for i in G[cr]: #G[cr]から行ける各頂点へ
		if seen[i] == 0: #まだ通っていなかったら
			dist[i] = dist[cr]+1
			dfs(G,i,seen,dist)


n,u,v = ( int(x) for x in input().split() )

u,v = u-1,v-1

a = [[] for i in range(n)]

for i in range(n-1):
	# 配列indexは0開始だからinputは-1
	l,m = ( int(x)-1 for x in input().split() )
	a[l].append(m)
	a[m].append(l)

# v視点のdistを調べる
dist_u = [0]*n
dfs(a,u,[0]*n,dist_u)
# u視点のdistを調べる
dist_v = [0]*n
dfs(a,v,[0]*n,dist_v)

for i in range(n):
	if dist_u[i] >= dist_v[i]: # vが追いつける距離なら
		dist_u[i] = -1

print(dist_v[dist_u.index(max(dist_u))] - 1)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

n,u,v = ( int(x) for x in input().split() )

u,v = u-1,v-1

# -1なら行先なし
a = [[] for i in range(n)]
b = [[] for i in range(n)]

for i in range(n-1):
	# 配列indexは0開始だからinputは-1
	l,m = ( int(x)-1 for x in input().split() )
	a[l].append(m)
	a[m].append(l)

# G:graph, cr:current node, seen:pathed or not, dist:out;each distance
def distance(G,cr,seen,dist):
	seen[cr] = 1
	for i in G[cr]: #G[cr]から行ける各頂点へ
		if seen[i] == 0:# or dist[i] > dist[cr]+1: #まだ通っていなかったら
			dist[i] = dist[cr]+1
			distance(G,i,seen,dist)

# 欲しいのはdist of u-v, back way of u
# u視点のdistを調べる
dist_v = [0]*(n)
distance(a,v,[0]*(n),dist_v)
# v視点のdistを調べる
dist_u = [0]*(n)
distance(a,u,[0]*(n),dist_u)

for i in range(n):
	if dist_u[i] >= dist_v[i]:
		dist_u[i] = -1

tmp = max(dist_u)
max_idx = dist_u.index(tmp)

print(dist_v[max_idx] - 1)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

n,u,v = ( int(x) for x in input().split() )

u,v = u-1,v-1

# -1なら行先なし
a = [[] for i in range(n)]
b = [[] for i in range(n)]

for i in range(n-1):
	# 配列indexは0開始だからinputは-1
	l,m = ( int(x)-1 for x in input().split() )
	a[l].append(m)
	a[m].append(l)

def distance(cr,p,dist): # cr:current node, p:pathed or not, dist:out;each distance
	for i in a[cr]: #a[cr]から行ける各頂点へ
		p[cr] = 1
		if p[i] == 0 or dist[i] > dist[cr]+1: #まだ通っていなかったら
			p[i] = 1
			dist[i] = dist[cr]+1
			distance(i,p,dist)

# 欲しいのはdist of u-v, back way of u
# u視点のdistを調べる
p = [0]*(n)
dist_v = [0]*(n)
distance(v,p,dist_v)
# v視点のdistを調べる
p = [0]*(n)
dist_u = [0]*(n)
distance(u,p,dist_u)

for i in range(n):
	if dist_u[i] >= dist_v[i]:
		dist_u[i] = -1

tmp = max(dist_u)
max_idx = dist_u.index(tmp)

print(dist_v[max_idx] - 1)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

n,u,v = ( int(x) for x in input().split() )

u,v = u-1,v-1

# -1なら行先なし
a = [[] for i in range(n)]
b = [[] for i in range(n)]

for i in range(n-1):
	# 配列indexは0開始だからinputは-1
	l,m = ( int(x)-1 for x in input().split() )
	a[l].append(m)
	a[m].append(l)


def distance(cr,p,dist): # cr:current node, p:pathed or not, dist:out;each distance
	for i in a[cr]: #a[cr]から行ける各頂点へ
		p[cr] = 1
		if p[i] == 0: #まだ通っていなかったら
			p[i] = 1
			dist[i] = dist[cr]+1
			distance(i,p,dist)

# 欲しいのはdist of u-v, back way of u
# u視点のdistを調べる
p = [0]*(n)
dist_v = [0]*(n)
distance(v,p,dist_v)
# v視点のdistを調べる
p = [0]*(n)
dist_u = [0]*(n)
distance(u,p,dist_u)

for i in range(n):
	if dist_u[i] >= dist_v[i]:
		dist_u[i] = -1

tmp = max(dist_u)
max_idx = dist_u.index(tmp)

print(dist_v[max_idx] - 1)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
import numpy as np

n,u,v = ( int(x) for x in input().split() )

u,v = u-1,v-1

a = [[] for i in range(n)]
b = [[] for i in range(n)]

for i in range(n-1):
	# 配列indexは0開始だからinputは-1
	l,m = ( int(x)-1 for x in input().split() )
	a[l].append(m)
	a[m].append(l)

def distance(cr,p,dist): # cr:current node, p:pathed or not, dist:out;each distance
	for i in a[cr]: #a[cr]から行ける各頂点へ
		p[cr] = 1
		if p[i] == 0: #まだ通っていなかったら
			p[i] = 1
			dist[i] = dist[cr]+1
			distance(i,p,dist)

p = [0]*(n)
dist_v = [0]*(n)
distance(v,p,dist_v)
p = [0]*(n)
dist_u = [0]*(n)
distance(u,p,dist_u)

for i in range(n):
	if dist_u[i] >= dist_v[i]:
		dist_u[i] = -1

tmp = max(dist_u)
max_idx = dist_u.index(tmp)

print(dist_v[max_idx] - 1)