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

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

int main() {
  int n, m, q, k;
  int s, t;
  int i, j;
  queue<int> nq;
  vector<vector<int>> node;
  vector<int> ngrp;

  scanf("%d%d", &n, &m);
  node.resize(n);
  ngrp.resize(n);

  for (i = 0; i < m; ++i) {
    scanf("%d%d", &s, &t);
    node[s].push_back(t);
    node[t].push_back(s);
  }

  for (i = 0; i < n; ++i) {
    ngrp[i] = -1;
  }

  for (i = 0; i < n; ++i) {
    if (ngrp[i] == -1) {
      nq.push(i);
      ngrp[i] = i;
      while (!nq.empty()) {
        k = nq.front();
        for (j = 0; j < (int)node[k].size(); ++j) {
          if (ngrp[node[k][j]] == -1) {
            ngrp[node[k][j]] = i;
            nq.push(node[k][j]);
          }
        }
        nq.pop();
      }
    }
  }
  scanf("%d", &q);
  for (i = 0; i < q; ++i) {
    scanf("%d%d", &s, &t);
    printf((ngrp[s] == ngrp[t]) ? "yes\\n" : "no\\n");
  }
  return 0;
}

Error: TLE

Fixed submission:

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

bool isReachable(int k, int l, vector<vector<int>> node) {
  int n = (int)node.size();
  int i;
  int m;
  queue<int> qnode;
  vector<int> visited;
  visited.resize(n);
  for (i = 0; i < n; ++i) {
    visited[i] = 0;
  }
  visited[k] = 1;
  qnode.push(k);
  while (!qnode.empty()) {
    m = qnode.front();
    for (i = 0; i < (int)node[m].size(); ++i) {
      if (node[m][i] == l) {
        return true;
      } else {
        if (visited[node[m][i]] == 0) {
          qnode.push(node[m][i]);
          visited[node[m][i]] = 1;
        }
      }
    }
    qnode.pop();
  }
  return false;
}

int main() {
  int m, n, k, l;
  int q;
  int i;
  vector<vector<int>> node;
  scanf("%d%d", &n, &m);
  node.resize(n);
  for (i = 0; i < m; ++i) {
    scanf("%d%d", &k, &l);
    node[k].push_back(l);
    node[l].push_back(k);
  }
  scanf("%d", &q);
  for (i = 0; i < q; ++i) {
    scanf("%d%d", &k, &l);
    printf((isReachable(k, l, node) ? "yes\\n" : "no\\n"));
  }
  return 0;
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<int> par(100005, -1);

void initialize(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
  }
  return;
}

int root(int x) {
  if (x == par[x]) {
    return x;
  } else {
    return par[x] = root(par[x]);
  }
}

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

void uni(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y) {
    return;
  } else {
    par[x] = y;
  }
}

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

  cin >> n >> m;

  initialize(n);

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

  cin >> q;

  for (i = 0; i < q; i++) {
    cin >> s >> t;
    if (find(s, 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, q, i, j;
  int s, t;
  vector<int> L(100005, -1);
  cin >> n >> m;

  for (j = 0; j < n; j++) {
    L[j] = j;
  }

  for (i = 0; i < m; i++) {
    cin >> s >> t;
    if (L[s] != L[t]) {
      int a = L[t];
      for (j = 0; j < n; j++) {
        if (L[j] == a) {
          L[j] = L[s];
        }
      }
    }
  }

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

#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
vector<vector<int>> v(100001);
int n, m;
int f[100001] = {0};
int r = 0;

void dfs(int a) {
  if (f[a] == 0) {
    f[a] = r;
  } else {
    return;
  }
  for (int i = 0; i < v[a].size(); i++) {
    if (f[v[a][i]] == 0) {
      dfs(v[a][i]);
    }
  }
}

int main() {
  cin >> n >> m;
  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++) {
    if (f[i] == 0) {
      r++;
    }
    dfs(i);
  }

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

Error: TLE

Fixed submission:

#include <iostream>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
vector<vector<int>> v(100001);
// stack<int> st;
int n, m;
bool f[100001] = {false};

int main() {
  cin >> n >> m;
  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;
  vector<string> Ans(q);
  for (int i = 0; i < q; i++) {
    int s, t;
    cin >> s >> t;

    stack<int> st;
    st.push(s);
    while (!st.empty()) {
      int a = st.top();
      st.pop();
      f[a] = true;
      if (a == t) {
        break;
      }
      for (int i = 0; i < v[a].size(); i++) {
        if (f[v[a][i]] == false) {
          st.push(v[a][i]);
        }
      }
    }
    if (f[t] == true) {
      Ans[i] = "yes";
      memset(f, false, n);
    } else {
      Ans[i] = "no";
      memset(f, false, n);
    }
  }
  for (int i = 0; i < q; i++) {
    cout << Ans[i] << endl;
  }
  return 0;
}
Problem p02240 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

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

void dfs(int idx, int c) {
  stack<int> s;
  s.push(idx);
  color[idx] = 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] == -1) {
        color[v] = c;
        s.push(v);
      }
    }
  }
}

void AssignColor() {
  int id = 1;
  for (int i = 0; i < n; ++i)
    color[i] = -1;
  for (int u = 0; u < n; ++u) {
    if (color[u] == -1)
      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);
  }

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

Error: 0

Fixed submission:

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

using namespace std;

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

void dfs(int idx, int c) {
  stack<int> s;
  s.push(idx);
  color[idx] = 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] == -1) {
        color[v] = c;
        s.push(v);
      }
    }
  }
}

void AssignColor() {
  int id = 1;
  for (int i = 0; i < n; ++i)
    color[i] = -1;
  for (int u = 0; u < n; ++u) {
    if (color[u] == -1)
      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);
  }

  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;
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <queue>
#include <stdio.h>
#include <vector>

#define MAX 100000

std::vector<int> G[MAX + 1];
int group[MAX];
int n, m;

bool bfs(int src, int id) {

  std::queue<int> Q;
  Q.push(src);
  while (!Q.empty()) {
    int cur = Q.front();
    Q.pop();
    for (int i = 0; i < G[cur].size(); ++i) {
      int t = G[cur][i];
      if (group[t] != -1)
        continue;
      group[t] = id;
      Q.push(t);
    }
  }

  return false;
}

void assignGroup() {
  int id = 1;

  for (int i = 0; i < n; ++i)
    group[i] = -1;
  for (int i = 0; i < n; ++i) {
    if (group[i] != -1)
      continue;
    bfs(i, id++);
  }
}

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

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

  assignGroup();

  int q;
  scanf("%d", &q);
  for (int i = 1; i <= q; ++i) {
    int s, d;
    scanf("%d %d", &s, &d);
    group[s] != -1 && group[s] == group[d] ? printf("yes\\n") : printf("no\\n");
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <queue>
#include <stdio.h>
#include <vector>

#define MAX 100000

std::vector<int> G[MAX + 1];
int n, m;

bool bfs(int src, int dest) {
  bool C[MAX];
  for (int i = 0; i < n; ++i)
    C[i] = false;

  std::queue<int> Q;
  Q.push(src);
  while (!Q.empty()) {
    int cur = Q.front();
    Q.pop();
    for (int i = 0; i < G[cur].size(); ++i) {
      int t = G[cur][i];
      if (C[t])
        continue;
      if (t == dest)
        return true;
      Q.push(t);
      C[t] = true;
    }
  }

  return false;
}

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

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

  int q;
  scanf("%d", &q);
  for (int i = 1; i <= q; ++i) {
    int s, d;
    scanf("%d %d", &s, &d);
    bfs(s, d) ? printf("yes\\n") : printf("no\\n");
  }

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

// http://hfuji.hatenablog.jp/entry/2015/12/09/175721

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

using namespace std;

// ---------------------
// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
typedef long long ll;
vector<int> par;
vector<int> rnk; // ??±???

void init(int n) {
  par = vector<int>(n);
  rnk = vector<int>(n);
  REP(i, n) {
    par[i] = i;
    rnk[i] = 0;
  }
}
int root(int key) {
  if (par[key] == key)
    return key;
  return root(par[key]);
}
int same(int a, int b) { return root(a) == root(b); }
void unite(int a, int b) {
  int x = root(a);
  int y = root(b);
  if (x == y)
    return;

  if (rnk[x] < rnk[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rnk[x] == rnk[y])
      rnk[x]++;
  }
}

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

  init(n);

  REP(_, m) {
    int a, b;
    // cin >> a >> b;
    scanf("%d %d", &a, &b);
    unite(a, b);
  }
  int q;
  cin >> q;
  REP(_, q) {
    int a, b;
    // cin >> a >> b;
    scanf("%d %d", &a, &b);
    if (same(a, b)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

// ---------------------
// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
typedef long long ll;
vector<int> par;

void init(int n) {
  par = vector<int>(n);
  REP(i, n) { par[i] = i; }
}
int root(int key) {
  if (par[key] == key)
    return key;
  return root(par[key]);
}
int same(int a, int b) { return root(a) == root(b); }
void unite(int a, int b) {
  int x = root(a);
  int y = root(b);

  if (x == y)
    return;

  par[x] = y;
}

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

  init(n);

  REP(_, m) {
    int a, b;
    // cin >> a >> b;
    scanf("%d %d", &a, &b);
    unite(a, b);
  }
  int q;
  cin >> q;
  REP(_, q) {
    int a, b;
    // cin >> a >> b;
    scanf("%d %d", &a, &b);
    if (same(a, b)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

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

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

const long MAX = 100000;
const long NIL = -1;

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

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

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

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

  assignColor();

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

  return 0;
}

Error: MLE

Fixed submission:

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

const int N = 10000;
const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;
const int NIL = -1;

int n, M[N][N];
int color[N], cc[N], cp = 0;
int nt[N];

int next(int u) {
  for (int v = nt[u]; v <= n; v++) {
    nt[u] = v + 1;
    if (M[u][v])
      return v;
  }
  return NIL;
}

void dfs_visit(int r) {
  for (int i = 0; i < n; i++)
    nt[i] = 0;

  stack<int> S;
  S.push(r);
  color[r] = GRAY;
  cc[r] = cp;

  while (!S.empty()) {
    int u = S.top();
    int v = next(u);
    if (v != NIL) {
      if (color[v] == WHITE) {
        color[v] = GRAY;
        cc[v] = cp;
        S.push(v);
      }
    } else {
      S.pop();
      color[u] = BLACK;
    }
  }
  cp++;
}

void dfs() {
  for (int i = 0; i < n; i++) {
    color[i] = WHITE;
    nt[i] = 0;
  }

  for (int u = 0; u < n; u++) {
    if (color[u] == WHITE)
      dfs_visit(u);
  }
}

int main() {
  int m, q;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      M[i][j] = 0;
  }
  for (int i = 0; i < m; i++) {
    int s, t;
    cin >> s >> t;
    M[s][t] = M[t][s] = 1;
  }

  dfs();

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

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

#include <algorithm>
#include <climits>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define MAX_N 100000
#define MAX_M 100000
#define MAX_Q 10000

int n, m, q, k, kind[MAX_N] = {};
queue<int> Q;

void bf(vector<vector<int>> &sns, int s) {
  Q.push(s);
  while (!Q.empty()) {
    s = Q.front();
    Q.pop();
    kind[s] = k;
    for (int i = 0; i < sns[s].size(); i++) {
      if (!kind[sns[s][i]])
        Q.push(sns[s][i]);
    }
  }
}

int main() {
  cin >> n >> m;
  vector<vector<int>> sns(n);
  int s, d;
  while (m--) {
    cin >> s >> d;
    sns.at(s).push_back(d);
    sns.at(d).push_back(s);
  }
  k = 0;
  for (int s = 0; s < n; s++) {
    if (!kind[s]) {
      k++;
      bf(sns, s);
    }
  }
  cin >> q;
  while (q--) {
    cin >> s >> d;
    cout << (kind[s] == kind[d] ? "yes" : "no") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define MAX_N 100000
#define MAX_M 100000
#define MAX_Q 10000

int n, m, q;

int bf(vector<vector<int>> &sns, int src, int dst) {
  int d[MAX_N] = {};
  queue<int> Q;
  Q.push(src);
  while (!Q.empty()) {
    if (dst == (src = Q.front())) {
      return 1;
    }
    Q.pop();
    d[src]++;
    for (auto itr = sns.at(src).begin(); itr != sns.at(src).end(); itr++) {
      if (!d[*itr])
        Q.push(*itr);
    }
  }
  return 0;
}

int main() {
  cin >> n >> m;
  vector<vector<int>> sns(n);
  int s, d;
  while (m--) {
    cin >> s >> d;
    sns.at(s).push_back(d);
    sns.at(d).push_back(s);
  }
  cin >> q;
  while (q--) {
    cin >> s >> d;
    cout << (bf(sns, s, d) ? "yes" : "no") << endl;
  }
  return 0;
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, m, s, t, u;
vector<vector<int>> a;
int f[100000];

void path(int s1, int w) {
  f[s1] = w;
  for (int i = 0; i < (int)a[s1].size(); i++)
    if (f[a[s1][i]] == 0)
      path(a[s1][i], w);
  return;
}

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

  a.resize(n);

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

  for (int i = 0; i < n; i++)
    f[i] = 0;

  int j = 0, k = 1;
  while (j < n) {
    if (f[j] == 0) {
      path(j, k);
      k++;
    }
    j++;
  }

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

    if (f[s] == f[t])
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

int n, m, s, t, u;
vector<vector<int>> a;
bool f[100000];

bool path(int s1) {
  f[s1] = 1;
  for (int i = 0; i < (int)a[s1].size(); i++) {
    if (a[s1][i] == t) {
      return true;
    } else if (!f[a[s1][i]])
      if (path(a[s1][i]))
        return true;
  }
  return false;
}

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

  a.resize(n);

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

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

    for (int i = 0; i < n; i++)
      f[i] = 0;

    if (path(s))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }

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

#include <iostream>
#include <stack>
#include <vector>
using namespace std;
static const int MAX = 100000;
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 assign() {
  int id = 1;
  for (int i = 0; i < n; i++) {
    color[i] = NIL;
  }
  for (int j = 0; j < n; j++) {
    if (color[j] == NIL) {
      dfs(j, 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);
  }
  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: 0

Fixed submission:

#include <iostream>
#include <stack>
#include <vector>
using namespace std;
static const int MAX = 10000;
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 assign() {
  int id = 1;
  for (int i = 0; i < n; i++) {
    color[i] = NIL;
  }
  for (int j = 0; j < n; j++) {
    if (color[j] == NIL) {
      dfs(j, 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);
  }
  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;
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <queue>
#include <vector>

typedef struct {
  std::vector<int> neighbors;
  int cluster;
} node;

void dfs(node *nodes, int id, int cluster) {
  for (int i = 0; i < nodes[id].neighbors.size(); i++) {
    if (nodes[nodes[id].neighbors[i]].cluster == -1) {
      nodes[nodes[id].neighbors[i]].cluster = cluster;
      dfs(nodes, nodes[id].neighbors[i], cluster);
    }
  }
}

int main() {
  int n, d, num_relation, n_query, s, t, cluster;
  node *nodes;

  scanf("%d %d", &n, &num_relation);
  nodes = new node[n];
  for (int i = 0; i < n; i++) {
    nodes[i].cluster = -1;
  }
  for (int i = 0; i < num_relation; i++) {
    scanf("%d %d", &s, &t);
    nodes[s].neighbors.push_back(t);
    nodes[t].neighbors.push_back(s);
  }
  cluster = -1;
  for (int i = 0; i < n; i++) {
    cluster++;
    if (nodes[i].cluster == -1)
      dfs(nodes, i, cluster);
  }
  scanf("%d", &n_query);
  for (int i = 0; i < n_query; i++) {
    scanf("%d %d", &s, &t);
    if (nodes[s].cluster != -1 && nodes[s].cluster != -1 &&
        nodes[s].cluster == nodes[t].cluster)
      printf("yes\\n");
    else
      printf("no\\n");
  }

  delete[] nodes;
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <queue>
#include <vector>

typedef struct {
  bool isVisited;
  std::vector<int> neighbors;
} node;

void dfs(node *nodes, int id, int t) {

  for (int i = 0; i < nodes[id].neighbors.size(); i++) {
    if (nodes[t].isVisited)
      break;
    if (!nodes[nodes[id].neighbors[i]].isVisited) {
      nodes[nodes[id].neighbors[i]].isVisited = true;
      dfs(nodes, nodes[id].neighbors[i], t);
    }
  }
}

int main() {
  int n, d, num_relation, n_query, s, t;
  node *nodes;

  scanf("%d %d", &n, &num_relation);
  nodes = new node[n];
  for (int i = 0; i < num_relation; i++) {
    scanf("%d %d", &s, &t);
    nodes[s].neighbors.push_back(t);
    nodes[t].neighbors.push_back(s);
  }

  scanf("%d", &n_query);
  for (int i = 0; i < n_query; i++) {
    for (int j = 0; j < n; j++) {
      nodes[j].isVisited = false;
    }
    scanf("%d %d", &s, &t);
    dfs(nodes, s, t);
    if (nodes[t].isVisited)
      printf("yes\\n");
    else
      printf("no\\n");
  }

  delete[] nodes;
  return 0;
}