Skip to content

Commit 162cdae

Browse files
authored
Added subordinates problem solution
1 parent 1dfb0e2 commit 162cdae

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

CSES/Subordinates.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
3+
Problem Name : Subordinates
4+
Problem Link : https://cses.fi/problemset/task/1674/
5+
6+
*/
7+
8+
#include <bits/stdc++.h>
9+
using namespace std;
10+
11+
typedef vector<int> vi;
12+
typedef vector<vi> vvi;
13+
14+
const char newl = '\n';
15+
const int N = 2e5 + 10;
16+
const int mod = 1e9+7;
17+
18+
int dfs(vvi &child, vi &child_size,int u = 0){
19+
20+
for(int v:child[u]){
21+
child_size[u] += (dfs(child,child_size,v)+1);
22+
}
23+
return child_size[u];
24+
25+
}
26+
void solve(){
27+
28+
int n; cin>>n;
29+
vvi child(n);
30+
31+
for(int i = 1; i < n; i++){
32+
int parent;
33+
cin >> parent;
34+
child[parent-1].push_back(i);
35+
}
36+
37+
vi child_size(n);
38+
dfs(child,child_size);
39+
for(int ele: child_size) cout << ele << " ";
40+
41+
}
42+
43+
int main()
44+
{
45+
ios_base::sync_with_stdio(false);
46+
cin.tie(0);
47+
solve();
48+
return 0;
49+
}

0 commit comments

Comments
 (0)