code: private void bfs(int v, int[] cc, int id) {
          cc[v] = id;
          Queue<Integer> queue = new LinkedList<>();
          queue.offer(v);
          while (!queue.isEmpty()) {
              int t = queue.poll();
              for (Edge edge : graph[t]) {
                  int i = edge.v2;
                  if (!digraph && i == t) {
                      i = edge.v1;
                  }
  
                  if (cc[i] == -1) {
                      queue.offer(i);
                      cc[i] = id;
                  }
              }
          }
      }
docs: Breadth-first search connected components of graph.
  @param v the start vertex.
  @param cc the array to store the connected component id of vertices.
  @param id the current component id.