Incremental Even-Weighted Cycle Queries Problem
Description
LeetCode Problem 3887.
You are given a positive integer n.
There is an undirected graph with n nodes labeled from 0 to n - 1. Initially, the graph has no edges.
You are also given a 2D integer array edges, where edges[i] = [u_i, v_i, w_i] represents an edge between nodes u_i and v_i with weight w_i. The weight w_i is either 0 or 1.
Process the edges in edges in the given order. For each edge, add it to the graph only if, after adding it, the sum of the weights of the edges in every cycle in the resulting graph is even.
Return an integer denoting the number of edges that are successfully added to the graph.
Example 1:
Input: n = 3, edges = [[0,1,1],[1,2,1],[0,2,1]]
Output: 2
Explanation:

- [0, 1, 1]: We add the edge between vertex 0 and vertex 1 with weight 1.
- [1, 2, 1]: We add the edge between vertex 1 and vertex 2 with weight 1.
- [0, 2, 1]: The edge between vertex 0 and vertex 2 (the dashed edge in the diagram) is not added because the cycle 0 - 1 - 2 - 0 has total edge weight 1 + 1 + 1 = 3, which is an odd number.
Example 2:
Input: n = 3, edges = [[0,1,1],[1,2,1],[0,2,0]]
Output: 3
Explanation:

- [0, 1, 1]: We add the edge between vertex 0 and vertex 1 with weight 1.
- [1, 2, 1]: We add the edge between vertex 1 and vertex 2 with weight 1.
- [0, 2, 0]: We add the edge between vertex 0 and vertex 2 with weight 0.
- Note that the cycle 0 - 1 - 2 - 0 has total edge weight 1 + 1 + 0 = 2, which is an even number.
Constraints:
3 <= n <= 5 * 10^41 <= edges.length <= 5 * 10^4edges[i] = [u_i, v_i, w_i]0 <= u_i < v_i < n- All edges are distinct.
w_i = 0 or w_i = 1
Solution
Approach: Union-Find with Parity
Every cycle has an even total weight exactly when the weights can be explained by labels c(x) in {0, 1} on the nodes, with w(u, v) = c(u) xor c(v) for every edge. So an edge may be added if and only if it keeps such labels possible.
Use a union-find where each node stores the parity of the path to its parent, so that find also gives the parity from a node to its root. For an edge (u, v, w):
- If
uandvare in different components, join them and set the parity of the joined root tod[u] xor d[v] xor w. The edge creates no cycle, so it is always added. - If they are already in the same component, the edge closes a cycle whose weight parity is
d[u] xor d[v] xor w. Add the edge only when that is0, that isd[u] xor d[v] == w.
Rejected edges are not added to the graph, so they change nothing. Count the added edges. Union by size keeps the trees shallow.
Complexity: time O((n + m) * log n) or better, space O(n), where m is the number of edges.
class Solution:
def numberOfEdgesAdded(self, n: int, edges: List[List[int]]) -> int:
p = list(range(n))
d = [0] * n # parity of the path from a node to its parent
def find(x):
path = []
while p[x] != x:
path.append(x)
x = p[x]
# compress: walk back so each node points to the root
acc = 0
for y in reversed(path):
acc ^= d[y]
d[y] = acc
p[y] = x
return x
ans = 0
for u, v, w in edges:
ru, rv = find(u), find(v)
if ru == rv:
if d[u] ^ d[v] == w:
ans += 1
else:
p[ru] = rv
d[ru] = d[u] ^ d[v] ^ w
ans += 1
return ans