File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed
Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change 1+ // https://www.acmicpc.net/problem/1068
2+
3+ #include < bits/stdc++.h>
4+ using namespace std ;
5+
6+ int N, dv;
7+ bool flag[51 ];
8+ vector <int > parent[51 ];
9+
10+ void dfs (int dv){
11+ flag[dv] = true ;
12+ if (parent[dv].size () == 0 ) {
13+ return ;
14+ }
15+
16+ for (int i = 0 ; i < parent[dv].size (); i++){
17+ dfs (parent[dv][i]);
18+ }
19+ }
20+
21+ int main () {
22+ ios_base::sync_with_stdio (false );
23+ cin.tie (NULL ); cout.tie (NULL );
24+
25+ cin >> N;
26+ for (int i = 0 ; i < N; i++){
27+ int v;
28+ cin >> v;
29+ if (v == -1 ) continue ;
30+ parent[v].push_back (i);
31+ }
32+ cin >> dv;
33+ dfs (dv);
34+
35+ int res = 0 ;
36+ for (int i = 0 ; i < N; i++){
37+ int cnt = 0 ;
38+ for (int j = 0 ; j < parent[i].size (); j++){
39+ if (flag[parent[i][j]]) cnt++;
40+ }
41+ if (!flag[i] && cnt == parent[i].size ()) res++;
42+ }
43+ cout << res;
44+
45+ return 0 ;
46+ }
You can’t perform that action at this time.
0 commit comments