almost working code fails for test case graph.in