Contest #2 editorial

Revision en2, by hundera, 2024-05-01 17:39:42

A.Party

import heapq, math
from itertools import *
from bisect import *
from collections import *
from sys import stdin, stdout


###########-> Templates <- ##########
def pr(out):
    return stdout.write(f"{out}\n")


def input():
    return stdin.readline().rstrip()


def int_inp():
    return int(input())


def inp_int_l():
    return list(map(int, stdin.readline().split()))


def inp_l():
    return stdin.readline().split()


####################################


def solve():
    n = int(input())
    graph = [[] for _ in range(n)]
    indegree = [0] * n
    for node in range(n):
        p = int_inp()
        if p != -1:
            graph[p - 1].append(node)
            indegree[node] += 1
    level = [[node for node in range(n) if indegree[node] == 0]]
    queue = deque(level[0])
    while queue:

        lev = []
        for _ in range(len(queue)):
            node = queue.popleft()
            for nei in graph[node]:
                lev.append(nei)
                queue.append(nei)
        level.append(lev)
    count = 0
    for l in level:
        if l:
            count += 1

    print(count)


if __name__ == "__main__":
    t = 1
    for _ in range(t):
        solve()

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en15 English hundera 2024-05-08 08:48:54 6 (published)
en14 English hundera 2024-05-08 08:48:29 3465
en13 English hundera 2024-05-08 08:39:47 2650
en12 English hundera 2024-05-08 08:02:31 2830
en11 English hundera 2024-05-01 18:02:21 1278
en10 English hundera 2024-05-01 17:48:27 1306
en9 English hundera 2024-05-01 17:46:46 39
en8 English hundera 2024-05-01 17:45:43 422
en7 English hundera 2024-05-01 17:41:39 1253
en6 English hundera 2024-05-01 17:41:05 2
en5 English hundera 2024-05-01 17:40:42 1258 Tiny change: '# [A.Party' -> '### [A.Party'
en4 English hundera 2024-05-01 17:40:23 2 Tiny change: '# [A.Party' -> '### [A.Party'
en3 English hundera 2024-05-01 17:40:13 1252
en2 English hundera 2024-05-01 17:39:42 1328
en1 English tesfaymebre 2024-04-26 20:01:42 32 Initial revision (saved to drafts)