Here is the **full question rewritten cleanly in t...
Creato il: 14 gennaio 2026
Risposto usando GPT-5.2 Thinking da Chat01
Creato il: 14 gennaio 2026
Risposto usando GPT-5.2 Thinking da Chat01
Here is the full question rewritten cleanly in text, exactly matching what’s shown in the screenshots, without adding or changing logic.
A database team is migrating their system to a new cluster-based architecture.
You are asked to determine whether it is possible to create controller–worker configurations for each subtree in a network of databases.
A tree with network_nodes nodes.
Two integer arrays:
network_fromnetwork_to(network_from[i], network_to[i]) represents an edge between two nodes.This means that the nodes of a subtree must be divided into disjoint adjacent pairs.
For each node, determine whether a valid configuration is possible for the subtree rooted at that node.
Return a string consisting of characters '1' and '0' where:
'1' means a valid configuration is possible'0' means a valid configuration is not possibleThe i-th character of the string corresponds to node i.
A subtree for a given node x (in a rooted tree) is defined as:
x itselfxNode x is a descendant of node y if there is a path from x to the root that passes through y.
Input
textnetwork_nodes = 4 network_from = [3, 2, 1] network_to = [4, 4, 2]
Tree Structure
text1 | 2 | 4 / 3
Output
1001
Explanation
1: valid pairing exists → 12: no valid pairing → 03: no valid pairing → 04: valid pairing exists → 1Complete the function:
textstring isColoringPossible( int network_nodes, vector<int> network_from, vector<int> network_to )
network_nodes'1' or '0'2 ≤ network_nodes ≤ 3 × 10^51 ≤ network_from[i], network_to[i] ≤ network_nodesc++ code correct
pythondef solve(n, fr, to): g=[[] for _ in range(n+1)] for a,b in zip(fr,to): g[a].append(b); g[b].append(a) parent=[0]*(n+1) children=[[] for _ in range(n+1)] order=[] st=[1] parent[1]=-1 while st: u=st.pop() order.append(u) for v in g[u]: if v==parent[u]: continue parent[v]=u children[u].append(v) st.append(v) dp0=[False]*(n+1) dp1=[False]*(n+1) for u in reversed(order): ch=children[u] if not ch: dp0[u]=True; dp1[u]=False continue k=len(ch) pref=[True]*(k+1) for i in range(k): pref[i+1]=pref[i] and dp1[ch[i]] suf=[True]*(k+1) for i in range(k-1,-1,-1): suf[i]=suf[i+1] and dp1[ch[i]] dp0[u]=pref[k] ok=False for i in range(k): if dp0[ch[i]] and pref[i] and suf[i+1]: ok=True; break dp1[u]=ok return ''.join('1' if dp1[i] else '0' for i in range(1,n+1)) print(solve(4,[3,2,1],[4,4,2]))
Below is an O(n) C++ solution (works for n ≤ 3e5) using a standard tree DP for perfect matching on every rooted subtree (root assumed to be node 1, matching the usual “subtree rooted at i” definition used in these problems and your example).
Root the tree at 1. For each node u, compute:
dp0[u] = subtree of u can be perfectly paired except u is left unpaired (so u would need to pair with its parent outside the subtree).dp1[u] = subtree of u can be perfectly paired and u is paired with exactly one of its children (fully paired inside the subtree).For the answer for node u’s subtree (which does not include its parent), we need u paired inside the subtree, so output '1' iff dp1[u] is true.
Transitions:
dp0[u] is true iff all children c satisfy dp1[c] (each child must be fully matched internally).dp1[u] is true iff there exists a child t such that:
dp0[t] is true (child t can be internally matched except itself, because it will match with u)c ≠ t satisfy dp1[c]We compute the “all other children” condition efficiently with prefix/suffix AND in O(deg(u)).
cpp#include <bits/stdc++.h> using namespace std; string isColoringPossible( int network_nodes, vector<int> network_from, vector<int> network_to ) { int n = network_nodes; int m = (int)network_from.size(); // Build adjacency with degree-based reserve (faster + fewer reallocs) vector<int> deg(n + 1, 0); for (int i = 0; i < m; i++) { deg[network_from[i]]++; deg[network_to[i]]++; } vector<vector<int>> adj(n + 1); for (int i = 1; i <= n; i++) adj[i].reserve(deg[i]); for (int i = 0; i < m; i++) { int a = network_from[i], b = network_to[i]; adj[a].push_back(b); adj[b].push_back(a); } // Root the tree at 1 vector<int> parent(n + 1, 0); vector<vector<int>> children(n + 1); for (int i = 1; i <= n; i++) children[i].reserve(deg[i]); vector<int> order; order.reserve(n); vector<int> st; st.reserve(n); st.push_back(1); parent[1] = -1; while (!st.empty()) { int u = st.back(); st.pop_back(); order.push_back(u); for (int v : adj[u]) { if (v == parent[u]) continue; parent[v] = u; children[u].push_back(v); st.push_back(v); } } // dp0[u]: subtree(u) matched except u is free // dp1[u]: subtree(u) fully matched and u matched with one child vector<char> dp0(n + 1, 0), dp1(n + 1, 0); // Postorder compute for (int idx = n - 1; idx >= 0; --idx) { int u = order[idx]; auto &ch = children[u]; int k = (int)ch.size(); if (k == 0) { dp0[u] = 1; // leaf can be "all matched except itself" dp1[u] = 0; // can't match inside (no child) continue; } // prefix/suffix AND of dp1 on children vector<char> pref(k + 1, 1), suf(k + 1, 1); for (int i = 0; i < k; i++) pref[i + 1] = (char)(pref[i] && dp1[ch[i]]); for (int i = k - 1; i >= 0; i--) suf[i] = (char)(suf[i + 1] && dp1[ch[i]]); // u is free => all children must be fully matched internally dp0[u] = pref[k]; // u matched to exactly one child t: // child t must be dp0[t] (t free internally), others must be dp1 char ok = 0; for (int i = 0; i < k; i++) { int t = ch[i]; if (dp0[t] && pref[i] && suf[i + 1]) { ok = 1; break; } } dp1[u] = ok; } // Answer for each node i: subtree rooted at i must match i internally => dp1[i] string ans; ans.reserve(n); for (int i = 1; i <= n; i++) { ans.push_back(dp1[i] ? '1' : '0'); } return ans; }
O(n)O(n)If you want, I can also provide a version that avoids storing children (slightly lower memory), but the above is already safe and fast for 3e5.