Graph Theory37 sections · 1633 units
Open in Course

CSES 1676 Road Construction - Logic

Maintaining State

1.1. Components: Start at nn. Decrement on successful union.

2.2. Max size: Start at 11.

After union, update to max of current and new component size. After each edge, output components maxSize. Time: O(mα(n))O(m \cdot \alpha(n)) for mm edges. This is the best you can do for this problem type. Use union by size (not rank) so you always know each component's exact size from the root's stored value.