90842024-02-13 19:44:57NagyLeoLegtávolabbi leszármazottpython3Időlimit túllépés 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()
RészfeladatÖsszpontTesztVerdiktIdőMemória
base46/50
1Elfogadva0/018ms11608 KiB
2Elfogadva0/0259ms45424 KiB
3Elfogadva1/118ms13044 KiB
4Elfogadva3/317ms13228 KiB
5Elfogadva3/318ms13660 KiB
6Elfogadva1/117ms13676 KiB
7Elfogadva1/118ms13724 KiB
8Elfogadva1/119ms13760 KiB
9Elfogadva2/2275ms47948 KiB
10Elfogadva3/3263ms47992 KiB
11Elfogadva3/319ms14220 KiB
12Elfogadva4/4277ms49124 KiB
13Elfogadva4/4291ms49336 KiB
14Elfogadva3/343ms17672 KiB
15Elfogadva3/3272ms49240 KiB
16Elfogadva3/3254ms47932 KiB
17Elfogadva3/3275ms48536 KiB
18Elfogadva4/4199ms36424 KiB
19Elfogadva4/4248ms47044 KiB
20Időlimit túllépés0/4317ms50296 KiB