private boolean isRootedDAG(Digraph G) {
        marked = new boolean[G.V()];
        onStack = new boolean[G.V()];
        for (int v = 0; v < G.V(); v++) {
            if (!marked[v]) {
                if (!dfs(G, v)) {
                    return false;
                }
            }
        }
        return true;
    }