90852024-02-13 19:48:47NagyLeoLegtávolabbi leszármazottpython3Elfogadva 50/50289ms41556 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 = [0] * (n + 1)  # fiuk

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

  parent = 0
  for f in range(1, n + 1):
    if con[f] == 0:
      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
base50/50
1Elfogadva0/018ms11580 KiB
2Elfogadva0/0243ms34964 KiB
3Elfogadva1/117ms11920 KiB
4Elfogadva3/317ms12284 KiB
5Elfogadva3/318ms12380 KiB
6Elfogadva1/117ms12548 KiB
7Elfogadva1/118ms12716 KiB
8Elfogadva1/120ms13044 KiB
9Elfogadva2/2266ms39188 KiB
10Elfogadva3/3257ms39260 KiB
11Elfogadva3/319ms13116 KiB
12Elfogadva4/4289ms39560 KiB
13Elfogadva4/4264ms39704 KiB
14Elfogadva3/341ms16016 KiB
15Elfogadva3/3263ms38672 KiB
16Elfogadva3/3241ms36904 KiB
17Elfogadva3/3261ms38256 KiB
18Elfogadva4/4189ms32448 KiB
19Elfogadva4/4234ms35496 KiB
20Elfogadva4/4284ms41556 KiB