LogoLogo
  • CP Gymnasium
  • Week 3 Math / Number Theory
    • Week 3 Math / Number Theory
      • Problem 1: Tetration
      • Problem 2: Peragrams
      • Problem 3: Veci
      • Problem 4: Architecture
      • Problem 5: Joint Attack
      • Problem 6: How Many Digits?
      • Problem 7: Abstract Painting
  • Week 4 Array / Greedy
    • Week 4 Array / Greedy
      • Problem 1: Vaccine Efficacy
      • Problem 2: Frosh Week
      • Problem 3: Inquiry
      • Problem 4: Bank Queue
      • Problem 5: Log Land
  • Week 6 Sorting / Binary Search
    • Week 6 Sorting / Binary Search
      • Problem 1: Falling Apart
      • Problem 2: Synchronizing Lists
      • Problem 3: Distributing Ballot Boxes
      • Problem 4: Financial Planning
      • Problem 5: Big Boxes
  • Week 7 Dynamic Programming
    • Week 7 Dynamic Programming
      • Problem 1: Ocean's Anti-11
      • Problem 2: Batmanacci
      • Problem 3: Radio Commercials
      • Problem 4: Welcome to Code Jam (Hard)
      • Problem 5: Honeycomb Walk
  • Week 8 Graph Traversals
    • Week 8 Graph Traversals
      • Problem 1: Reachable Roads
      • Problem 2: Money Matters
      • Problem 3: Squawk Virus
      • Problem 4: Beehives
      • Problem 5: Running MoM
      • Problem 6: Amanda Lounges
Powered by GitBook
On this page
  1. Week 8 Graph Traversals
  2. Week 8 Graph Traversals

Problem 1: Reachable Roads

https://open.kattis.com/problems/reachableroads

Our goal is to find the minimum number of roads that can be added to the grid so that every node is reachable. A good place to start, then, is figuring out what would make a node unreachable.

Let's say we have some node A, and we want to get to some node B. We could get from A to B if there exists a set of edges (A, e1), (e1, e2), ..., (en, B) - a path - from A to B. If such a path does not exist, A and B are unreachable from each other by definition.

We can also note that, if A and B are unreachable from each other, any place that is reachable by A cannot also be reachable by B, because then you would be able to reach B through A by travelling through that point. All of this to say, if A and B are unreachable, they must be in separate connected components. Further, as soon as we connect a node in one connected component to a node in another, all points in those components become mutually reachable.

So! To make all nodes reachable from every other node, you would need to add enough roads to connect all connected components.

If cc = # of connected components, the answer is cc-1.

How do I find Connected Components?

Use your favorite graph traversal method!

Start from some node, and if there are still unvisited nodes when your B/DFS terminates, there are still connected components for you to explore, so call B/DFS again (on an unvisited node).

PreviousWeek 8 Graph TraversalsNextProblem 2: Money Matters

Last updated 4 years ago