Adding code for perfect matching