package leetcode;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* @author 江河
* @date 2019-09-09 20:16
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()){
//输入第一行
String str1 = sc.nextLine();
String[] numstr1 = str1.split(" ");
int[] num1 = new int[numstr1.length];
List<Integer> pids = new ArrayList<>();
for(int i = 0; i < numstr1.length; i ++) {
num1[i] = Integer.parseInt(numstr1[i]);
pids.add(num1[i]);
}
//输入第二行
String str2 = sc.nextLine();
String[] numstr2 = str2.split(" ");
int[] num2 = new int[numstr2.length];
List<Integer> ppids = new ArrayList<>();
for(int i = 0; i < numstr2.length; i ++) {
num2[i] = Integer.parseInt(numstr2[i]);
ppids.add(num2[i]);
}
//输入第三行
String str3 = sc.nextLine();
String[] numstr3 = str3.split(" ");
int[] num3 = new int[numstr3.length];
num3[0] = Integer.parseInt(numstr3[0]);
int kills= num3[0];
List<Integer> ss = killProcess(pids,ppids,kills);
int x= ss.size();
System.out.println(x);
}
}
/**
* 下面是一个找遍历的递归的树是个集合
* @param pid
* @param ppid
* @param kill
* @return
*/
public static List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
ArrayList<Integer> ans = new ArrayList<Integer>();
ans.add(kill);
for (int i = 0; i < ppid.size(); i++) {
if (ppid.get(i) == kill) {
ans.addAll(killProcess(pid, ppid, pid.get(i)));
}
}
return ans;
}
}