alexjercan/bugnet huggingface dataset:
Problem p02240 (C++) — buggy submission, verdict: Runtime Error

#include <cstdint>
#include <vector>

using namespace std;

#define GRAPH_SIZE 100000

enum state_t {
  WHITE,
  GRAY,
  BLACK,
};

struct vertex_t {
  uint32_t color;
  vector<uint32_t> connections;
};

static vertex_t graph[GRAPH_SIZE];
static state_t state[GRAPH_SIZE];

static void colorize(uint32_t index, uint32_t color) {
  if (state[index] == WHITE) {
    state[index] = GRAY;
    graph[index].color = color;
    for (uint32_t i = 0; i < graph[index].connections.size(); i++) {
      colorize(graph[index].connections[i], color);
    }
    state[index] = BLACK;
  }
}

void check_connection(size_t num) {
  for (uint32_t i = 0; i < num; i++) {
    state[i] = WHITE;
  }

  uint32_t color = 1;
  for (uint32_t i = 0; i < num; i++) {
    colorize(i, color);
    color++;
  }
}

#include <iostream>

int32_t main() {
  uint32_t n, m;
  cin >> n >> m;

  for (uint32_t i = 0; i < m; i++) {
    uint32_t index, to;
    cin >> index >> to;
    graph[index].connections.push_back(to);
    graph[to].connections.push_back(index);
  }

  check_connection(n);

  uint32_t qs;
  cin >> qs;

  for (uint32_t i = 0; i < qs; i++) {
    uint32_t from, to;
    cin >> from >> to;

    if (graph[from].color == graph[to].color) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}

Error: -11

Fixed submission:

#include <cstdint>
#include <vector>

using namespace std;

#define GRAPH_SIZE 100000

enum state_t {
  WHITE,
  GRAY,
  BLACK,
};

struct vertex_t {
  uint32_t color;
  vector<uint32_t> connections;
};

static vertex_t graph[GRAPH_SIZE];
static state_t state[GRAPH_SIZE];

static void colorize(uint32_t index, uint32_t color) {
  if (state[index] == WHITE) {
    state[index] = GRAY;
    graph[index].color = color;
    for (uint32_t i = 0; i < graph[i].connections.size(); i++) {
      colorize(graph[i].connections[i], color);
    }
    state[index] = BLACK;
  }
}

void check_connection() {
  for (uint32_t i = 0; i < GRAPH_SIZE; i++) {
    state[i] = WHITE;
  }

  uint32_t color = 0;
  for (uint32_t i = 0; GRAPH_SIZE; i++) {
    colorize(i, color);
    color++;
  }
}

#include <iostream>

int32_t main() {
  uint32_t n, m;
  cin >> n >> m;

  for (uint32_t i = 0; i < m; i++) {
    uint32_t index, to;
    cin >> index >> to;
    graph[index].connections.push_back(to);
  }

  check_connection();

  uint32_t qs;
  cin >> qs;

  for (uint32_t i = 0; i < qs; i++) {
    uint32_t from, to;
    cin >> from >> to;

    if (graph[from].color == graph[to].color) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

class UnionFindTree {
private:
  vector<int32_t> parent, rank;

  int32_t find(int32_t x) {
    if (parent[x] == x) {
      return x;
    } else {
      int32_t p = find(parent[x]);
      parent[x] = p;
      return p;
    }
  }

public:
  UnionFindTree(int32_t n) {
    parent.resize(n);
    rank.resize(n);

    for (int32_t i = 0; i < n; i++) {
      parent[i] = i;
      rank[i] = 0;
    }
  }

  void unite(int32_t x, int32_t y) {
    x = find(x);
    y = find(y);

    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      parent[x] = y;
    } else {
      parent[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int32_t x, int32_t y) { return find(x) == find(y); }
};

int main() {
  int32_t n, m;
  cin >> n >> m;

  UnionFindTree uf(n);

  for (int32_t i = 0; i < m; i++) {
    int32_t s, t;
    cin >> s >> t;

    uf.unite(s, t);
  }

  int32_t q;
  cin >> q;

  for (int32_t i = 0; i < q; i++) {
    int32_t s, t;
    cin >> s >> t;

    cout << (uf.same(s, t) ? "yes" : "no") << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <unordered_set>
#include <vector>

using namespace std;

const int N_MAX = 100000;
vector<vector<int32_t>> A(N_MAX);
vector<bool> visited(N_MAX);
vector<int32_t> deadend;

void dfs1(int32_t u) {
  visited[u] = true;

  for (int32_t v : A[u]) {
    if (!visited[v]) {
      dfs1(v);
    }
  }

  deadend.push_back(u);

  return;
}

unordered_set<int32_t> dfs2(int32_t u, unordered_set<int32_t> &scc) {
  visited[u] = true;

  for (int32_t v : A[u]) {
    if (!visited[v]) {
      dfs2(v, scc);
    }
  }

  scc.insert(u);

  return scc;
}

int main() {
  fill(visited.begin(), visited.end(), false);

  int32_t n, m;
  cin >> n >> m;

  for (int32_t i = 0; i < m; i++) {
    int32_t s, t;
    cin >> s >> t;

    A[s].push_back(t);
    A[t].push_back(s);
  }

  for (int32_t i = 0; i < n; i++) {
    if (!visited[i]) {
      dfs1(i);
    }
  }

  reverse(deadend.begin(), deadend.end());
  fill(visited.begin(), visited.end(), false);
  vector<unordered_set<int32_t>> sccs;

  for (int32_t u : deadend) {
    if (!visited[u]) {
      unordered_set<int32_t> scc;
      dfs2(u, scc);
      sccs.push_back(scc);
    }
  }

  int32_t q;
  cin >> q;

  for (int32_t i = 0; i < q; i++) {
    int32_t s, t;
    cin >> s >> t;

    bool flag = false;
    for (auto scc : sccs) {
      if (scc.find(s) != scc.end() && scc.find(t) != scc.end()) {
        flag = true;
        break;
      }
    }

    cout << (flag ? "yes" : "no") << endl;
  }

  return 0;
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int uni[100000];

int oya(int a) {
  if (uni[a] < 0) {
    return a;
  } else {
    uni[a] = oya(uni[a]);
    return oya(uni[a]);
  }
}

bool ren(int a, int b) {
  a = oya(a);
  b = oya(b);
  if (a == b) {
    return false;
  } else {

    return true;
  }
}

int main() {

  int n, m;
  cin >> n >> m;
  for (int y = 0; y < n; y++) {
    uni[y] = -1;
  }

  for (int r = 0; r < m; r++) {
    int s, g;
    cin >> s >> g;
    if (ren(s, g)) {
      s = oya(s);
      uni[s] = g;
    }
  }
  int q;
  cin >> q;
  for (int rr = 0; rr < q; rr++) {
    int s, g;
    cin >> s >> g;
    if (ren(s, g) == true) {
      cout << "no" << endl;
    } else {
      cout << "yes" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int d[100100];
  int m, n, q;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    d[i] = i;
  }
  for (int l = 0; l < m; l++) {
    int a, b;
    cin >> a >> b;
    int j, h;
    j = a, h = b;
    while (d[j] != j && d[h] != h) {
      j = d[j];
      h = d[h];
    }
    if (d[j] == j) {
      while (d[h] != h) {
        h = d[h];
      }
      d[j] = h;
      d[a] = h;
    } else {
      while (d[j] != j) {
        j = d[j];
      }
      d[h] = j;
      d[b] = j;
    }
  }
  cin >> q;
  for (int y = 0; y < q; y++) {
    int a, b;
    cin >> a >> b;
    int j, h;
    j = a, h = b;
    while (d[j] != j || d[h] != h) {
      j = d[j];
      h = d[h];
    }
    if (j == h) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}
Problem p02240 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define MAX_N 500000
vector<int> X[MAX_N];
queue<int> Q;
int COLOR[MAX_N], N, M, Q1, A, B;
int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> A >> B;
    X[A].push_back(B);
    X[B].push_back(A);
  }
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (COLOR[i] == 0) {
      Q.push(i);
      cnt++;
      COLOR[i] = cnt;
      while (!Q.empty()) {
        int a = Q.front();
        Q.pop();
        for (int j = 0; j < X[a].size(); j++) {
          int to = X[a][j];
          if (COLOR[to] == 0) {
            COLOR[to] = cnt;
            Q.push(to);
          }
        }
      }
    }
  }
  cin >> Q1;
  for (int i = 0; i < Q1; i++) {
    cin >> A >> B;
    if (COLOR[A] == COLOR[B]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
queue<int> Q;
int x[200000], n, m, q, s, t;
vector<int> a[100000];

bool bfs(int A, int B) {
  memset(x, 0, sizeof(x));
  x[A] = 1;
  Q.push(A);
  while (!Q.empty() || x[B] == 0) {
    for (int k = 0; k < a[Q.front()].size(); k++) {
      if (x[a[Q.front()][k]] == 0) {
        Q.push(a[Q.front()][k]);
        x[a[Q.front()][k]] = 1;
        a[Q.front()].push_back(a[Q.front()][k]);
      }
    }
    Q.pop();
  }
  if (x[B] == 1) {
    return true;
  } else {
    return false;
  }
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> s >> t;
    a[s].push_back(t);
    a[t].push_back(s);
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> s >> t;
    if (bfs(s, t)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

struct node {
  int id;
  vector<int> rel;
};

node *nodes;

void dfs(int start, int cur, int *h) {
  if (h[cur] == -1) {
    h[cur] = start;
    for (int i = 0; i < nodes[cur].rel.size(); i++) {
      dfs(start, nodes[cur].rel[i], h);
    }
  }
}

int main() {
  int n, m, q;
  scanf("%d %d", &n, &m);

  nodes = new node[n];
  for (int i = 0; i < n; i++) {
    nodes[i].id = i;
  }
  while (m--) {
    int l, r;
    scanf("%d %d", &l, &r);
    nodes[l].rel.push_back(r);
    nodes[r].rel.push_back(l);
  }

  int *h = new int[n];
  for (int i = 0; i < n; i++) {
    h[i] = -1;
  }
  for (int i = 0; i < n; i++) {
    dfs(i, i, h);
  }

  scanf("%d", &q);
  while (q--) {
    int l, r;
    scanf("%d %d", &l, &r);
    printf("%s\\n", h[l] == h[r] ? "yes" : "no");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

struct node {
  int id;
  vector<int> rel;
};

int n, m, q;
node *nodes;

/* DFS * TLE! *
bool canReach(int s, int d, bool* h){
        h[s] = true;
        for(int i = 0; i < nodes[s].rel.size(); i++){
                if(nodes[s].rel[i] == d) return true;
                else if(!h[nodes[s].rel[i]]){
                        if(canReach(nodes[s].rel[i], d, h)) return true;
                }
        }
        return false;
}
*/

int main() {
  scanf("%d %d", &n, &m);

  nodes = new node[n];
  for (int i = 0; i < n; i++) {
    nodes[i].id = i;
  }
  while (m--) {
    int l, r;
    scanf("%d %d", &l, &r);
    nodes[l].rel.push_back(r);
    nodes[r].rel.push_back(l);
  }

  scanf("%d", &q);
  while (q--) {
    int l, r;
    scanf("%d %d", &l, &r);

    bool *h = new bool[n];
    for (int i = 0; i < n; i++) {
      h[i] = false;
    }

    queue<int> qu;
    qu.push(l);
    while (qu.size()) {
      h[qu.front()] = true;
      for (int i = 0; i < nodes[qu.front()].rel.size(); i++) {
        if (nodes[qu.front()].rel[i] == r) {
          puts("yes");
          goto BREAK;
        } else if (!h[nodes[qu.front()].rel[i]]) {
          qu.push(nodes[qu.front()].rel[i]);
        }
      }
      qu.pop();
    }
    puts("no");
  BREAK:;
    delete[] h;
  }
  return 0;
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define MAX 100000
#define INFTY 1 << 30 // 2^30
using namespace std;
typedef long long llong;

static const int NIL = -1;

int n;
vector<int> G[MAX];
int color[MAX];

void dfs(int r, int c) {
  stack<int> S;
  S.push(r);
  color[r] = c;
  while (!S.empty()) {
    int u = S.top();
    S.pop();
    for (int i = 0; i < G[u].size(); i++) {
      int v = G[u][i];
      if (color[v] == NIL) {
        color[v] = c;
        S.push(v);
      }
    }
  }
}

void assingColor() {
  int id = 1;
  for (int i = 0; i < n; i++)
    color[i] = NIL;
  for (int u = 0; u < n; u++)
    if (color[u] == NIL)
      dfs(u, id++);
}

int main() {
  int s, t, m, q;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }

  assingColor();

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> s >> t;
    cout << ((color[s] == color[t]) ? "yes" : "no") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int *passed;

bool do_check(int s, int t, vector<vector<int>> a) {
  queue<int> q;

  bool flag = false;
  if (s == t) {
    flag = true;
  }
  q.push(s);
  while (q.size() > 0 && flag == false) {
    s = q.front();
    q.pop();
    for (int i = 0; i < a[s].size(); i++) {
      int tmp = a[s].at(i);
      if (passed[tmp] == 0) {
        passed[tmp] = 1;
        q.push(tmp);
        if (tmp == t) {
          flag = true;
          break;
        }
      }
    }
  }
  return flag;
}

int main() {
  int n, m;
  cin >> n >> m;
  passed = new int[n];
  vector<vector<int>> a;

  for (int i = 0; i < n; i++) {
    vector<int> tmp;
    a.push_back(tmp);
  }

  for (int i = 0; i < m; i++) {
    int tmpa, tmp;
    cin >> tmpa >> tmp;
    a[tmpa].push_back(tmp);
    a[tmp].push_back(tmpa);
  }

  // Questions
  int q_num;
  cin >> q_num;
  // cout << "q_num: " << q_num << endl;
  for (int i = 0; i < q_num; i++) {
    for (int j = 0; j < n; j++) {
      passed[j] = 0;
    }
    int s, t;
    cin >> s >> t;

    // cout << s << ": " << do_check(s, t, a) << endl;
    if (do_check(s, t, a)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int n;
void make_group(vector<int> &group, vector<vector<int>> &g) {
  bool visited[n];
  for (int i = 0; i < n; i++)
    visited[i] = false;
  queue<int> que;
  int count = 1;
  for (int s = 0; s < n; s++) {
    if (!visited[s]) {
      visited[s] = true;
      que.push(s);
      while (!que.empty()) {
        int v = que.front();
        que.pop();
        group[v] = count;
        for (int i = 0; i < g[v].size(); i++) {
          if (!visited[g[v][i]]) {
            visited[g[v][i]] = true;
            que.push(g[v][i]);
          }
        }
      }
    }
    count++;
  }
}

int main() {
  int m, s, t, q;
  scanf("%d %d\\n", &n, &m);
  vector<vector<int>> g(n);
  vector<int> group(n, 0);
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &s, &t);
    g[s].push_back(t);
    g[t].push_back(s);
  }

  make_group(group, g);
  // cout << group << endl;
  /*
  for(int i=0;i<n;i++) {
    printf("%d:%d\\n",i,group[i]);
  }
  */

  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &s, &t);
    if (group[s] == group[t]) {
      printf("yes\\n");
    } else {
      printf("no\\n");
    }
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define MAX_N 100000
int n;

bool can_connect(int s, int t, const vector<vector<int>> &g) {
  bool visited[n];
  queue<int> que;
  for (int i = 0; i < n; i++)
    visited[i] = false;
  while (!que.empty())
    que.pop();

  visited[s] = true;
  que.push(s);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int i = 0; i < g[v].size(); i++) {
      if (!visited[g[v][i]]) {
        if (g[v][i] == t)
          return true;
        visited[g[v][i]] = true;
        que.push(g[v][i]);
      }
    }
  }
  return false;
}

int main() {
  int m, s, t, q;
  scanf("%d %d\\n", &n, &m);
  vector<vector<int>> g(n);
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &s, &t);
    g[s].push_back(t);
    g[t].push_back(s);
  }
  /*
  for(int i=0;i<g.size();i++) {
    printf("%d : ",i);
    for(int j=0;j<g[i].size();j++) {
      printf("%d ",g[i][j]);
    }
    printf("\\n");
  }
  */

  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &s, &t);
    // printf("%d,%d: ",s,t);
    if (can_connect(s, t, g)) {
      printf("yes\\n");
    } else {
      printf("no\\n");
    }
  }
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<list<int>> v;
vector<bool> used;
int p[100000];
int r[100000];

int find(int x) {
  if (p[x] == x)
    return x;
  else
    return p[x] = find(p[x]);
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (r[x] < r[y])
    p[x] = y;
  else {
    p[y] = x;
    if (r[x] == r[y])
      r[x]++;
  }
}

bool same(int x, int y) { return find(x) == find(y); }

void dfs(int pos, int bgn) {
  if (used[pos])
    return;
  used[pos] = true;
  unite(pos, bgn);
  for (list<int>::iterator it = v[pos].begin(); it != v[pos].end(); it++) {
    dfs(*it, bgn);
  }
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> m;
  v = vector<list<int>>(n, list<int>(0));
  used = vector<bool>(n, false);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }

  for (int i = 0; i < n; i++) {
    p[i] = i;
    r[i] = 0;
  }

  for (int i = 0; i < n; i++) {
    dfs(i, i);
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    if (same(a, b))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<list<int>> v;
vector<bool> used;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> m;
  v = vector<list<int>>(n, list<int>(0));
  used = vector<bool>(n, false);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    used = vector<bool>(n, false);
    int a, b;
    cin >> a >> b;
    bool flg = false;
    queue<int> que;
    que.push(a);
    while (!que.empty()) {
      int q = que.front();
      que.pop();
      for (list<int>::iterator it = v[q].begin(); it != v[q].end(); it++) {
        if (*it == b) {
          flg = true;
          break;
        }
        if (used[*it])
          continue;
        used[*it] = true;
        que.push(*it);
      }
      if (flg)
        break;
    }
    if (flg)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}
Problem p02240 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

static const int N = 100001;

int n, m;
int color[N], dist;

vector<int> G[N];

void dfs(int i, int c) {
  queue<int> q;
  q.push(i);
  color[i] = c;

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < G[u].size(); ++i) {
      int v = G[u][i];
      if (-1 == color[v]) {
        color[v] = c;
        q.push(v);
      }
    }
  }
}

void search() {
  for (int i = 0; i < n; ++i)
    color[i] = -1;

  int c = 0;
  for (int i = 0; i < n; ++i) {
    if (color[i] == -1)
      dfs(i, c++);
  }
}

int main() {
  cin >> n >> m;

  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }

  search();

  cin >> n;
  for (int i = 0; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    if (color[u] == color[v])
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

static const int N = 100;

int n, m;
int color[N], dist;

vector<int> G[N];

void dfs(int i, int c) {
  queue<int> q;
  q.push(i);
  color[i] = c;

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < G[u].size(); ++i) {
      int v = G[u][i];
      if (-1 == color[v]) {
        color[v] = c;
        q.push(v);
      }
    }
  }
}

void search() {
  for (int i = 0; i < n; ++i)
    color[i] = -1;

  int c = 0;
  for (int i = 0; i < n; ++i) {
    if (color[i] == -1)
      dfs(i, c++);
  }
}

int main() {
  cin >> n >> m;

  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }

  search();

  cin >> n;
  for (int i = 0; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    if (color[u] == color[v])
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }

  return 0;
}
Problem p02240 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

typedef long long ll;

using namespace std;

static const int MAX = 100010;
static const int NIL = -1;

int n;
vector<int> G[MAX];
int color[MAX];
int id;

void dfs(int r, int c) {
  stack<int> s;
  s.push(r);
  color[r] = c;
  while (!s.empty()) {
    int t = s.top();
    s.pop();
    for (int i = 0; i < G[t].size(); i++) {
      int k = G[t][i];
      if (color[k] == NIL) {
        color[k] = c;
        s.push(k);
      }
    }
  }
}

void assign() {
  id = 1;
  memset(color, NIL, sizeof(color));
  for (int i = 0; i < n; i++) {
    if (color[i] == NIL)
      dfs(i, id++);
  }
}

int main() {
  ios::sync_with_stdio(false);
  int s, t, m, q;
  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }

  assign();

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> s >> t;
    if (color[s] == color[t]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()


Fixed submission:

#include <cstdio>
#include <iostream>
#include <vector>
#define DFS_WHITE -1
#define DFS_BLACK 1
using namespace std;
typedef vector<int> vi;

vector<vi> graph;
vi dfs_num;

void readGraph() {
  string edge;
  int node1, node2;
  getline(cin, edge);

  while (getline(cin, edge)) {
    if (edge.size() == 0)
      break;
    node1 = edge[0] - 65;
    node2 = edge[1] - 65;
    graph[node1].push_back(node2);
    graph[node2].push_back(node1);
  }
}

void print() {
  for (int x = 0; x < graph.size(); x++) {
    cout << "Nodo : " << x << endl;
    for (int y = 0; y < graph[x].size(); y++)
      cout << graph[x][y] << " ";
    cout << endl;
  }
}

void dfs(int node) {
  dfs_num[node] = DFS_BLACK;
  for (int c = 0; c < graph[node].size(); c++)
    if (dfs_num[graph[node][c]] == DFS_WHITE)
      dfs(graph[node][c]);
}

int main() {
  string nodes;
  int casos, CC, flag = 0;
  scanf("%d", &casos);
  for (int x = 0; x < casos; x++) {
    cin >> nodes;
    graph.assign(nodes[0] - 64, vi());
    dfs_num.assign(nodes[0] - 64, DFS_WHITE);
    readGraph();
    // print();
    CC = 0;
    for (int z = 0; z < dfs_num.size(); z++) {
      if (dfs_num[z] == DFS_WHITE) {
        dfs(z);
        CC++;
      }
    }
    if (flag == 0)
      flag++;
    else
      printf("\\n");
    printf("%d\\n", CC);
  }
  return 0;
}