Approach
Depth-first search
For Sum of Perfect Square Ancestors, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- Combine child results or undo the choice when the branch finishes.
Code notes
- 121 lines of C++ from the credited upstream file sum-of-perfect-square-ancestors.cpp.
- The implementation visibly relies on sequence storage, hash lookup.
- 8 loop blocks detected, together with recursive traversal.
Complexity
Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1234 5vector<int> linear_sieve_of_eratosthenes(int n) { 6 vector<int> spf(n + 1, -1);7 vector<int> primes;8 for (int i = 2; i <= n; ++i) {9 if (spf[i] == -1) {10 spf[i] = i;11 primes.emplace_back(i);12 }13 for (const auto& p : primes) {14 if (i * p > n || p > spf[i]) {15 break;16 }17 spf[i * p] = p;18 }19 }20 return spf;21}22 23const int MAX_NUMS = 1e5;24const auto& SPF = linear_sieve_of_eratosthenes(MAX_NUMS);25 2627class Solution {28public:29 long long sumOfAncestors(int n, vector<vector<int>>& edges, vector<int>& nums) {30 const auto& prime_factors = [](int x) {31 int result = 1;32 for (; x != 1; x /= SPF[x]) {33 if (result % SPF[x] == 0) {34 result /= SPF[x];35 } else {36 result *= SPF[x];37 }38 }39 return result;40 };41 42 vector<vector<int>> adj(n);43 for (const auto& e : edges) {44 adj[e[0]].emplace_back(e[1]);45 adj[e[1]].emplace_back(e[0]);46 }47 unordered_map<int, int> cnt;48 const auto& iter_dfs = [&]() {49 int64_t result = 0;50 using P = tuple<int, int, int, int, int>;51 vector<P> stk = {{1, 0, -1, -1, 0}};52 while (!empty(stk)) {53 const auto [step, u, p, i, x] = stk.back(); stk.pop_back();54 if (step == 1) {55 const auto& x = prime_factors(nums[u]);56 result += cnt[x]++;57 stk.emplace_back(3, -1, -1, -1, x);58 stk.emplace_back(2, u, p, 0, 0);59 } else if (step == 2) {60 if (i == size(adj[u])) {61 continue;62 }63 stk.emplace_back(2, u, p, i + 1, 0);64 const auto& v = adj[u][i];65 if (v == p) {66 continue;67 }68 stk.emplace_back(1, v, u, -1, 0);69 } else if (step == 3) {70 --cnt[x];71 }72 }73 return result;74 };75 76 return iter_dfs();77 }78};79 8081828384class Solution2 {85public:86 long long sumOfAncestors(int n, vector<vector<int>>& edges, vector<int>& nums) {87 const auto& prime_factors = [](int x) {88 int result = 1;89 for (; x != 1; x /= SPF[x]) {90 if (result % SPF[x] == 0) {91 result /= SPF[x];92 } else {93 result *= SPF[x];94 }95 }96 return result;97 };98 99 vector<vector<int>> adj(n);100 for (const auto& e : edges) {101 adj[e[0]].emplace_back(e[1]);102 adj[e[1]].emplace_back(e[0]);103 }104 unordered_map<int, int> cnt;105 const function<int64_t (int, int)> dfs = [&](int u, int p) {106 const auto& x = prime_factors(nums[u]);107 int64_t result = cnt[x]++;108 for (const auto& v : adj[u]) {109 if (v == p) {110 continue;111 }112 result += dfs(v, u);113 }114 --cnt[x];115 return result;116 };117 118 return dfs(0, -1);119 }120};121