Company: Uber

Difficulty: hard

Problem Statement

# Co-prime Subtree Analysis A service dependency graph is a rooted tree of `N` services numbered `1` through `N`, with service `1` as the root. Service `i` has a positive integer security signature `value[i]`. For every service `v`, count its strict descendants `u` for which `gcd(value[v], value[u]) = 1`. Print the sum of these counts over all services. ## Input Format - The first line contains `N`. - The second line contains `N - 1` integers; the `i`-th integer is the parent of service `i + 2`. This line is empty when `N = 1`. - The third line contains `N` integers `value[1] ... value[N]`. ## Output Format Print the total number of co-prime ancestor-descendant pairs. ## Constraints - `1 <= N <= 100000` - `1 <= value[i] <= 1000` - The parent array describes a valid rooted tree. - Use 64-bit arithmetic for the answer. ## Example Input: 3 1 2 2 3 5 Output: 3 ## Notes - A vertex is not its own descendant. - Only the ancestor-descendant relationship matters; unrelated vertices

More Uber OA questionsInterview experiences