The Question
Coding

Consistent Parity Cycle Detection

Given an undirected graph with $n$ nodes and a sequence of weighted edges where weights are either 0 or 1, process the edges one by one in the given order. An edge is added to the graph if and only if its inclusion does not create any cycle with an odd sum of weights. Determine the total number of edges successfully added to the graph.
Java
Union-Find
XOR
Graph Theory
Questions & Insights

Clarifying Questions

What are the constraints on n and the number of edges? (Assuming n, |edges| \le 10^5 for typical competitive programming constraints).
Are multiple edges between the same two nodes allowed? (If so, they should be treated like any other edge).
Does the "every cycle" condition imply that once a graph becomes non-acyclic, we still need to maintain the parity property for all possible cycles formed? (Yes, if a graph has cycles, every single one must have an even sum of weights).
Can the weights be anything other than 0 or 1? (The problem states 0 or 1, which simplifies the logic to XOR operations).
Assumptions:
The graph is undirected.
We process edges in the exact order provided.
The condition "sum of weights is even" is equivalent to "XOR sum of weights is 0" since weights are only 0 or 1.
A graph where every cycle has an even weight sum allows us to consistently assign a binary state (0 or 1) to each node such that for any edge (u, v) with weight w, state[u] \oplus state[v] = w.

Thinking Process

To ensure every cycle has an even weight sum, the graph must maintain a "consistent parity" property. This is a generalization of bipartiteness. If w=1 for all edges, the graph must be bipartite. If weights are 0 or 1, we can think of this as a 2-coloring problem where the color difference is constrained by w.
We use a Disjoint Set Union (DSU) with "potentials" (or weights). Each node in the DSU will store its parent and the parity of the weight of the path to that parent.
For each edge (u, v, w):
If u and v are in different components, adding the edge will not create a cycle. We union the sets and calculate the parity of the new bridge such that the relative parities of all nodes remain consistent.
If u and v are already in the same component, adding the edge creates a cycle. We check the existing parity of the path between u and v in the DSU. If dist(u, v) \oplus w == 0, the cycle is even and the edge is added. Otherwise, it's rejected.
This approach ensures that we only add edges that preserve the property that any path between two nodes u and v has the same parity.
Implementation Breakdown

Problem Set

Functional Requirement: Process a stream of edges and return the count of edges that satisfy the even-cycle parity constraint.
Input: int n, int[][] edges.
Output: int count.
Constraint: Must handle up to 10^5 nodes and edges within O(E \cdot \alpha(N)) time.

Approach

Algorithm: Disjoint Set Union (DSU) with Path Compression and Parity Tracking.
Data Structure: Array-based DSU (parent[] and dist[]).
Complexity:
Time: O(E \cdot \alpha(N)), where \alpha is the inverse Ackermann function.
Space: O(N) to store DSU structures.

Implementation

Wrap Up

Advanced Topics

Path Compression vs. Union by Rank: While the current solution uses recursion for path compression, in high-performance Java environments, using Union by Rank/Size is recommended to keep the tree shallow and avoid StackOverflowError for very deep trees (though 10^5 is usually fine for Java's default stack).
Thread Safety: If edges were arriving via multiple threads, the DSU would require fine-grained locking (e.g., locking the roots) or a concurrent Disjoint Set implementation to maintain the parity consistency without race conditions.
Offline vs. Online: This is an online approach. If the problem asked for the maximum number of edges we could choose to keep the graph even-cycled (rather than processing in order), it would become a variation of the Maximum Spanning Forest problem.