Sum of Perfect Square Ancestors Problem
Description
LeetCode Problem 3715.
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] = [u_i, v_i] indicates an undirected edge between nodes u_i and v_i.
You are also given an integer array nums, where nums[i] is the positive integer assigned to node i.
Define a value t_i 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 t_i values for all nodes i in range [1, n - 1].
Note:
- In a rooted tree, the ancestors of node
iare all nodes on the path from nodeito the root node 0, excludingiitself.
Example 1:
Input: n = 3, edges = [[0,1],[1,2]], nums = [2,8,2]
Output: 3
Explanation:
i Ancestors nums[i] * nums[ancestor] Square Check t_i
1 [0] nums[1] * nums[0] = 8 * 2 = 16 16 is a perfect square 1
2 [1, 0] nums[2] * nums[1] = 2 * 8 = 16 nums[2] * nums[0] = 2 * 2 = 4 Both 4 and 16 are perfect squares 2
Thus, the total number of valid ancestor pairs across all non-root nodes is 1 + 2 = 3.
Example 2:
Input: n = 3, edges = [[0,1],[0,2]], nums = [1,2,4]
Output: 1
Explanation:
i Ancestors nums[i] * nums[ancestor] Square Check t_i
1 [0] nums[1] * nums[0] = 2 * 1 = 2 2 is not a perfect square 0
2 [0] nums[2] * nums[0] = 4 * 1 = 4 4 is a perfect square 1
Thus, the total number of valid ancestor pairs across all non-root nodes is 1.
Example 3:
Input: n = 4, edges = [[0,1],[0,2],[1,3]], nums = [1,2,9,4]
Output: 2
Explanation:
i Ancestors nums[i] * nums[ancestor] Square Check t_i
1 [0] nums[1] * nums[0] = 2 * 1 = 2 2 is not a perfect square 0
2 [0] nums[2] * nums[0] = 9 * 1 = 9 9 is a perfect square 1
3 [1, 0] nums[3] * nums[1] = 4 * 2 = 8 nums[3] * nums[0] = 4 * 1 = 4 Only 4 is a perfect square 1
Thus, the total number of valid ancestor pairs across all non-root nodes is 0 + 1 + 1 = 2.
Constraints:
1 <= n <= 10^5edges.length == n - 1edges[i] = [u_i, v_i]0 <= u_i, v_i <= n - 1nums.length == n1 <= nums[i] <= 10^5- The input is generated such that
edgesrepresents a valid tree.
Solution
Approach: Square-Free Part + DFS Counting
a * b is a perfect square exactly when a and b have the same square-free part (the number left after dividing out every square factor). So reduce each nums[i] to its square-free part with a sieve: for every p with p * p <= max(nums), divide m by p * p as long as it divides, for each multiple m of p * p.
Then run a DFS from the root (iteratively, since the tree can be 10^5 deep) with a table cnt of the square-free parts on the current root path. On entering a node, add cnt[part] to the answer (that is the number of matching ancestors), then increment it; on leaving the node, decrement it.
Complexity: O(n + M log M) time and O(n + M) space, where M = max(nums).
class Solution:
def sumOfAncestors(self, n: int, edges: List[List[int]], nums: List[int]) -> int:
mx = max(nums)
# sf[m]: m with all square factors removed
sf = list(range(mx + 1))
p = 2
while p * p <= mx:
q = p * p
for m in range(q, mx + 1, q):
while sf[m] % q == 0:
sf[m] //= q
p += 1
g = [[] for _ in range(n)]
for u, v in edges:
g[u].append(v)
g[v].append(u)
cnt = [0] * (mx + 1)
par = [-1] * n
ans = 0
stack = [0]
while stack:
v = stack.pop()
if v < 0:
cnt[sf[nums[~v]]] -= 1
continue
k = sf[nums[v]]
ans += cnt[k]
cnt[k] += 1
stack.append(~v)
for u in g[v]:
if u != par[v]:
par[u] = v
stack.append(u)
return ans