Solution to the Dominator problem in C++