private boolean dfs(Digraph G, int v) {
        onStack[v] = true;
        marked[v] = true;
        boolean result = true;
        int adj = 0;
        for (int w : G.adj(v)) {
            adj++;
            if (onStack[w]) {
                result = false;
            }
            if (!marked[w]) {
                result = dfs(G, w);
            }
        }
        onStack[v] = false;
        // if v has no neighbours, update count.
        // if updated count > 1, there are more than one roots. return false
        if (adj == 0 && ++count > 1) {
            return false;
        }
        return result;
    }