Question: icon - menu link - logo question search Sign In Homework Help > Science > Computer Science You are given a tree consisting of N

icon-menu
link-logo
question search
Sign In
Homework Help>Science>Computer Science
You are given a tree consisting of N nodes You are also given two arrays A and P of size N each where the value Ai denotes the value written on the ith node and the value Pi denotes that there is an...
Question
You are given a tree consisting of N nodes
You are also given two arrays A and P of size N each, where the value A[i] denotes the value written on the ith node and the value P[i] denotes that there is an edge between the node i and P[i].
We say that an edge is considered good, if after deleting this edge (this will result in formation of 2 trees), the values in each of the nodes of the trees are distinct.
Find the total number of good edges present in tree.
Input format:
1. The first line contains an integer, N, denoting the number of elements in A
2. Each line i of the N subsequent lines (where 0<= i N) contains an integer describing A[i].
3. Each line i of the N subsequent lines (where 0<= i N) contains an integer describing P[i].
Constraints:
1<= N <=10^5
1<= A[i]<=10^5
1<= P[i]<=10^5
Sample Test Cases:
Case 1:
Input :
2
1
1
0
1
Output:
1
Explanation:
Given N =2, A =[1,1], P =[0,1]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!