14239 - Seafu FrankLee   

Description

In the programming academy, students enjoy learning from each other, where each person can become the “seafu” of others. Over time, this has led to a unique hierarchical system similar to a tree structure.

Within the academy, every student has exactly one "direct seafu,” except for the "Master FrankLee”, who sits atop the academy's hierarchy. He is the only one without a "direct seafu”, and he is the “seafu” of every students.

There are N students in the academy, each person has a uniuqe id number from 1 ~ N. There is a unique custom in the academy, seafu's id must be smaller than disciples' id. So "FrankLee" is always id number 1.

Now give you every student's direct seafu's id (except “FrankLee”), please answer the following questions.

Note: a student can be more than 1 students' "direct seafu", but can only have 1 "direct seafu".

 

Disciples Query:

Given an id number X. Output the number of disciples of student X.

A person is disciple of student X if student X is this person’s seafu. So Master FrankLee has N-1 disciples.

 

Seafu Test:

Given 2 id numbers A, B. (A≠B)

Check if student A is B’s seafu or student B is A’s seafu or neither.

if student A is B’s seafu, output "1".

if student B is A’s seafu, output "0".

if neither output "-1"

 

Note: A person x is person y’s seafu if person x is y’s direct seafu or y’s direct seafu’s direct seafu or y’s direct seafu’s direct seafu’s direct seafu, and so on…

Input

  • The first line contains an integer N (1 ≤ N ≤ 1000), representing the number of people (excluding FrankLee) in the academy.
  • The second line contains N - 1 space-separated integers, representing p2, p3, ..., pN, which pi means the direct seafu id of student i.
  • Student id number 1 is always master FrankLee. And it is guarantee that pi < i.
  • The following line contains an integer Q (1 ≤ Q ≤ 1000), representing the number of queries.
  • Each of the next Q lines contains a query "1 X"(disiples query) or "2 A B"(seafu test).

Constraints

  • Testcase 1: There is only seafu test query, and the answer is to the query is either 0 or 1.
  • Testcase 2: There is only seafu test query.
  • Testcase 3~4: There is only disiples query. 
  • Testcase 5~6: Satisfy no additional constraints.

 

Output

  • Output Q lines, each lines contain the answer tod  each query.

Sample Input  Download

Sample Output  Download

Tags




Discuss