fork download
  1. import sys
  2. input=sys.stdin.readline
  3.  
  4. t=int(input())
  5. for _ in range(t):
  6. n=int(input())
  7. parents=list(map(int,input().split()))
  8. cnt=[0]*(n+1)
  9. cnt[0]+=1
  10. for p in parents:
  11. cnt[p]+=1
  12.  
  13. arr=[x for x in cnt if x>0]
  14. arr.sort(reverse=True)
  15.  
  16. r=[]
  17. time=0
  18.  
  19. while arr:
  20. if time==0:
  21. r.append(arr[0]-1)
  22. time+=1
  23. arr.pop(0)
  24. else:
  25. for i in range(len(r)):
  26. r[i]-=1
  27. r.append(arr[0]-1)
  28. arr.pop(0)
  29. time+=1
  30.  
  31. ar=[y for y in r if y>0]
  32.  
  33. if not ar:
  34. print(time)
  35. else:
  36. ar.sort(reverse=True)
  37. while ar:
  38. for j in range(len(ar)):
  39. ar[j]-=1
  40. time+=1
  41. ar=[x for x in ar if x>0]
  42.  
  43. print(time)
Success #stdin #stdout 0.11s 14020KB
stdin
5
7
1 1 1 2 2 4
5
5 5 1 4
2
1
3
3 1
6
1 1 1 1 1
stdout
4
4
2
3
5