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 i th 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.
Constraints.
$$$1 \le N \le 10^{5}$$$
$$$1 \le A[i] \le 10^{5}$$$
$$$1 \le P[i] \le 10^{5}$$$
~~~~~~~~~~
import java.util.*;
public class Solution { private static List<List> tree; private static int[] values; private static int goodEdges;
}
~~~~~~~~~~~
Try this maybe
It's worst state time complexity is $$$N{^2}logN$$$