fork download
  1. from sys import stdin,stdout,setrecursionlimit
  2. setrecursionlimit(300002)
  3. from math import *
  4. from collections import *
  5. from pprint import pprint
  6. import random
  7. def read_list_line(elem_func=int):
  8. return [elem_func(x) for x in stdin.readline().strip().split()]
  9.  
  10. def read_line(elem_func=str):
  11. return elem_func(stdin.readline().strip())
  12.  
  13. def print_list_line(the_list,elem_func=str,sep=' ',end="\n"):
  14. st=(sep.join([elem_func(x) for x in the_list]))+end
  15. stdout.write(st)
  16. return st
  17.  
  18. def print_line(st):
  19. stdout.write(str(st)+"\n")
  20. return st
  21.  
  22.  
  23. ntest = 1 #
  24. # ntest = read_line(int)
  25.  
  26. def main():
  27. n = 200000
  28. # n = read_line(int)
  29. r=[0] + list(range(2,n+1))+[1]
  30. # r=[0] + read_list_line(int)
  31. stp=[[]]
  32. for r0 in r:
  33. stp[0].append(r0)
  34. for i in range(1,int(log2(n*4))):
  35. stp.append(stp[i-1].copy())
  36. for j in range(1,n+1):
  37. stp[i][j]=stp[i-1][stp[i-1][j]]
  38. stable=[0]*(n+1)
  39. for i in range(1,1+n):
  40. stable[stp[len(stp)-1][i]]=1
  41.  
  42. def dfs(u):
  43. if (stable[u]>=1):
  44. return
  45. v = r[u]
  46. dfs(v)
  47. stable[u]=stable[v]+1
  48. for i in range(1,n+1):
  49. if (stable[i]==0):
  50. dfs(i)
  51. print_line(max(stable)+1)
  52. return 0
  53.  
  54. for test_case in range(1,1+ntest):
  55. main()
Success #stdin #stdout 0.72s 48992KB
stdin
Standard input is empty
stdout
2