Project Euler 186 - Connectedness of a Network

Official link: https://projecteuler.net/problem=186

Thought Process

This was a fun and simple problem!

Image every caller ID is a node, and we create an edge between 2 nodes if a call is made between them, then the problem can easily be stated as finding when the connected component of the prime minister has more than 990,000 members.

We could just run a BFS each time we add an edge, but since the graph is large this would take a lot of time. Instead, we dynamically update every connected component as we add the calls, this way we don't need to run BFS at all!

The idea is simple:

Interactive Code

Due to memory issues the code is too slow to run on the online IDE. Instead I provide my code with comments!

Some extra test cases are: