Can anyone give an optimal approach to solve the below problem which had been asked in hacker earth hiring challenge?
Problem statement -
You have been given a tree having all nodes colored with either black or white color. You have to count the number of pairs of only black nodes which are having at least one white node in between their path.
Remove the white nodes, for the remaining components add sz_i \choose 2, where sz_i denotes size of i^{th} component. This gives you number of paths with only black nodes. Subtract this value from b \choose 2, where b is number of black nodes.
By size of the component, he means the size of connected component. After removing the white nodes, the tree might not be connected and hence would take the form of a graph with several connected components. Just google connected components in a graph for further details. You can use simple dfs to find the size of connected components.