Skip to content

Instantly share code, notes, and snippets.

@jungrae-prestolabs
Created May 12, 2018 10:12
Show Gist options
  • Save jungrae-prestolabs/ce7fd4da4835fb1a36ca7482883a3bfb to your computer and use it in GitHub Desktop.
Save jungrae-prestolabs/ce7fd4da4835fb1a36ca7482883a3bfb to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <vector>
using namespace std;
int n,root,cnt,chk[100];
vector<int> v[100];
void dfs(int a){
chk[a]=1;
bool flag=true;
for (int i=0;i<v[a].size();i++){
if (!chk[v[a][i]]) dfs(v[a][i]),flag=false;
}
if (flag) cnt++;
}
int main(){
int i,a;
scanf("%d",&n);
for (i=0;i<n;i++){
scanf("%d",&a);
if (a==-1) root=i;
else v[a].push_back(i);
}
scanf("%d",&a); chk[a]=1;
if (!chk[root]) dfs(root);
printf("%d",cnt);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment