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

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, m, s, t, q, i, j;
  int id, color = 0;
  bool answer_yes = false;

  // enter
  cin >> n >> m;
  vector<vector<int>> Adj(n);
  vector<int> Stack;
  vector<int> Check(n, 0);
  vector<int>::iterator it, itend;
  for (i = 0; i < m; i++) {
    cin >> s >> t;
    Adj[s].push_back(t);
    Adj[t].push_back(s);
  }
  /*for (i = 0; i < n; i++) {
          cout << "\\n" << i << " : " << flush;
          for (auto it2 = Adj[i].begin(); it2 != Adj[i].end(); it2++) {
                  cout << *it2 << " " << flush;
          }
  }
  cout << endl;*/

  // calculate
  for (i = 0; i < n; i++) {
    if (Check[i] == 0) {
      color++;
      Stack.push_back(i);
      Check[i] = color;
      while (!Stack.empty()) {
        id = Stack.back();
        // cout << "\\nid : " << id << " color : " << color << endl;
        itend = Adj[id].end();
        for (it = Adj[id].begin(); it != itend; it++) {
          if (!Check[*it]) {
            Stack.push_back(*it);
            Check[*it] = color;
            break;
          }
        }
        if (it == itend) {
          Stack.pop_back();
        }
      }
    }
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, m, s, t, q, i, j;
  int id;
  bool answer_yes = false;

  cin >> n >> m;
  vector<vector<int>> Adj(n);
  vector<int> Stack;
  vector<int> Check(n);
  vector<int>::iterator it, itend;

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

  /*for (i = 0; i < n; i++) {
          cout << "\\n" << i << " : " << flush;
          for (auto it2 = Adj[i].begin(); it2 != Adj[i].end(); it2++) {
                  cout << *it2 << " " << flush;
          }
  }
  cout << endl;*/

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

    // calculate
    Stack.push_back(s);
    Check[s] = true;
    while (!Stack.empty()) {
      id = Stack.back();
      // cout << "\\nid : " << id << " stc : " << Stack.size() << endl;
      itend = Adj[id].end();
      for (it = Adj[id].begin(); it != itend; it++) {
        // cout << *it << " " << flush;
        if (!Check[*it]) {
          if (*it == t) {
            answer_yes = true;
            Stack.clear();
            break;
          }
          Stack.push_back(*it);
          Check[*it] = true;
          break;
        }
      }
      if (it == itend) {
        Stack.pop_back();
      }
    }
    if (answer_yes) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
      Stack.clear();
    }
    answer_yes = false;
    itend = Check.end();
    for (it = Check.begin(); it != itend; it++) {
      *it = false;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
static const int MAX = 100000;
static const int NIL = -1;

int n, m;
vector<int> color(MAX, NIL);
vector<vector<int>> M(MAX);

void dfs(int i, int id) {
  int size = M[i].size();
  color[i] = id;
  for (int j = 0; j < size; j++) {
    if (color[M[i][j]] == NIL) {
      color[M[i][j]] = id;
      dfs(M[i][j], id);
    }
  }
}

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

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

  for (int i = 0; i < m; i++) {
    int id, f;
    cin >> id >> f;
    M[id].push_back(f);
    M[f].push_back(id);
  }
  int qn;
  cin >> qn;
  vector<vector<int>> q(qn, vector<int>(2));
  for (int i = 0; i < qn; i++) {
    cin >> q[i][0] >> q[i][1];
  }

  assignColor();

  for (int i = 0; i < qn; i++) {
    if (color[q[i][0]] == color[q[i][1]]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int n, m;

bool frend(vector<vector<int>> &M, vector<int> &memo, int i, int t) {
  if (memo[i] == 1)
    return false;
  memo[i] = 1;
  int cnt = M[i].size();
  for (int z = 0; z < cnt; z++) {
    if (M[i][z] == t)
      return true;
    if (memo[M[i][z]] == 0) {
      if (frend(M, memo, M[i][z], t)) {
        return true;
      }
    }
  }
  return false;
}

int main() {
  cin >> n >> m;
  vector<vector<int>> M(n);

  for (int i = 0; i < m; i++) {
    int id, f;
    cin >> id >> f;
    M[id].push_back(f);
    M[f].push_back(id);
  }
  int qn;
  cin >> qn;
  vector<vector<int>> q(qn, vector<int>(2));
  for (int i = 0; i < qn; i++) {
    cin >> q[i][0] >> q[i][1];
  }

  for (int i = 0; i < qn; i++) {
    vector<int> memo(n);
    bool res = frend(M, memo, q[i][0], q[i][1]);
    if (res) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <iostream>
#include <queue>

using namespace std;

const int MAX = 100000;

int parent(int table[], int child) {
  queue<int> q;

  while (table[child] != -1) {
    q.push(child);
    child = table[child];
  }
  while (!q.empty()) {
    table[q.front()] = child;
    q.pop();
  }
  return child;
}

int main() {
  int n, m, q;
  int table[MAX];

  cin >> n >> m;

  for (int i = 0; i < n; i++)
    table[i] = -1;
  for (int i = 0; i < m; i++) {
    int s, t;
    cin >> s >> t;

    int ps = parent(table, s);
    int pt = parent(table, t);
    if (ps != pt) {
      table[ps] = pt;
    }
  }

  cin >> q;

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

    int ps = parent(table, s);
    int pt = parent(table, t);

    if (ps == pt) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:


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

using namespace std;

const int MAX = 100000;

struct Connection {
  vector<int> c;
};

bool search(Connection cn[], int s, int t) {
  bool check[MAX] = {false};
  queue<int> q;

  q.push(s);
  while (!q.empty()) {
    int u = q.front();
    check[u] = true;
    q.pop();

    if (u == t)
      return true;

    for (int i = 0; i < cn[u].c.size(); i++) {
      if (check[cn[u].c[i]] == false) {
        q.push(cn[u].c[i]);
      }
    }
  }
  return false;
}

int main() {
  int n, m, q;
  Connection cn[MAX];

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

  cin >> q;

  for (int i = 0; i < q; i++) {
    int s, t;

    cin >> s >> t;
    if (search(cn, s, t)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}
Problem p02240 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
using namespace std;

const int MAX = 100000;

int parent(int table[], int child) {
  if (table[child] == -1)
    return child;
  return table[child] = parent(table, table[child]);
}

int main() {
  int n, m, q;
  int table[MAX];

  scanf("%d %d", &n, &m);

  for (int i = 0; i < n; i++)
    table[i] = -1;
  for (int i = 0; i < m; i++) {
    int s, t;
    scanf("%d %d", &s, &t);

    int ps = parent(table, s);
    int pt = parent(table, t);
    if (ps != pt) {
      table[ps] = pt;
    }
  }

  scanf("%d", &q);

  for (int i = 0; i < q; i++) {
    int s, t;

    scanf("%d %d", &s, &t);

    int ps = parent(table, s);
    int pt = parent(table, t);

    if (ps == pt) {
      printf("yes\\n");
    } else {
      printf("no\\n");
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <stdio.h>
using namespace std;

const int MAX = 100000;

int parent(int table[], int child) {
  if (table[child] == -1)
    return child;
  return table[child] = parent(table, table[child]);
}

int main() {
  int n, m, q;
  int table[MAX];

  scanf("%d %d", &n, &m);

  for (int i = 0; i < n; i++)
    table[i] = -1;
  for (int i = 0; i < m; i++) {
    int s, t;
    scanf("%d %d", &s, &t);

    int ps = parent(table, s);
    int pt = parent(table, t);
    if (ps != pt) {
      table[ps] = pt;
    } else {
      table[s] = ps;
      table[t] = pt;
    }
  }

  scanf("%d", &q);

  for (int i = 0; i < q; i++) {
    int s, t;

    scanf("%d %d", &s, &t);

    int ps = parent(table, s);
    int pt = parent(table, t);

    if (ps == pt) {
      printf("yes\\n");
    } else {
      printf("no\\n");
    }
  }
  return 0;
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) (a) > (b) ? (b) : (a)
#define max(a, b) (a) > (b) ? (a) : (b)

using namespace std;

int n, m, q, from, to;
int Rank[100000];
int parent[100000];

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

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

void unite(int x, int 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]++;
  }
}

int main() {

  scanf("%d %d", &n, &m);

  for (int i = 0; i < n; i++) {
    Rank[i] = 0;
    parent[i] = i;
  }

  for (int i = 0; i < m; i++) {
    scanf("%d %d", &from, &to);

    if (isSame(from, to))
      continue;
    unite(from, to);
  }

  scanf("%d", &q);

  int left, right;

  for (int i = 0; i < q; i++) {
    scanf("%d %d", &left, &right);

    if (isSame(left, right))
      printf("yes\\n");
    else {
      printf("no\\n");
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) (a) > (b) ? (b) : (a)
#define max(a, b) (a) > (b) ? (a) : (b)

using namespace std;

int from[100000];
int to[100000];

bool checked[100000];

bool judge(int f, int t, int m) {
  if (checked[f]) {
    return false;
  }

  checked[f] = true;

  bool isFriend = false;
  for (int i = 0; i < m; i++) {
    if (from[i] == f) {
      if (to[i] == t) {
        isFriend = true;
      } else {
        isFriend = judge(to[i], t, m);
      }
      if (isFriend) {
        break;
      }
    }
    if (to[i] == f) {
      if (from[i] == t) {
        isFriend = true;
      } else {
        isFriend = judge(from[i], t, m);
      }
      if (isFriend) {
        break;
      }
    }
  }
  return isFriend;
}

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

  for (int i = 0; i < m; i++) {
    int f, t;
    cin >> f >> t;

    from[i] = f;
    to[i] = t;
  }

  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int f, t;
    cin >> f >> t;

    for (int i = 0; i < n; i++) {
      checked[i] = false;
    }

    printf(judge(f, t, m) ? "yes\\n" : "no\\n");
  }
  return 0;
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
#define MAX 101000

int ran[MAX], par[MAX];

int init(int n) {
  for (int i = 0; i < n; i++) {
    ran[i] = 0;
    par[i] = i;
  }
}

int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); }

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

  if (x == y)
    return;

  if (ran[y] > ran[x]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (ran[y] == ran[x])
      ran[x]++;
  }
}

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

int main() {
  int n, m, q, s, t;

  cin >> n >> m;

  init(n);

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

  cin >> q;

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

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

void boo(int b[], int y[], int w, int m) {
  for (int i = 0; i < m; i++) {
    if (b[i] == w) {
      if (y[b[i + m]] != -1)
        y[b[i + m]] = 1;
    }
  }
  for (int i = m; i < m * 2; i++) {
    if (b[i] == w) {
      if (y[b[i - m]] != -1)
        y[b[i - m]] = 1;
    }
  }
}

int foo(int a, int s, int b[], int m, int n) {
  int z = 0, y[100100] = {}, k = 1;
  y[a] = 1;
  while (k) {
    k = 0;
    for (int i = 0; i < n; i++) {
      if (y[i] == -1)
        continue;
      else if (y[i] == 1) {
        if (i == s) {
          z = 1;
          k = 0;
          break;
        } else {
          k = 1;
          y[i] = -1;
          boo(b, y, i, m);
        }
      }
    }
  }
  return z;
}

int main() {
  int b[200100];
  int n, m, q, a, s;
  int i, j, k;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> b[i] >> b[m + i];
  }

  cin >> q;

  for (i = 0; i < q; i++) {
    cin >> a >> s;
    if (foo(a, s, b, m, n) == 1) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

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

//
//
//  ad-algoD.cpp - created by us162022
//
#include <iostream>
using namespace std;
int find(int join[], int x) {
  if (x == join[x])
    return x;

  return find(join, join[x]);
}
int main(int argc, char *argv[]) {
  int n, m, join[100000], num[100000], s, t;
  cin >> n >> m;

  for (int i = 0; i < n; i++) {
    join[i] = i;
    num[i] = 0;
  }

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

    if (s != t) {
      if (num[s] < num[t])
        join[s] = join[t];

      else {
        join[t] = join[s];
        if (num[s] == num[t])
          num[s]++;
      }
    }
  }

  int q;
  cin >> q;

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

    if (find(join, s) == find(join, t))
      cout << "yes\\n";

    else
      cout << "no\\n";
  }
}

Error: TLE

Fixed submission:

//
//
//  ad-algoD.cpp - created by us162022
//
#include <iostream>
using namespace std;

int main(int argc, char *argv[]) {
  int n, m, join[100000], num[100000], s, t;
  cin >> n >> m;

  for (int i = 0; i < n; i++) {
    join[i] = i;
    num[i] = 1;
  }

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

    if (join[t] != join[s]) {
      int js = join[s];
      int jt = join[t];

      jt = js;
      num[js] += 1;
      num[jt] -= 1;

      for (int j = 0, count = 0; count < num[t]; j++) {
        if (join[j] == jt) {
          join[j] = js;
          num[t] += 1;
          num[t] -= 1;
        }
      }
    }
  }

  int q;
  cin >> q;
  bool answer[10000];

  for (int i = 0; i < q; i++) {
    answer[i] = false;
    cin >> s >> t;

    if (join[s] == join[t])
      answer[i] = true;
  }

  for (int i = 0; i < q; i++) {
    if (answer[i])
      cout << "yes\\n";

    else
      cout << "no\\n";
  }
}
Problem p02240 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <set>
#include <stack>
#include <vector>
using namespace std;

void set_color(const vector<vector<int>> &G, vector<int> &color, int color_name,
               int a, int i) {
  if (i >= G[a].size())
    return;
  if (color[G[a][i]] == -1) {
    color[G[a][i]] = color_name;
    set_color(G, color, color_name, G[a][i], 0);
  }
  set_color(G, color, color_name, a, i + 1);
}

bool DFS(const vector<vector<int>> &G, int a, int b) {
  if (G[a].size() == 0 || G[b].size() == 0)
    return false;
  static vector<int> color(G.size(), -1);
  static int color_name = 0;
  if (color[a] != color[b])
    return false;
  if (color[a] == -1) {
    set_color(G, color, color_name++, a, 0);
  }
  if (color[a] == color[b])
    return true;
  return false;
}

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

  vector<vector<int>> G(n);

  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++) {
  //     cout << i << " : ";
  //     for (vector<int>::iterator it = G[i].begin(); it != G[i].end(); it++){
  //         cout << *it << " ";
  //     }
  //     cout << endl;
  // }

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

Error: MLE

Fixed submission:

#include <iostream>
#include <set>
#include <stack>
#include <vector>
using namespace std;

bool DFS(vector<vector<int>> &G, int a, int b) {
  if (G[a].size() == 0)
    return false;

  stack<pair<int, int>> S;
  vector<bool> is_visited(G.size(), false);
  int idx = 0;
  do {
    if (idx >= G[a].size()) {
      a = S.top().first;
      idx = S.top().second;
      S.pop();
      idx++;
      continue;
    }
    if (G[a][idx] == b)
      return true;
    if (!is_visited[G[a][idx]]) {
      is_visited[G[a][idx]] = true;
      S.push(make_pair(a, idx));
      a = G[a][idx];
      idx = 0;
      continue;
    }
    idx++;
  } while (!S.empty());
  return false;
}

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

  vector<vector<int>> G(n);
  vector<vector<bool>> D(100000, vector<bool>(100000));

  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++) {
  //     cout << i << " : ";
  //     for (set<int>::iterator it = G[i].begin(); it != G[i].end(); it++){
  //         cout << *it << " ";
  //     }
  //     cout << endl;
  // }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    if (DFS(G, a, b)) {
      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;

void set_color(const vector<vector<int>> &u, int p, vector<int> &color, int c) {
  if (color[p] != -1)
    return;
  color[p] = c;
  for (int i = 0; i < u[p].size(); i++) {
    set_color(u, u[p][i], color, c);
  }
}

bool is_friend(const vector<vector<int>> &u, int s, int t) {
  static vector<int> color(u.size(), -1);
  static int c = 0;
  set_color(u, s, color, c++);
  return color[s] == color[t];
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> u(n);
  for (int i = 0; i < m; i++) {
    int s, t;
    cin >> s >> t;
    u[s].push_back(t);
    u[t].push_back(s);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int s, t;
    cin >> s >> t;
    if (is_friend(u, s, t)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

void set_color(const vector<vector<int>> &u, int p, vector<int> &color, int c) {
  if (color[p] != -1)
    return;
  color[p] = c;
  for (int i = 0; i < u[p].size(); i++) {
    set_color(u, u[p][i], color, c);
  }
}

bool is_friend(const vector<vector<int>> &u, int s, int t) {
  vector<int> color(u.size(), -1);
  int c = 0;
  set_color(u, s, color, c++);
  return color[s] == color[t];
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> u(n);
  for (int i = 0; i < m; i++) {
    int s, t;
    cin >> s >> t;
    u[s].push_back(t);
    u[t].push_back(s);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int s, t;
    cin >> s >> t;
    if (is_friend(u, s, t)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int n;
vector<int> g[100002];
int color[100002];

void dfs(int r, int c);
void assigncolor(); // 割り当てる

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);
  }

  assigncolor();

  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);
}

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

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int n, m, q;
int s, t;
int ck[100002];
list<int> l[100002];
list<int> ans[100002];
queue<int> que;

int slv(int x);

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

  for (int i = 0; i < n; i++) {
    if (ck[i] == 0) {
      while (!que.empty())
        que.pop();
      que.push(i);
      slv(i);
    }
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> s >> t;
    int flag = 0;
    for (int j = 0; j < ans[s].size(); j++) {
      int x = ans[s].front();
      ans[s].pop_front();
      ans[s].push_back(x);
      if (x == t) {
        flag = 1;
        break;
      }
    }
    if (flag == 1)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
}

int slv(int x) {
  for (int i = 0; i < l[x].size(); i++) {
    int y = l[x].front();
    l[x].pop_front();
    l[x].push_back(y);

    for (int j = 0; j < que.size(); j++) {
      int z = que.front();
      que.pop();
      que.push(z);
      ans[z].push_back(y);
      ans[y].push_back(z);
    }

    if (ck[y] == 0) {
      que.push(y);
      ck[y] = 1;
      slv(y);
    }
  }
  return (0);
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<int> ad[100000];
vector<int> v, d;

int main() {
  int i, j, n, m, a, b, q, l, k;
  queue<int> Q;

  cin >> n;
  cin >> m;
  for (i = 0; i <= m - 1; i++) {
    cin >> a;
    cin >> b;
    ad[a].push_back(b);
    ad[b].push_back(a);
  }
  for (i = 0; i <= n - 1; i++) {
    ad[i].push_back(-1);
    v.push_back(0);
    d.push_back(-1);
  }
  j = 0;
  for (i = 0; i <= n - 1; i++) {
    if (v[i] == 0) {
      Q.push(i);
      v[i] = 1;
      d[i] = j;
      while (Q.empty() != 1) {
        l = Q.front();
        for (k = 0; ad[l][k] != -1; k++) {
          if (v[ad[l][k]] == 0) {
            Q.push(ad[l][k]);
            v[ad[l][k]] = 1;
            d[ad[l][k]] = j;
          }
        }
        Q.pop();
      }
      j++;
    }
  }
  cin >> q;
  for (i = 0; i <= q - 1; i++) {
    cin >> a;
    cin >> b;
    if (d[a] == d[b])
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

vector<int> ad[100000];
vector<int> v, d;

int main() {
  int i, j, n, m, a, b, q, l, k;
  queue<int> Q;

  cin >> n;
  cin >> m;
  for (i = 0; i <= m - 1; i++) {
    cin >> a;
    cin >> b;
    ad[a].push_back(b);
    ad[b].push_back(a);
  }
  for (i = 0; i <= n - 1; i++)
    ad[i].push_back(-1);
  cin >> q;
  for (i = 0; i <= q - 1; i++) {
    cin >> a;
    cin >> b;
    v.clear();
    d.clear();
    for (j = 0; j <= n - 1; j++) {
      v.push_back(0);
      d.push_back(-1);
    }
    v[a] = 1;
    d[a] = 0;
    Q.push(a);
    while (Q.empty() != 1) {
      l = Q.front();
      for (k = 0; ad[l][k] != -1; k++) {
        if (v[ad[l][k]] == 0) {
          Q.push(ad[l][k]);
          v[ad[l][k]] = 1;
          d[ad[l][k]] = d[l] + 1;
        }
      }
      Q.pop();
    }
    if (d[b] >= 0)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}