Improved implementation of undirected graph a bit (slightly faster).