alexjercan/bugnet huggingface dataset:
Problem p02240 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> groups(N, -1);
  vector<vector<int>> friends(N);
  for (int i = 0; i < M; i++) {
    int s, t;
    cin >> s >> t;
    friends[s].push_back(t);
    friends[t].push_back(s);
  }
  int group = 0;
  for (int i = 0; i < N; i++) {
    if (groups[i] != -1)
      continue;
    queue<int> cycle;
    cycle.push(i);
    while (cycle.size() != 0) {
      int now = cycle.front();
      cycle.pop();
      vector<int>::iterator itr;
      for (itr = friends[now].begin(); itr != friends[now].end(); ++itr) {
        if (groups[*itr] == -1)
          cycle.push(*itr);
      }
      groups[now] = group;
    }
    group++;
  }
  cin >> N;
  for (int i = 0; i < N; i++) {
    int s, t;
    cin >> s >> t;
    if (groups[s] == groups[t])
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}


Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> friends(N, vector<int>(N));
  vector<int> groups(N, -1);
  for (int i = 0; i < M; i++) {
    int s, t;
    cin >> s >> t;
    friends[s][t] = friends[t][s] = 1;
  }
  int group = 0;
  for (int i = 0; i < N; i++) {
    if (groups[i] != -1)
      continue;
    queue<int> cycle;
    cycle.push(i);
    while (cycle.size() != 0) {
      int now = cycle.front();
      cycle.pop();
      for (int l = 0; l < N; l++) {
        if (friends[now][l] && groups[l] == -1)
          cycle.push(l);
      }
      groups[now] = group;
    }
    group++;
  }
  cin >> N;
  for (int i = 0; i < N; i++) {
    int s, t;
    cin >> s >> t;
    if (groups[s] == groups[t])
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

const int N = 100000;
int n;
// int color[N];
int group[N];
vector<int> v[N];

void setColor(int r, int c) {
  queue<int> q;
  q.push(r);
  group[r] = c;

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

void assignColor() {
  int id = 1;
  for (int i = 0; i < n; i++)
    group[i] = -1;
  for (int u = 0; u < n; u++) {
    if (group[u] == -1)
      setColor(u, id++);
  }
}

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

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


Error: TLE

Fixed submission:

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

using namespace std;

const int N = 100000;
int n;
int color[N];
vector<int> v[N];

bool isFriend(int s, int t) {
  for (int i = 0; i < n; i++)
    color[i] = 0;
  queue<int> q;
  int x;
  q.push(s);
  while (!q.empty()) {
    x = q.front();
    q.pop();
    for (int i = 0; i < v[x].size(); i++) {
      int y = v[x][i];
      if (y == t)
        return true;
      if (color[y] == 0) {
        q.push(y);
        color[y] = 1;
      }
    }
  }
  return false;
}

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

// ALDS1_11_D.cpp
// Graph I - Connected Components

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

void bfs(vector<vector<int>> &sns, vector<int> &color, int x) {
  if (color[x] != -1 || sns[x].empty())
    return;
  queue<int> mq;
  mq.push(x);
  while (!mq.empty()) {
    int curr = mq.front();
    mq.pop();
    color[curr] = x;
    for (int i = 0; i < sns[curr].size(); i++) {
      int next = sns[curr][i];
      if (color[next] == -1) {
        mq.push(next);
        color[next] = x;
      }
    }
  }
}

void assignColor(vector<vector<int>> &sns, vector<int> &color) {
  for (int i = 0; i < sns.size(); i++) {
    bfs(sns, color, i);
  }
}

int main() {
  //    freopen("ALDS1_11_D_in22.txt", "r", stdin);
  //    freopen("out22.txt", "w", stdout);
  int m, n, q;
  cin >> n >> m;
  vector<vector<int>> sns(n, vector<int>());
  int x, y;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    sns[x].push_back(y);
    sns[y].push_back(x);
  }
  vector<int> color(n, -1);
  assignColor(sns, color);
  cin >> q;
  while (q--) {
    cin >> x >> y;
    if (color[x] == color[y] && color[x] != -1)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// ALDS1_11_D.cpp
// Graph I - Connected Components

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

void bfs(vector<vector<int>> &sns, vector<int> &arrived, int x, int y) {
  if (arrived[y] != 0)
    return;
  queue<int> mq;
  mq.push(x);
  while (!mq.empty()) {
    int curr = mq.front();
    mq.pop();
    arrived[curr] = 2;
    for (int i = 0; i < sns[curr].size(); i++) {
      int next = sns[curr][i];
      if (arrived[next] == 0) {
        mq.push(next);
        arrived[next] = 1;
      }
    }
  }
}

int main() {
  //    freopen("ALDS1_11_D_in22.txt", "r", stdin);
  //    freopen("out22.txt", "w", stdout);
  int m, n, q;
  cin >> n >> m;
  vector<vector<int>> sns(n, vector<int>());
  int x, y;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    sns[x].push_back(y);
    sns[y].push_back(x);
  }

  cin >> q;
  while (q--) {
    vector<int> arrived(n);
    cin >> x >> y;
    bfs(sns, arrived, x, y);
    if (arrived[y])
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}
Problem p02240 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <vector>
using namespace std;

vector<vector<int>> g;
int n, m, q;

vector<bool> visited;
vector<int> color;
void dfs(int s) {
  for (int i = 0; i < g[s].size(); i++) {
    if (color[g[s][i]] == -1) {
      color[g[s][i]] = color[s];
      dfs(g[s][i]);
    }
  }
}
int main() {
  cin >> n >> m;
  g.resize(n);
  color.resize(n, -1);
  for (int i = 0; i < m; i++) {
    int s, t;
    cin >> s >> t;
    g[s].push_back(t);
    g[t].push_back(s);
  }
  for (int i = 0; i < n; i++) {
    if (color[i] == -1) {
      color[i] = i;
      dfs(i);
    }
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int s, t;
    cin >> s >> t;
    cout << (color[s] == color[t] ? "yes" : "no") << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <vector>
using namespace std;

vector<vector<int>> g;
int n, m, q;

vector<bool> visited;
bool dfs(int s, int t) {
  if (s == t)
    return true;
  visited[s] = true;
  for (int i = 0; i < g[s].size(); i++) {
    if (!visited[g[s][i]]) {
      if (dfs(g[s][i], t)) {
        return true;
      }
    }
  }
  return false;
}
int main() {
  cin >> n >> m;
  g.resize(n);

  for (int i = 0; i < m; i++) {
    int s, t;
    cin >> s >> t;
    g[s].push_back(t);
    g[t].push_back(s);
  }
  cin >> q;
  if (q > 5)
    return 1;
  for (int i = 0; i < q; i++) {
    visited.resize(n, false);
    int s, t;
    cin >> s >> t;
    string ret = (dfs(s, t) ? "yes" : "no");
    cout << ret << endl;
  }
  return 0;
}
Problem p02240 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

vector<vector<int>> g;
int n, m, q, s, t;

vector<bool> visited;
vector<int> color;
void dfs(int s) {
  for (int i = 0; i < g[s].size(); i++) {
    if (color[g[s][i]] == -1) {
      color[g[s][i]] = color[s];
      dfs(g[s][i]);
    }
  }
}
bool dfs2(int s, int t) {
  if (s == t)
    return true;
  visited[s] = true;
  for (int i = 0; i < g[s].size(); i++) {
    if (!visited[g[s][i]]) {
      if (dfs2(g[s][i], t)) {
        return true;
      }
    }
  }
  return false;
}
int main() {
  scanf("%d %d", &n, &m);
  g.resize(n);
  color.resize(n, -1);
  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 < n; i++) {
    if (color[i] == -1) {
      color[i] = i;
      dfs(i);
    }
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &s, &t);
    visited.resize(n, false);
    string s1 = (dfs2(s, t) ? "yes" : "no");
    string s2 = (color[s] == color[t] ? "yes" : "no");
    if (s1 != s2 && s1 == "yes")
      return 1;
    printf((color[s] == color[t] ? "yes\\n" : "no\\n"));
  }
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

vector<vector<int>> g;
int n, m, q, s, t;

vector<bool> visited;
vector<int> color;
void dfs(int s) {
  for (int i = 0; i < g[s].size(); i++) {
    if (color[g[s][i]] == -1) {
      color[g[s][i]] = color[s];
      dfs(g[s][i]);
    }
  }
}
bool dfs2(int s, int t) {
  if (s == t)
    return true;
  visited[s] = true;
  for (int i = 0; i < g[s].size(); i++) {
    if (!visited[g[s][i]]) {
      if (dfs2(g[s][i], t)) {
        return true;
      }
    }
  }
  return false;
}
int main() {
  scanf("%d %d", &n, &m);
  g.resize(n);
  color.resize(n, -1);
  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 < n; i++) {
    if (color[i] == -1) {
      color[i] = i;
      dfs(i);
    }
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &s, &t);
    visited.resize(n, false);
    string s1 = (dfs2(s, t) ? "yes" : "no");
    string s2 = (color[s] == color[t] ? "yes" : "no");
    if (s1 != s2)
      return 1;
    printf((color[s] == color[t] ? "yes\\n" : "no\\n"));
  }
  return 0;
}
Problem p02240 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> G(100000);
vector<int> c(100000);
void dfs(int s, int x) {
  if (c[s])
    return;
  c[s] = x;
  for (auto a : G[s])
    dfs(a, x);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  for (int i = 0; i < n; ++i)
    dfs(i, i + 1);
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int a, b;
    cin >> a >> b;
    cout << (c[a] - c[b] ? "no" : "yes") << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> G(100);
vector<int> c(100);
void dfs(int s, int x) {
  if (c[s])
    return;
  c[s] = x;
  for (auto a : G[s])
    dfs(a, x);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  for (int i = 0; i < n; ++i)
    dfs(i, i + 1);
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int a, b;
    cin >> a >> b;
    cout << (c[a] - c[b] ? "no" : "yes") << endl;
  }
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define Vi vector<int>
#define FOR(i, s, e) for (int i = s; i < e; i++)
#define ITER(v) v.begin(), v.end()

constexpr int N_MAX = 100'000;

Vi adj[N_MAX];
int color[N_MAX];

int n;
void build_color() {
  bool isqueued[N_MAX] = {false};
  for (int i = 0, c = 0; i < n; ++i) {
    if (isqueued[i]) {
      continue;
    }
    queue<int> q;
    q.push(i);
    isqueued[i] = true;
    while (!q.empty()) {
      const int j = q.front();
      color[j] = c;
      for (const int k : adj[j]) {
        if (isqueued[k]) {
          continue;
        }
        q.push(k);
        isqueued[k] = true;
      }
      q.pop();
    }
    ++c;
  }
}

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

  int m;
  cin >> n >> m;

  int a, b;
  FOR(i, 0, m) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  /*
  cout << "adj read." << endl;
  FOR(i,0,n) {
      for (const int j : adj[i]) {
          cout << " " << j;
      }
      cout << endl;
  }
  */
  build_color();

  int q;
  cin >> q;
  FOR(i, 0, q) {
    cin >> a >> b;
    cout << (color[a] == color[b] ? "yes" : "no") << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define Vi vector<int>
#define FOR(i, s, e) for (int i = s; i < e; i++)
#define ITER(v) v.begin(), v.end()

constexpr int N_MAX = 100'000;

Vi adj[N_MAX];

int n;
bool isconnected(const int a, const int b) {
  bool isqueued[N_MAX] = {false};
  queue<int> q;
  q.push(a);
  isqueued[a] = true;
  while (!q.empty()) {
    const int i = q.front();
    // cout << "#" << i << endl;
    if (i == b) {
      return true;
    }
    for (const int j : adj[i]) {
      if (isqueued[j]) {
        continue;
      }
      q.push(j);
      isqueued[j] = true;
    }
    q.pop();
  }
  return false;
}

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

  int m;
  cin >> n >> m;

  int a, b;
  FOR(i, 0, m) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  /*
  cout << "adj read." << endl;
  FOR(i,0,n) {
      for (const int j : adj[i]) {
          cout << " " << j;
      }
      cout << endl;
  }
  */

  int q;
  cin >> q;
  FOR(i, 0, q) {
    cin >> a >> b;
    cout << (isconnected(a, b) ? "yes" : "no") << endl;
  }

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

#include <cstdio>
#include <vector>
#define N 100000
using namespace std;

vector<int> G[N];
/*
??£??\\??????????????????????????°???????????¨??????????????£????????????????????????DFS??§????????????

 */
int labels[N], c = 0;

void dfs_init() {
  int i;
  for (i = 0; i < N; i++)
    labels[i] = 0;
}

/*
  ?????°?????¨??????????£?
  ??§???????????????????????????
  stack???????????°??§???????????????
 */
bool dfs(int u, int v) {
  if (u == v)
    return true;
  labels[u] = 1;
  int i, x;
  for (i = 0; i < G[u].size(); i++) {
    x = G[u][i];
    dfs(x, v);
  }
  return false;
}

void dfs2(int u) {
  labels[u] = c;
  int i;
  for (i = 0; i < G[u].size(); i++) {
    if (labels[G[u][i]] == 0)
      dfs2(G[u][i]);
  }
}

int main(void) {
  int n, m, i, q, u, v;
  scanf("%d %d", &n, &m);
  for (i = 0; i < m; i++) {
    scanf("%d %d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs_init();
  /*
    ????????§?????¨???????????????
   */
  for (i = 0; i < n; i++) {
    if (labels[i] == 0) {
      c++;
      dfs2(i);
    }
  }

  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    scanf("%d %d", &u, &v);
    if (labels[u] > 0 && labels[u] == labels[v])
      printf("yes\\n");
    else
      printf("no\\n");
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <vector>
#define N 100000
using namespace std;

vector<int> G[N];
/*
??£??\\??????????????????????????°???????????¨??????????????£????????????????????????DFS??§????????????

 */
int labels[N];

void dfs_init() {
  int i;
  for (i = 0; i < N; i++)
    labels[i] = 0;
}

/*
  ?????°?????¨??????????£?
  ??§???????????????????????????
  stack???????????°??§???????????????
 */
bool dfs(int u, int v) {
  if (u == v)
    return true;
  labels[u] = 1;
  int i, x;
  for (i = 0; i < G[u].size(); i++) {
    x = G[u][i];
    dfs(x, v);
  }
  return false;
}

void dfs2(int u) {
  labels[u] = 1;
  int i;
  for (i = 0; i < G[u].size(); i++) {
    if (labels[G[u][i]] == 0)
      dfs2(G[u][i]);
  }
}

int main(void) {
  int n, m, i, q, u, v;
  scanf("%d %d", &n, &m);
  for (i = 0; i < m; i++) {
    scanf("%d %d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }

  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    scanf("%d %d", &u, &v);
    dfs_init();
    dfs2(u);
    if (labels[u] > 0 && labels[u] == labels[v])
      printf("yes\\n");
    else
      printf("no\\n");
  }

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

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

int main() {
  int n, m, v1, v2;
  scanf("%d%d", &n, &m);
  vector<int> v[100000];

  for (int i = 0; i < m; i++) {
    scanf("%d%d", &v1, &v2);
    v[v1].push_back(v2);
    v[v2].push_back(v1);
  }

  int group[n];
  for (int i = 0; i < n; i++)
    group[i] = -1;

  for (int i = 0, id = 0; i < n; i++, id++) {
    queue<int> que;
    if (group[i] == -1)
      que.push(i);
    while (!que.empty()) {
      int x = que.front();
      que.pop();
      group[x] = id;
      for (int j = 0; j < v[x].size(); j++)
        if (group[v[x][j]] == -1)
          que.push(v[x][j]);
    }
  }

  int q;
  scanf("%d", &q);

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

  return 0;
}

Error: TLE

Fixed submission:

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

struct Edge {
  int from;
  int to;

  friend bool operator==(const Edge &l, const Edge &r) {
    return l.from == r.from && l.to == r.to;
  }
};

int main() {
  int n, m, v1, v2;
  scanf("%d%d", &n, &m);
  vector<Edge> v;

  for (int i = 0; i < m; i++) {
    scanf("%d%d", &v1, &v2);
    v.push_back({v1, v2});
  }

  int group[n];
  for (int i = 0; i < n; i++)
    group[i] = -1;

  int id = 0;
  while (!v.empty()) {
    queue<Edge> q;
    q.push(v.front());
    while (!q.empty()) {
      Edge edge = q.front();
      q.pop();
      group[edge.from] = id;
      group[edge.to] = id;
      v.erase(remove(v.begin(), v.end(), edge), v.end());
      // auto itr = find(v.begin(), v.end(), edge);
      // if (itr != v.end())
      //   v.erase(itr);
      for (auto const &e : v)
        if (e.from == edge.from || e.to == edge.from || e.from == edge.to ||
            e.to == edge.to)
          q.push(e);
    }
    id++;
  }

  int q;
  scanf("%d", &q);

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

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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
const int maxx = 100010;

int pre[maxx];

int find(int u) {
  if (pre[u] != u)
    pre[u] = find(pre[u]);
  return pre[u];
}

int main() {
  int n, m, x, y;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    pre[i] = i;
  while (m--) {
    cin >> x >> y;
    pre[find(x)] = find(y);
  }
  cin >> m;
  while (m--) {
    cin >> x >> y;
    if (find(x) == find(y))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
const int maxx = 100010;

int pre[maxx];

int find(int u) {
  while (pre[u] != u)
    u = find(pre[u]);
  return u;
}

int main() {
  int n, m, x, y;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    pre[i] = i;
  //	for(int i=0;i<n;i++) cout<<i<<" ";cout<<endl;
  //	for(int i=0;i<n;i++) cout<<pre[i]<<" ";cout<<endl;
  while (m--) {
    cin >> x >> y;
    pre[find(x)] = find(y);
    //		for(int i=0;i<n;i++) cout<<i<<" ";cout<<endl;
    //		for(int i=0;i<n;i++) cout<<pre[i]<<" ";cout<<endl;
  }
  cin >> m;
  while (m--) {
    cin >> x >> y;
    if (find(x) == find(y))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}
Problem p02240 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#define Lens 100005
#define Nil -1

using namespace std;
int n, m, q;
queue<int> Q;
vector<int> A[Lens];
int c[Lens], d[Lens];

void bns(int k, int v) {
  Q.push(k);
  c[k] = v;
  while (!Q.empty()) {
    k = Q.front();
    Q.pop();
    c[k] = v;
    for (int i = 0; i < A[k].size(); i++) {
      int tmp = A[k][i];
      if (c[tmp] == Nil) {
        Q.push(tmp);
      }
    }
  }
}

int main() {
  int v = 1;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int s, t;
    cin >> s >> t;
    c[s] = c[t] = Nil;
    A[s].push_back(t);
    A[t].push_back(s);
  }
  for (int i = 0; i < n; i++) {
    if (c[i] == Nil) {
      bns(i, v);
      v++;
    }
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int s, t;
    cin >> s >> t;

    if (c[s] == c[t] && c[s] != Nil && c[s] != 0)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#define Lens 1005
#define Nil -1

using namespace std;
int n, m, q;
queue<int> Q;
vector<int> A[Lens];
int c[Lens], d[Lens];
int way[Lens][Lens];

void bns(int k, int v) {
  Q.push(k);
  c[k] = v;
  while (!Q.empty()) {
    k = Q.front();
    Q.pop();
    c[k] = v;
    for (int i = 0; i < A[k].size(); i++) {
      int tmp = A[k][i];
      if (c[tmp] == Nil) {
        Q.push(tmp);
      }
    }
  }
}

int main() {
  int v = 1;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int s, t;
    cin >> s >> t;
    c[s] = c[t] = Nil;
    A[s].push_back(t);
    A[t].push_back(s);
  }
  for (int i = 0; i < n; i++) {
    if (c[i] == Nil) {
      bns(i, v);
      v++;
    }
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int s, t;
    cin >> s >> t;

    if (c[s] == c[t] && c[s] != Nil && c[s] != 0)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}