Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

struct Info {
  Info() { s = t = -1; }
  Info(int tmp_s, int tmp_t) {
    s = tmp_s;
    t = tmp_t;
  }
  int s;
  int t;
};

class Relation {
public:
  Relation(int tmp_n, int tmp_m) {
    n = tmp_n;
    m = tmp_m;
    global = new Info[m];
    each = new int *[n];
    each_index = new int[n];
    num_of_each = new int[n];
    group = new int[n];
    for (int i = 0; i < n; i++) {
      each_index[i] = 0;
      num_of_each[i] = 0;
      group[i] = 0;
    }
    global_index = 0;
    group_number = 1;
  }

  void push(int s, int t) {
    global[global_index++] = Info(s, t);
    num_of_each[s]++;
    num_of_each[t]++;
  }

  void allocate() {
    for (int i = 0; i < n; i++) {
      if (num_of_each[i] > 0) {
        each[i] = new int[num_of_each[i]];
      }
    }
    Info tmp;
    for (int i = 0; i < m; i++) {
      tmp = global[i];
      each[tmp.s][each_index[tmp.s]++] = tmp.t;
      each[tmp.t][each_index[tmp.t]++] = tmp.s;
    }
  }

  void makeGroup() {
    int node, i;
    queue<int> Q;
    Q.push(0);
    group[0] = 1;
    while (true) {
      while (!Q.empty()) {
        node = Q.front();
        Q.pop();
        if (num_of_each[node] > 0) {
          for (int k = 0; k < num_of_each[node]; k++) {
            if (group[each[node][k]] == 0) {
              group[each[node][k]] = group[node];
              Q.push(each[node][k]);
            }
          }
        }
      }
      group_number++;

      for (i = 0; i < n; i++) {
        if (group[i] == 0) {
          group[i] = group_number;
          Q.push(i);
          break;
        }
      }
      if (i == n)
        break;
    }
  }

  bool canAccsess(int s, int t) { return group[s] == group[t]; }

private:
  Info *global;
  int **each;
  int *each_index;
  int *num_of_each;
  int *group;
  int group_number;
  int global_index, n, m;
};

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

  Relation relation(n, m);
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &s, &t);
    relation.push(s, t);
  }
  relation.allocate();
  relation.makeGroup();
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &s, &t);
    if (relation.canAccsess(s, t)) {
      printf("yes\\n");
    } else {
      printf("no\\n");
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

struct Info {
  Info() { s = t = -1; }
  Info(int tmp_s, int tmp_t) {
    s = tmp_s;
    t = tmp_t;
  }
  int s;
  int t;
};

class Relation {
public:
  Relation(int tmp_n, int tmp_m) {
    n = tmp_n;
    m = tmp_m;
    global = new Info[m];
    each = new int *[n];
    each_index = new int[n];
    num_of_each = new int[n];
    went = new int[n];
    for (int i = 0; i < n; i++) {
      each_index[i] = 0;
      num_of_each[i] = 0;
      went[i] = 0;
    }
    global_index = 0;
  }

  void push(int s, int t) {
    global[global_index++] = Info(s, t);
    num_of_each[s]++;
    num_of_each[t]++;
  }

  void allocate() {
    for (int i = 0; i < n; i++) {
      if (num_of_each[i] > 0) {
        each[i] = new int[num_of_each[i]];
      }
    }
    Info tmp;
    for (int i = 0; i < m; i++) {
      tmp = global[i];
      each[tmp.s][each_index[tmp.s]++] = tmp.t;
      each[tmp.t][each_index[tmp.t]++] = tmp.s;
    }
  }

  void recursive(int s, int t, bool &FLG) {
    went[s] = 1;
    for (int i = 0; i < num_of_each[s]; i++) {
      if (each[s][i] == t) {
        FLG = true;
        return;
      } else if (went[each[s][i]] == 0) {
        recursive(each[s][i], t, FLG);
      }
    }
  }

  bool canAccsess(int s, int t) {
    for (int i = 0; i < n; i++)
      went[i] = 0;
    bool FLG = false;
    recursive(s, t, FLG);
    if (FLG) {
      return true;
    } else {
      return false;
    }
  }

private:
  Info *global;
  int **each;
  int *each_index;
  int *num_of_each;
  int global_index, n, m;
  int *went;
};

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

  Relation relation(n, m);
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &s, &t);
    relation.push(s, t);
  }
  relation.allocate();
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &s, &t);
    if (relation.canAccsess(s, t)) {
      printf("yes\\n");
    } else {
      printf("no\\n");
    }
  }
  return 0;
}