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

#include <bits/stdc++.h>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int friendid[100000] = {0};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  vector<vector<int>> v;
  vector<int> u;
  int n, m, r, l, id = 0, k;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    u.push_back(i);
    u.push_back(0);
    v.push_back(u);
    u.pop_back();
    u.pop_back();
  }
  for (int i = 0; i < m; i++) {
    cin >> r >> l;
    ++v[r][1];
    ++v[l][1];
    v[r].push_back(l);
    v[l].push_back(r);
  }

  queue<int> q;
  for (int i = 0; i < n; i++) {

    if (friendid[i] == 0) {

      q.push(i);
      ++id;
      friendid[i] = id;
      while (q.size()) {
        k = q.front();
        q.pop();
        for (int j = 0; j < v[k][1]; j++) {
          if (friendid[v[k][j + 2]] == 0) {
            friendid[v[k][j + 2]] = id;
            q.push(v[k][j + 2]);
          }
        }
      }
    }
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 1000000;
const int CRL = -1;
int v[N] = {0};
void ketugou(int k, int l, int n) {
  int x, mn;
  x = max(v[k], v[l]);
  mn = min(v[k], v[l]);
  for (int i = 0; i < n; i++) {
    if (v[i] == x)
      v[i] = mn;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, q;
  memset(v, CRL, sizeof(v));
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int k, l;
    cin >> k >> l;
    if (v[k] == CRL && v[l] == CRL) {
      v[k] = v[l] = min(k, l);
    } else if (v[k] != CRL && v[l] != CRL) {
      ketugou(k, l, n);
    } else if (v[k] == CRL) {
      v[k] = v[l];
    } else {
      v[l] = v[k];
    }
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int k, l;
    cin >> k >> l;
    if (v[k] == CRL || v[l] == CRL)
      cout << "no" << endl;
    else if (v[k] == v[l])
      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;
struct user {
  user(){};
  user(int i) : id{i}, fs{}, group{-1} {};
  int id;
  vector<int> fs;
  int group;
};

void add_friends(user *us, int x, int y) {
  us[x].fs.push_back(y);
  us[y].fs.push_back(x);
}

void dfs(user *us, int id, int g) {
  stack<int> ss{};
  ss.push(id);
  while (ss.size()) {
    int p = ss.top();
    ss.pop();
    us[p].group = g;
    vector<int> fs = us[p].fs;
    for (auto x : fs) {
      if (us[x].group == g) {
        continue;
      }
      ss.push(x);
    }
  }
}

void make_group(user *us, int n) {
  int g = 0;
  for (int i = 0; i < n; ++i) {
    if (us[i].group < 0) {
      dfs(us, i, g);
      ++g;
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  user us[n];
  for (int i = 0; i < n; ++i) {
    us[i] = user(i);
  }
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    add_friends(us, a, b);
  }
  make_group(us, n);
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int a, b;
    cin >> a >> b;
    if (us[a].group == us[b].group) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;
struct user {
  user(){};
  user(int i) : id{i}, fs{}, group{-1} {};
  int id;
  vector<int> fs;
  int group;
};

void add_friends(user *us, int x, int y) {
  us[x].fs.push_back(y);
  us[y].fs.push_back(x);
}

void dfs(user *us, int id, int g) {
  if (us[id].group == g) {
    return;
  }
  us[id].group = g;
  vector<int> fs = us[id].fs;
  for (auto x : fs) {
    dfs(us, x, g);
  }
}

void make_group(user *us, int n) {
  int g = 0;
  for (int i = 0; i < n; ++i) {
    if (us[i].group < 0) {
      dfs(us, i, g);
      ++g;
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  user us[n];
  for (int i = 0; i < n; ++i) {
    us[i] = user(i);
  }
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    add_friends(us, a, b);
  }
  make_group(us, n);
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int a, b;
    cin >> a >> b;
    if (us[a].group == us[b].group) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <vector>

#define N 100000

using namespace std;

vector<int> G[N];
int color[N];

void dfs(int s, int c) {
  color[s] = c;
  for (int i = 0; i < G[s].size(); i++) {
    if (color[G[s][i]] != c)
      dfs(G[s][i], c);
  }
}

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++)
    color[i] = -1;
  for (int i = 0; i < m; i++) {
    int s, t;
    scanf("%d %d", &s, &t);
    G[s].push_back(t);
    G[t].push_back(s);
  }
  int q;
  scanf("%d", &q);
  for (int i = 0; i < n; i++) {
    if (color[i] == -1)
      dfs(i, i); // node_id and color_id
  }
  for (int i = 0; i < q; i++) {
    int s, t;
    scanf("%d %d", &s, &t);
    if (color[s] == color[t])
      cout << "yes\\n";
    else
      cout << "no\\n";
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <vector>

#define N 100000

using namespace std;

vector<int> G[N];
int color[N];

void dfs(int s, int c) {
  color[s] = c;
  for (int i = 0; i < G[s].size(); i++) {
    if (color[G[s][i]] != c)
      dfs(G[s][i], c);
  }
}

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++)
    color[i] = -1;
  for (int i = 0; i < m; i++) {
    int s, t;
    scanf("%d %d", &s, &t);
    G[s].push_back(t);
    G[t].push_back(s);
  }
  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int s, t;
    scanf("%d %d", &s, &t);
    dfs(s, i);
    if (color[s] == color[t])
      cout << "yes\\n";
    else
      cout << "no\\n";
  }
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int inf = 1000000000;

class UF {
public:
  vector<int> u;
  vector<int> r;

  void init(int n) {
    u.resize(n, 0);
    r.resize(n, 0);
    for (int i = 0; i < n; i++)
      u[i] = i;
  }
  int root(int x) {
    // if( u[x] == x ) return x;
    // else return root(u[x]);
    return u[x] == x ? x : u[x] = root(u[x]);
  }
  bool same(int x, int y) { return root(x) == root(y); }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;

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

int main(void) {

  int n, m;
  cin >> n >> m;
  UF uf;
  uf.init(n);
  int s, t;
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &s, &t);
    uf.unite(s, t);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &s, &t);
    cout << (uf.same(s, t) ? "yes" : "no") << endl;
  }

  return 0;
}

// EOF

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int inf = 1000000000;

class UF {
public:
  vector<int> u;
  void init(int n) {
    u.resize(n, 0);
    for (int i = 0; i < n; i++)
      u[i] = i;
  }
  int root(int x) {
    if (u[x] == x)
      return x;
    else
      return root(u[x]);
  }
  bool same(int x, int y) { return root(x) == root(y); }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    u[x] = y;
  }
};

int main(void) {

  int n, m;
  cin >> n >> m;
  UF uf;
  uf.init(n);
  int s, t;
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &s, &t);
    uf.unite(s, t);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &s, &t);
    cout << (uf.same(s, t) ? "yes" : "no") << endl;
  }

  return 0;
}

// EOF
Problem p02240 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
using namespace std;

int n;

string *color;

queue<int> q;

int *ans;

vector<int> g[1000000];

void bfs(int x) {
  color[x] = "gray";
  q.push(x);

  while (q.size() != 0) {
    int u = q.front();
    q.pop();

    for (int i = 0; i < g[u].size(); i++) {
      int v = g[u][i];
      if (color[v] == "white") {
        color[v] = "gray";
        q.push(v);
      }
    }

    color[u] = "black";
    ans[u] = x;
  }
  return;
}

int main() {

  cin >> n;

  color = new string[n];

  ans = new int[n];

  int m;

  cin >> m;

  int a, b;

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

  for (int i = 0; i < n; i++) {
    color[i] = "white";
  }

  for (int j = 0; j < n; j++) {
    if (color[j] == "white") {
      bfs(j);
    }
  }

  int q;

  cin >> q;

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

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
using namespace std;

int n;

string *color;

queue<int> q;

int *ans;

vector<int> g[10000];

void bfs(int x) {
  color[x] = "gray";
  q.push(x);

  while (q.size() != 0) {
    int u = q.front();
    q.pop();

    for (int i = 0; i < g[u].size(); i++) {
      int v = g[u][i];
      if (color[v] == "white") {
        color[v] = "gray";
        q.push(v);
      }
    }

    color[u] = "black";
    ans[u] = x;
  }
  return;
}

int main() {

  cin >> n;

  color = new string[n];

  ans = new int[n];

  int m;

  cin >> m;

  int a, b;

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

  for (int i = 0; i < n; i++) {
    color[i] = "white";
  }

  for (int j = 0; j < n; j++) {
    if (color[j] == "white") {
      bfs(j);
    }
  }

  int q;

  cin >> q;

  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    if (ans[a] == ans[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;

int n, m, q;
int dp[100000];
vector<int> v[100000];

void f(int p, int idx) {
  if (dp[p] < 0) {
    dp[p] = idx;
    for (int i = 0; i < v[p].size(); i++) {
      f(v[p][i], idx);
    }
  }
}

void f2() {
  int idx = 0;
  for (int i = 0; i < n; i++) {
    if (dp[i] < 0) {
      f(i, idx++);
    }
  }
}

int main() {
  int s, t;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> s >> t;
    v[s].push_back(t);
    v[t].push_back(s);
  }
  cin >> q;
  fill(dp, dp + n, -1);
  f2();
  for (int I = 0; I < q; I++) {
    cin >> s >> t;
    if (dp[s] == dp[t]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int n, m, q;
bool dp[100000];
vector<int> v[100000];

bool f(int s, int t) {
  if (!dp[s]) {
    if (s == t) {
      return true;
    }
    dp[s] = true;
    for (int i = 0; i < v[s].size(); i++) {
      if (f(v[s][i], t)) {
        return true;
      }
    }
  }
  return false;
}

int main() {
  int s, t;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> s >> t;
    v[s].push_back(t);
    v[t].push_back(s);
  }
  cin >> q;
  for (int I = 0; I < q; I++) {
    cin >> s >> t;
    fill(dp, dp + n, false);
    if (f(s, t)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

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

#include <cstdio>
#include <iostream>
#include <vector>

#define MAX 100000

#define WHITE 0
#define GRAY 1
#define BLACK 2

using namespace std;

vector<int> G[MAX];
int graphc[MAX];
int color[MAX];
int colorCount;

void def(int o) {
  int v;

  for (int i = 0; i < G[o].size(); i++) {

    v = G[o][i];
    if (color[v] == WHITE) {

      color[v] = GRAY;
      def(v);
    }
  }
  color[o] = BLACK;
  graphc[o] = colorCount;
}

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

  cin >> n >> m;

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

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

    if (color[i] == WHITE) {
      colorCount++;
      def(i);
    }
  }

  cin >> q;

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

    cin >> s >> t;

    cout << ((graphc[s] == graphc[t]) ? "yes" : "no") << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>

#define MAX 100000

using namespace std;

int gcol[MAX];

int main(void) {
  int n;
  int m;
  int s;
  int t;
  int q;
  int type = 1;

  cin >> n >> m;

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

    if (gcol[s] != 0 && gcol[t] != 0) {
      for (int j = 0; j < n; i++) {
        if (gcol[s] == gcol[j]) {
          gcol[j] = gcol[s];
        }
      }
    }
    if (gcol[s] != 0) {
      gcol[t] = gcol[s];
    } else if (gcol[t] != 0) {
      gcol[s] = gcol[t];
    } else {
      gcol[s] = gcol[t] = type;
      type++;
    }
  }

  cin >> q;

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

    if (gcol[s] == gcol[t] && gcol[s] != 0) {
      cout << "yes";
    } else {
      cout << "no";
    }

    cout << endl;
  }

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

/*
** Connected Components
*/

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

const int MAX = 100000;

int n, m, q, c[MAX];
vector<int> g[MAX];

void init() {
  scanf("%d%d", &n, &m);
  int u, v;
  while (m--) {
    scanf("%d%d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
}

void bfs(int u, int mark) {
  queue<int> que;
  que.push(u);
  c[u] = mark;

  while (!que.empty()) {
    int t = que.front();
    que.pop();
    for (int i = 0; i < g[t].size(); i++) {
      if (c[g[t][i]] == 0) {
        c[g[t][i]] = mark;
        que.push(g[t][i]);
      }
    }
  }
}

void set_c() {
  int mark = 1;
  for (int i = 0; i < n; i++) {
    c[i] = 0;
  }
  for (int i = 0; i < n; i++) {
    if (c[i] == 0) {
      bfs(i, mark++);
    }
  }
}

void solve() {
  init();
  scanf("%d", &q);
  set_c();
  int u, v;
  while (q--) {
    scanf("%d%d", &u, &v);
    if (c[u] == c[v]) {
      printf("yes\\n");
    } else {
      printf("no\\n");
    }
  }
}

int main() {
  // freopen("infile", "r", stdin);
  // freopen("outfile", "w", stdout);
  solve();
  return 0;
}


Error: TLE

Fixed submission:

/*
** Connected Components
*/

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

const int MAX = 100000;

int n, m, q, c[MAX];
vector<int> g[MAX];

void init() {
  scanf("%d%d", &n, &m);
  int u, v;
  while (m--) {
    scanf("%d%d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
}

bool find_con(int u, int v) {
  queue<int> que;
  que.push(u);
  memset(c, 0, sizeof(c));
  c[u] = 1;
  bool rec = false;

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

  return rec;
}

void solve() {
  init();
  scanf("%d", &q);
  int u, v;
  while (q--) {
    scanf("%d%d", &u, &v);
    if (find_con(u, v)) {
      printf("yes\\n");
    } else {
      printf("no\\n");
    }
  }
}

int main() {
  // freopen("infile", "r", stdin);
  // freopen("outfile", "w", stdout);
  solve();
  return 0;
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void dfs(int root, int node, vector<vector<int>> &adjlist, vector<int> &used) {

  for (int i = 0; i < adjlist[node].size(); i++) {
    if (used[adjlist[node][i]] == -1) {
      used[adjlist[node][i]] = root;
      dfs(root, adjlist[node][i], adjlist, used);
    }
  }
}

int main() {

  int n, m;
  cin >> n >> m;

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

  // 入力
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b; // a ←→ b に辺がある
    adjlist[a].push_back(b);
    adjlist[b].push_back(a);
  }

  vector<int> used(n, -1);

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

  /*for(int i = 0; i < n; i++){
      cout << i << " " << used[i] << endl;
  }*/

  // 質問
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;

    if (used[a] == used[b])
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

bool dfs(int node, int gole, vector<vector<int>> &adjlist, vector<bool> &used) {

  if (node == gole)
    return true;

  for (int i = 0; i < adjlist[node].size(); i++) {
    if (used[adjlist[node][i]] == false) {
      used[adjlist[node][i]] = true;
      bool flag = dfs(adjlist[node][i], gole, adjlist, used);
      if (flag)
        return true;
    }
  }

  return false;
}

int main() {

  int n, m;
  cin >> n >> m;

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

  // 入力
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b; // a ←→ b に辺がある
    adjlist[a].push_back(b);
    adjlist[b].push_back(a);
  }

  // 質問
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;

    vector<bool> used(n, false);
    used[a] = true;
    if (dfs(a, b, adjlist, used))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <list>
#include <math.h>
#include <stdio.h>
#include <utility>
#include <vector>
#define N 100000
using namespace std;
int n;
list<int> adj[N];
bool f[N] = {};
int g[N] = {};

void setGraphNum(int v, int i) {
  g[v] = i;
  f[v] = true;
  list<int>::iterator itr = adj[v].begin();
  while (itr != adj[v].end()) {
    if (!f[*itr])
      setGraphNum(*itr, i);
    itr++;
  }
}
void CalcGraphNum() {
  int k = 1;
  for (int i = 0; i < n; i++) {
    if (g[i] == 0)
      setGraphNum(i, k++);
  }
}

int main() {
  int m, u, v, q;
  cin >> n >> m;

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

  CalcGraphNum();
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> u >> v;
    if (g[u] == g[v])
      printf("yes\\n");
    else
      printf("no\\n");
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
#include <math.h>
#include <stdio.h>
#include <utility>
#include <vector>
#define N 100000
using namespace std;
int n;
list<int> adj[N];

bool PathExists(int u, int v, bool f[]) {
  f[u] = true;
  if (u == v)
    return true;
  list<int>::iterator itr = adj[u].begin();
  while (itr != adj[u].end()) {
    if (!f[*itr] && PathExists(*itr, v, f))
      return true;
    itr++;
  }
  return false;
}

int main() {
  int m, u, v, q;
  cin >> n >> m;

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

  cin >> q;
  for (int i = 0; i < q; i++) {
    bool f[N] = {};
    cin >> u >> v;
    if (PathExists(u, v, f))
      printf("yes\\n");
    else
      printf("no\\n");
  }

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

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

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

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

void dfs(int v, int id) {
  stack<int> s;
  s.push(v);
  color[v] = id;

  while (!s.empty()) {
    int u = s.top();
    s.pop();
    for (int i = 0; i < G[u].size(); i++) {
      int v1 = G[u][i];
      if (color[v1] == NIL) {
        color[v1] = id;
        s.push(v1);
      }
    }
  }
}

void assignColor() {
  int id = 1;

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

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

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

  assignColor();

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

  return 0;
}


Error: 0

Fixed submission:

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

const int NIL = -1;

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

void dfs(int v, int id) {
  stack<int> s;
  s.push(v);
  color[v] = id;

  while (!s.empty()) {
    int u = s.top();
    s.pop();
    for (int i = 0; i < G[u].size(); i++) {
      int v1 = G[u][i];
      if (color[v1] == NIL) {
        color[v1] = id;
        s.push(v1);
      }
    }
  }
}

void assignColor() {
  int id = 1;

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

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

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

  assignColor();

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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define NMAX 100000
using namespace std;

struct Node {
  vector<int> friends;
};
int group[NMAX];

void bfs(vector<Node> &nodes, int id) {
  static int groupno = 1;
  queue<int> q;
  q.push(id);
  set<int> st;

  group[id] = groupno;

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < nodes[u].friends.size(); i++) {
      int fid = nodes[u].friends[i];
      if (group[fid] == -1) {
        q.push(fid);
        group[fid] = groupno;
      }
    }
  }
  groupno++;
}

int main() {
  vector<Node> nodes;
  int nrel, nuser;

  cin >> nuser;
  cin >> nrel;
  nodes.resize(nuser);
  int tmp1, tmp2;
  for (int i = 0; i < nrel; i++) {
    cin >> tmp1 >> tmp2;
    nodes[tmp1].friends.push_back(tmp2);
    nodes[tmp2].friends.push_back(tmp1);
  }
  for (int i = 0; i < nodes.size(); i++) {
    group[i] = -1;
  }

  int q, s, t;
  cin >> q;

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

Error: TLE

Fixed submission:

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

struct Node {
  vector<int> friends;
  int depth;
};

void bfs(vector<Node> nodes, int id, int target) {
  queue<int> q;
  q.push(id);

  for (auto el : nodes) {
    el.depth = -1;
  }
  nodes[id].depth = 0;

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto fid : nodes[u].friends) {
      if (nodes[fid].depth <= 0) {
        q.push(fid);
        nodes[fid].depth = nodes[u].depth + 1;
        if (fid == target) {
          cout << "yes" << endl;
          return;
        }
      }
    }
  }
  cout << "no" << endl;
}

int main() {
  vector<Node> nodes;
  int nrel, nuser;
  cin >> nuser;
  cin >> nrel;
  nodes.resize(nuser);
  int tmp1, tmp2;
  for (int i = 0; i < nrel; i++) {
    cin >> tmp1 >> tmp2;
    nodes[tmp1].friends.push_back(tmp2);
    nodes[tmp2].friends.push_back(tmp1);
  }

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