90842024-02-13 19:44:57NagyLeoLegtávolabbi leszármazottpython3Time limit exceeded 46/50317ms50296 KiB
import sys

sys.setrecursionlimit(10**6)

def main2():
  n = int(input())
  connections=[[] for i in range(n+1)] # graph
  con = set() # fiuk

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

  parent = 0
  for f in range(1,n+1):
    if f not in con:
      parent = f
      break

  def deepsearch(node, depth, result, name):
    if result < depth:
        result = depth
        name=node
    for i in connections[node]:
        result,name=deepsearch(i, depth+1, result, name)
    return (result,name)

  print(deepsearch(parent, 0, 0, 0)[1])

def main3():
  n = int(input())
  connections = [[] for i in range(n + 1)]  # graph
  con = set()  # fiuk

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

  parent = 0
  for f in range(1, n + 1):
    if f not in con:
      parent = f
      break

  start = [parent]
  while start:
    tmp = []
    for v in start:
      for w in connections[v]:
        tmp.append(w)
    if len(tmp) == 0:
      print(start[0])
      break
    start = tmp


main3()
SubtaskSumTestVerdictTimeMemory
base46/50
1Accepted0/018ms11608 KiB
2Accepted0/0259ms45424 KiB
3Accepted1/118ms13044 KiB
4Accepted3/317ms13228 KiB
5Accepted3/318ms13660 KiB
6Accepted1/117ms13676 KiB
7Accepted1/118ms13724 KiB
8Accepted1/119ms13760 KiB
9Accepted2/2275ms47948 KiB
10Accepted3/3263ms47992 KiB
11Accepted3/319ms14220 KiB
12Accepted4/4277ms49124 KiB
13Accepted4/4291ms49336 KiB
14Accepted3/343ms17672 KiB
15Accepted3/3272ms49240 KiB
16Accepted3/3254ms47932 KiB
17Accepted3/3275ms48536 KiB
18Accepted4/4199ms36424 KiB
19Accepted4/4248ms47044 KiB
20Time limit exceeded0/4317ms50296 KiB