90852024-02-13 19:48:47NagyLeoLegtávolabbi leszármazottpython3Accepted 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()
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/018ms11580 KiB
2Accepted0/0243ms34964 KiB
3Accepted1/117ms11920 KiB
4Accepted3/317ms12284 KiB
5Accepted3/318ms12380 KiB
6Accepted1/117ms12548 KiB
7Accepted1/118ms12716 KiB
8Accepted1/120ms13044 KiB
9Accepted2/2266ms39188 KiB
10Accepted3/3257ms39260 KiB
11Accepted3/319ms13116 KiB
12Accepted4/4289ms39560 KiB
13Accepted4/4264ms39704 KiB
14Accepted3/341ms16016 KiB
15Accepted3/3263ms38672 KiB
16Accepted3/3241ms36904 KiB
17Accepted3/3261ms38256 KiB
18Accepted4/4189ms32448 KiB
19Accepted4/4234ms35496 KiB
20Accepted4/4284ms41556 KiB