O N solution for connected graph