0
0

More than 3 years have passed since last update.

ABC備忘録[ABC163 C - managementr] (Python)

Posted at

問題文

$N$人の社員からなる会社があり、各社員には$1,...,N$の社員番号が割り当てられています。
社員番号1の社員以外の全ての社員には、自分より社員番号が小さい直属の上司がちょうど$1$人います。
$X$さんが$Y$さんの直属の上司であるとき、$Y$さんは$X$さんの直属の部下であるといいます。
社員番号$i$の社員の直属の上司の社員番号が$A_i$であることが与えられます。各社員について直属の部下が何人いるか求めてください。

制約

$2≤N≤2×10^5$
$1≤A_i<i$

ABC163 C - managementr

解法

入力されるAの要素がそれぞれ何個あるかを求め、それらの値を順に出力すればよい。

import collections

N = int(input())
A = list(map(int,input().split()))
c = collections.Counter(A)

for i in range(1, N + 1):
  print(c[i])

collections.Counter

collectionsライブラリのCounterクラスを使うことで配列の各要素数を求められる。

l = [1,1,2,2,2,3,4,4]
c = collections.Counter(l)

print(c)
# Counter({1: 2, 2: 3, 3: 1, 4: 2})
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0