Your problem is that you are entering the vertices in 1 based format, but all your arrays (such as parent) are in 0 based format. When you try to get parent[4] bad things happen.
Probably the easiest fix is to subtract 1 from the head and tail when they are entered, then add 1 again when printing the results out.