Some graph code from another project of mine along with handling the input format specified by the problem