-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.cpp
More file actions
42 lines (36 loc) · 1.24 KB
/
report.cpp
File metadata and controls
42 lines (36 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <bits/stdc++.h>
using namespace std;
// x番の組織が親組織に提出する枚数を返す
// childrenは組織の関係を表す2次元配列(参照渡し)
int count_report_num(vector<vector<int>> &children, int x) {
// (ここに追記して再帰関数を実装する)
//ベースケース 自分の会社分
int count = 1;
if (children.at(x).size() == 0){
return count;
}
for (int i = 0; i < children.at(x).size(); i++){
count += count_report_num(children,children.at(x).at(i)) ;
}
return count;
}
// これ以降の行は変更しなくてよい
int main() {
int N;
cin >> N;
vector<int> p(N); // 各組織の親組織を示す配列
p.at(0) = -1; // 0番組織の親組織は存在しないので-1を入れておく
for (int i = 1; i < N; i++) {
cin >> p.at(i);
}
// 組織の関係から2次元配列を作る
vector<vector<int>> children(N); // ある組織の子組織の番号一覧
for (int i = 1; i < N; i++) {
int parent = p.at(i); // i番の親組織の番号
children.at(parent).push_back(i); // parentの子組織一覧にi番を追加
}
// 各組織について、答えを出力
for (int i = 0; i < N; i++) {
cout << count_report_num(children, i) << endl;
}
}