Graph Theory37 sections · 1633 units
Open in Course

Problem - Road Construction

CSES - DSU application

You have n cities and m roads. Each road connects two cities. Initially, no roads exist. Roads are added one by one. After each road, output two numbers: the number of connected components and the size of the largest component.

This is an online connectivity problem. You process edges as they arrive and answer queries after each one. The DSU data structure is built for this. Initialize with nn components of size 11, then union as each road arrives.