solve graph problem. test on sample.in