I solved this by a brute force

By removing each and every pair, if no of connected components are greater than initial no , count ++

But it was very lengthy, and I saw many short codes, Which I couldn't understand?

Any optimized Algorithm to solve these kind of problems?

Thanks in advance !

The main point here is

notto use an optimized algorithm. Instead, use the simplest algorithm that is fast enough.With n=20, you can use Floyd-Warshall and two cycles to count connected components in like 5 lines of code. Here is my code that does that:

Thanks a lotmisof !Can u please explain this in

C++?It tried and failed in understanding your Python Code