Sum of Perfect Square Ancestors

You are given an integer n and an undirected tree rooted at node 0 with n nodes numbered from 0 to n - 1. This is represented by a 2D array edges of length n - 1, where edges[i] = [ui, vi] indicates an undirected edge between nodes ui and vi.

You are also given an integer array nums, where nums[i] is the positive integer assigned to node i.

Define a value ti as the number of ancestors of node i such that the product nums[i] * nums[ancestor] is a perfect square.

Return the sum of all ti values for all nodes i in range [1, n - 1].

Note:

  • In a rooted tree, the ancestors of node i are all nodes on the path from node i to the root node 0, excluding i itself.
Example 1
Inputn = 3, edges = [[0,1],[1,2]], nums = [2,8,2]
Output3
Node 1 has one valid ancestor pair and node 2 has two valid ancestor pairs, so the total is 3.
Example 2
Inputn = 3, edges = [[0,1],[0,2]], nums = [1,2,4]
Output1
Only node 2 forms a perfect square product with its ancestor node 0, so the total is 1.

Constraints

  • 1 <= n <= 10^5
  • edges.length == n - 1
  • edges[i] = [ui, vi]
  • 0 <= ui, vi <= n - 1
  • nums.length == n
  • 1 <= nums[i] <= 10^5
  • The input is generated such that edges represents a valid tree.

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate