3 ms·
[(1, 2, 3), (2, 4, 5)] would result in a graph like this: 1 - 2 - 3 \ 4 - 5 Building the (undirected) graph would take linear time, and once it i
by gk101 10y ago
[(1, 2, 3), (2, 4, 5)] would result in a graph like this:
1 - 2 - 3
\
4 - 5
Building the (undirected) graph would take linear time, and once it is built, you can do a simple Depth First Search to mark all the connected components.