Approach
Depth-first search
For Distinct Gate Paths to Lca, 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
- 103 lines of C++ from the credited upstream file distinct-gate-paths-to-lca.cpp.
- The implementation visibly relies on sequence storage.
- 10 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.
123 45class Solution {6public:7 int distinctPaths(int n, vector<int>& parent, vector<vector<int>>& gates, vector<vector<int>>& queries) {8 static const int64_t MOD = 1e9 + 7;9 enum { BB, BR, RB, RR };10 using Mat = array<int64_t, 4>;11 12 const auto& ceil_log2 = [&](uint32_t x) {13 return bit_width(x - 1);14 };15 16 const auto& mult = [&](const auto& x, const auto& y) -> Mat {17 return {18 (x[BB] * y[BB] + x[BR] * y[RB]) % MOD,19 (x[BB] * y[BR] + x[BR] * y[RR]) % MOD,20 (x[RB] * y[BB] + x[RR] * y[RB]) % MOD,21 (x[RB] * y[BR] + x[RR] * y[RR]) % MOD,22 };23 };24 25 vector<int> depth(n);26 vector<vector<int>> par(ceil_log2(n - 1) + 1, vector<int>(n));27 const auto& lca = [&](int a, int b) {28 if (depth[a] < depth[b]) {29 swap(a, b);30 }31 const auto& d = depth[a] - depth[b];32 for (int k = 0; k < size(par); ++k) {33 if (d & (1 << k)) {34 a = par[k][a];35 }36 }37 if (a == b) {38 return a;39 }40 for (int k = size(par) - 1; k >= 0; --k) {41 if (par[k][a] == par[k][b]) {42 continue;43 }44 a = par[k][a];45 b = par[k][b];46 }47 return par[0][a];48 };49 50 vector<vector<Mat>> cnt(ceil_log2(n - 1) + 1, vector<Mat>(n));51 const auto& count = [&](int u, int card, int t) -> int64_t {52 if (u == t) {53 return 1;54 }55 const auto& d = depth[u] - depth[t];56 int64_t b = (card == 0) ? 1 : 0;57 int64_t r = (card == 1) ? 1 : 0;58 for (int k = 0; k < size(par); ++k) {59 if (!(d & (1 << k))) {60 continue;61 }62 tie(b, r) = pair((b * cnt[k][u][BB] + r * cnt[k][u][RB]) % MOD, (b * cnt[k][u][BR] + r * cnt[k][u][RR]) % MOD);63 u = par[k][u];64 }65 return (b + r) % MOD;66 };67 68 vector<vector<int>> adj(n);69 for (int u = 0; u < n; ++u) {70 if (parent[u] != -1) {71 adj[parent[u]].emplace_back(u);72 }73 }74 vector<int> stk = {0};75 while (!empty(stk)) {76 const auto u = stk.back(); stk.pop_back();77 for (const auto& v : adj[u]) {78 depth[v] = depth[u] + 1;79 stk.emplace_back(v);80 }81 }82 for (int u = 0; u < n; ++u) {83 par[0][u] = parent[u];84 cnt[0][u] = {gates[u][1], gates[u][2], gates[u][2], gates[u][0]};85 }86 for (int k = 1; k < size(par); ++k) {87 for (int u = 0; u < n; ++u) {88 if (par[k - 1][u] == -1) {89 continue;90 }91 par[k][u] = par[k - 1][par[k - 1][u]];92 cnt[k][u] = mult(cnt[k - 1][u], cnt[k - 1][par[k - 1][u]]);93 }94 }95 int result = 0;96 for (const auto& q : queries) {97 const auto& l = lca(q[0], q[2]);98 result ^= (count(q[0], q[1], l) * count(q[2], q[3], l)) % MOD;99 }100 return result;101 }102};103