Skip to content
Back to libraryPart 3 · Graphs and dynamic programming

3.2Disjoint sets

Before you start

It helps to have read 3.1 Graph search: BFS and DFS and 1.5 Sorting. Overall level: Intermediate.

Practice: Disjoint sets

0 of 4 solved

About these problems

Apply, Connect, Reason and Stretch describe difficulty within this topic, not an official contest rating. Open any problem in any order; reading the chapter first is optional.

  1. 1Connection checksThere are n vertices, numbered 1 to n, with no connections at first. Then q queries arrive in order. Query 1 u v joins u and v. Query 2 u v asks whether u and v are currently connected through joins so far; a vertex is always connected to itself. Print YES or NO for each type 2 query.ApplyNot started
  2. 2Growing networkA graph has n vertices, numbered 1 to n, and no edges at first. Then m undirected edges are added one at a time. Print the number of connected components and the size of the largest component after each addition.ConnectNot started
  3. 3Redundant linksA graph has n vertices, numbered 1 to n, and no links at first. Add m undirected links one at a time in input order. A link is redundant if its endpoints were already connected before adding it; a self-loop is always redundant. Count the redundant links.ReasonNot started
  4. 4Least-cost backboneAn undirected graph has n vertices, numbered 1 to n, and m edges, each with a weight. Find the minimum total weight of a set of edges that connects all n vertices. Print -1 if the vertices cannot all be connected. A one-vertex graph needs no edges and costs 0.StretchNot started

Read the lesson