try on google
idea is simple :
1. create vector of such number of elements (or table) of graph (nodes)
2. initialize all elements to our number (i.e. 1,2,3 .... for node 1,2,3 ...)
3, for each added edge update values in such way, that to groups of nodes, which are separate earlier join in one group
example:
nodes 1,2,3,4,5
edges 12,14,53
table: 1,2,3,4,5
step 3. edge 12 table (after) : 1,1,3,4,5
step 3. edge 14 table (after) : 1,1,3,1,5
step 3. edge 53 table (after) : 1,1,3,1,3
and so on
![:)](./images/smilies/icon_smile.gif)
time complexity is O(E*V) and is better than using adjacency matrix which is typical O(V^3)
Best regards
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)