Checking whether a graph is a tree