code: public static boolean isConnectedAndAcyclic(int[] parents) {
          int numVisited = 0;
          // 1-indexed array indicating whether each node (including the wall at position 0) has been visited.
          boolean[] visited = new boolean[parents.length+1];
          Arrays.fill(visited, false);
          // Visit the nodes reachable from the wall in a pre-order traversal.
          IntStack stack = new IntStack();
          stack.push(-1);
          while (stack.size() > 0) {
              // Pop off the current node from the stack.
              int cur = stack.pop();
              if (visited[cur+1] == true) {
                  continue;
              }
              // Mark it as visited.
              visited[cur+1] = true;
              numVisited++;
              // Push the current node's unvisited children onto the stack.
              for (int i=0; i<parents.length; i++) {
                  if (parents[i] == cur && visited[i+1] == false) {
                      stack.push(i);
                  }
              }
          }
          return numVisited == parents.length + 1;
      }
docs: Whether the directed graph (including an implicit wall node) denoted by this parents array is
  fully connected. If a singly-headed directed graph is connected it must also be acyclic.