CP-templates

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Misuki743/CP-templates

:warning: graph/BFS.cpp

Code

array<vector<int>, 2> BFS(vector<vector<int>> &g, int s) {
  int n = ssize(g);
  vector<int> dis(n, -1), pre(n, -1);
  queue<int> q;

  dis[s] = 0;
  q.push(s);

  while(!q.empty()) {
    int v = q.front(); q.pop();
    for(int x : g[v]) {
      if (dis[x] != -1) continue;
      dis[x] = dis[v] + 1, pre[x] = v;
      q.push(x);
    }
  }

  return {dis, pre};
}
#line 1 "graph/BFS.cpp"
array<vector<int>, 2> BFS(vector<vector<int>> &g, int s) {
  int n = ssize(g);
  vector<int> dis(n, -1), pre(n, -1);
  queue<int> q;

  dis[s] = 0;
  q.push(s);

  while(!q.empty()) {
    int v = q.front(); q.pop();
    for(int x : g[v]) {
      if (dis[x] != -1) continue;
      dis[x] = dis[v] + 1, pre[x] = v;
      q.push(x);
    }
  }

  return {dis, pre};
}
Back to top page