code: public static boolean containsCycle(int[] parents) {
          for (int i=0; i<parents.length; i++) {
              int numAncestors = 0;
              int parent = parents[i];
              while(parent != ParentsArray.WALL_POSITION) {
                  numAncestors += 1;
                  if (numAncestors > parents.length - 1) {
                      return true;
                  }
                  parent = parents[parent];
              }
          }
          return false;
      }
docs: Checks if a dependency tree represented as a parents array contains a cycle.
  
  @param parents
  A parents array where parents[i] contains the index of the
  parent of the word at position i, with parents[i] = -1
  indicating that the parent of word i is the wall node.
  @return True if the tree specified by the parents array contains a cycle,
  False otherwise.