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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

// #include<bits/stdc++.h>

using namespace std;

int main() {

  int n;
  cin >> n;
  vector<vector<int>> t(n, vector<int>(0));
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    for (int j = 0; j < b; j++) {
      int x;
      cin >> x;
      x--;
      t[i].push_back(x);
    }
  }
  queue<int> r;
  r.push(0);
  int g[105] = {1000000};
  for (int i = 0; i < n; i++) {
    g[i] = 10000000;
  }
  g[0] = 0;
  set<int> p;
  p.insert(0);
  bool f[105] = {};
  while (r.size() || p.size() < n) {
    if (!r.size()) {
      for (int i = 0; i < n; i++) {
        if (g[i] == 10000000) {
          g[i] = -1;
          p.insert(i);
          // r.push(i);
        }
      }
    } else {
      int v = r.front();
      for (int i = 0; i < t[v].size(); i++) {
        if (!f[t[v][i]]) {
          f[t[v][i]] = 1;
          r.push(t[v][i]);
          p.insert(t[v][i]);
          g[t[v][i]] = min(g[t[v][i]], g[v] + 1);
        }
      }
      r.pop();
    }
  }
  for (int i = 0; i < n; i++)
    cout << i + 1 << " " << g[i] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

// #include<bits/stdc++.h>

using namespace std;

int main() {

  int n;
  cin >> n;
  vector<vector<int>> t(n, vector<int>(0));
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    for (int j = 0; j < b; j++) {
      int x;
      cin >> x;
      x--;
      t[i].push_back(x);
    }
  }
  queue<int> r;
  r.push(0);
  int g[105] = {1000000};
  for (int i = 0; i < n; i++) {
    g[i] = 10000000;
  }
  g[0] = 0;
  set<int> p;
  p.insert(0);
  while (r.size() || p.size() < n) {
    if (!r.size()) {
      for (int i = 0; i < n; i++) {
        if (g[i] == 10000000) {
          g[i] = -1;
          p.insert(i);
          // r.push(i);
        }
      }
    } else {
      int v = r.front();
      for (int i = 0; i < t[v].size(); i++) {
        r.push(t[v][i]);
        p.insert(t[v][i]);
        g[t[v][i]] = min(g[t[v][i]], g[v] + 1);
      }
      r.pop();
    }
  }
  for (int i = 0; i < n; i++)
    cout << i + 1 << " " << g[i] << endl;

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

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

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

  std::vector<std::vector<int>> graph;
  graph.resize(n);
  for (int i = 0; i < n; ++i) {
    int u, k, v;
    scanf("%d%d", &u, &k);
    for (int j = 0; j < k; ++j) {
      scanf("%d", &v);
      graph[u - 1].push_back(v - 1);
    }
  }

  std::queue<int> vertex;
  vertex.push(0);
  int current_distance = 0;

  int distances[100];
  for (int i = 0; i < 100; ++i)
    distances[i] = -1;

  while (vertex.size() > 0) {
    int s = vertex.size();

    // Loop for current depth
    for (int i = 0; i < s; ++i) {
      int id = vertex.front();
      vertex.pop();

      if (distances[id] == -1)
        distances[id] = current_distance;

      std::vector<int>::const_iterator itr;
      for (itr = graph[id].begin(); itr != graph[id].end(); ++itr) {
        if (distances[*itr] == -1)
          vertex.push(*itr);
      }
    }

    ++current_distance;
  }

  for (int i = 0; i < n; ++i)
    printf("%d %d\\n", i + 1, distances[i]);

  return 0;
}


Error: TLE

Fixed submission:

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

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

  std::vector<std::vector<int>> graph;
  graph.resize(n);
  for (int i = 0; i < n; ++i) {
    int u, k, v;
    scanf("%d%d", &u, &k);
    for (int j = 0; j < k; ++j) {
      scanf("%d", &v);
      graph[u - 1].push_back(v - 1);
    }
  }

  std::queue<int> vertex;
  vertex.push(0);
  int current_distance = 0;

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

  while (vertex.size() > 0) {
    int s = vertex.size();

    // Loop for current depth
    for (int i = 0; i < s; ++i) {
      int id = vertex.front();
      vertex.pop();

      if (distances[id] == -1)
        distances[id] = current_distance;

      std::vector<int>::const_iterator itr;
      for (itr = graph[id].begin(); itr != graph[id].end(); ++itr)
        vertex.push(*itr);
    }

    ++current_distance;
  }

  for (int i = 0; i < n; ++i)
    printf("%d %d\\n", i + 1, distances[i]);

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

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

struct node {
  std::vector<int> child;
  int distance;
};

int main() {
  int node_num;

  std::cin >> node_num;

  std::vector<node> n(node_num);

  for (int i = 0; i < node_num; i++) {
    int num, to, node_val;
    std::cin >> node_val >> num;
    for (int j = 0; j < num; j++) {
      std::cin >> to;
      n[node_val - 1].child.push_back(to);
    }
    n[i].distance = -1;
  }

  //???????????????????????¢0
  n[0].distance = 0;
  std::queue<int> q;
  int dist = 1;
  q.push(1);
  while (q.size() != 0) {
    int num = q.front();
    q.pop();
    for (std::vector<int>::iterator it = n[num - 1].child.begin();
         it != n[num - 1].child.end(); it++) {
      if (n[*it - 1].distance > n[num - 1].distance + 1 ||
          n[*it - 1].distance == -1) {
        n[*it - 1].distance = n[num - 1].distance + 1;
        q.push(*it);
      }
    }
  }

  for (int i = 0; i < node_num; i++) {
    std::cout << i + 1 << " " << n[i].distance << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

struct node {
  std::vector<int> child;
  int distance;
};

int main() {
  int node_num;

  std::cin >> node_num;

  std::vector<node> n(node_num);

  for (int i = 0; i < node_num; i++) {
    int num, to, node_val;
    std::cin >> node_val >> num;
    for (int j = 0; j < num; j++) {
      std::cin >> to;
      n[node_val - 1].child.push_back(to);
    }
    n[i].distance = -1;
  }

  //???????????????????????¢0
  n[0].distance = 0;
  std::queue<int> q;
  int dist = 1;
  q.push(1);
  while (q.size() != 0) {
    int num = q.front();
    q.pop();
    for (std::vector<int>::iterator it = n[num - 1].child.begin();
         it != n[num - 1].child.end(); it++) {
      q.push(*it);
      if (n[*it - 1].distance > dist || n[*it - 1].distance == -1) {
        n[*it - 1].distance = dist;
      }
    }
    dist++;
  }

  for (int i = 0; i < node_num; i++) {
    std::cout << i + 1 << " " << n[i].distance << std::endl;
  }
  return 0;
}
Problem p02239 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#define MAX 101
using namespace std;

int n;
int a[MAX][MAX] = {};
int d[MAX];

void track(int v, int depth) {
  if (d[v] > depth || d[v] == -1) {
    d[v] = depth;
  } else if (d[v] < depth) {
    return;
  }
  for (int i = 1; i <= n; i++) {
    if (a[v][i] == 1) {
      track(i, depth + 1);
    }
  }
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int v;
      cin >> v;
      a[i][v] = 1;
    }
  }

  for (int i = 1; i <= n; i++) {
    d[i] = -1;
  }

  track(1, 0);

  for (int i = 1; i <= n; i++) {
    cout << i << " " << d[i] << endl;
  }
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#define MAX 101
using namespace std;

int n;
int a[MAX][MAX] = {};
int d[MAX];

void track(int v, int depth) {
  if (d[v] > depth || d[v] == -1)
    d[v] = depth;
  for (int i = 1; i <= n; i++) {
    if (a[v][i] == 1) {
      track(i, depth + 1);
    }
  }
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int v;
      cin >> v;
      a[i][v] = 1;
    }
  }

  for (int i = 1; i <= n; i++) {
    d[i] = -1;
  }

  track(1, 0);

  for (int i = 1; i <= n; i++) {
    cout << i << " " << d[i] << endl;
  }
}
Problem p02239 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int n, m, l, p;
int a[100][100];
int D[100];
void bfs(int src) {
  queue<int> Q;
  Q.push(src);
  D[src] = 0;
  while (!Q.empty()) {
    int cur = Q.front();
    Q.pop();
    for (int dst = 0; dst < n; dst++) {
      if (D[dst] < 0 && a[cur][dst] == 1) {
        D[dst] = D[cur] + 1;
        Q.push(dst);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << i + 1 << " " << D[i] << endl;
  }
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    D[i] = -1;
    for (int j = 0; j < n; j++) {
      a[i][j] = 0;
    }
  }
  for (int i = 0; i < n; i++) {
    cin >> m;
    cin >> l;
    for (int j = 0; j < l; j++) {
      cin >> p;
      a[m - 1][p - 1] = 1;
    }
  }
  bfs(0);
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int n, m, l, p;
int a[100][100];
int D[100];
void bfs(int src) {
  queue<int> Q;
  Q.push(src);
  D[src] = 0;
  while (!Q.empty()) {
    int cur = Q.front();
    Q.pop();
    for (int dst = 0; dst < n; dst++) {
      if (D[dst] < 0 && a[cur][dst] == 1) {
        D[dst] = D[cur] + 1;
        Q.push(dst);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cerr << i + 1 << " " << D[i] << endl;
  }
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    D[i] = -1;
    for (int j = 0; j < n; j++) {
      a[i][j] = 0;
    }
  }
  for (int i = 0; i < n; i++) {
    cin >> m;
    cin >> l;
    for (int j = 0; j < l; j++) {
      cin >> p;
      a[m - 1][p - 1] = 1;
    }
  }
  bfs(0);
}
Problem p02239 (C++) — buggy submission, verdict: Runtime Error

#include <fstream>
#include <iostream>

#include <cassert>
#include <climits>

#include <string.h>

#include <deque>
#include <map>
#include <stack>
#include <vector>

enum Color {
  kWhite,
  kGray,
  kBlack,
};

int n;
int start_vertex = 0;
std::vector<std::vector<int>> matrix;
std::vector<Color> c;
std::vector<int> d;

void breadthFirstSearch() {
  for (int i = 0; i < n; ++i) {
    c.push_back(kWhite);
    d.push_back(-1);
  }

  std::deque<int> tmp_queue;
  tmp_queue.push_back(start_vertex);
  c[start_vertex] = kGray;
  d[start_vertex] = 0;

  while (!tmp_queue.empty()) {
    int current = tmp_queue.front();
    tmp_queue.pop_front();
    c[start_vertex] = kBlack;

    std::vector<int> line = matrix[current];
    for (int i = 0; i < line.size(); ++i) {
      if (line[i] > 0 && c[i] == kWhite) {
        tmp_queue.push_back(i);
        c[i] = kGray;
        d[i] = d[current] + 1;
      }
    }
  }
}

int main() {
  std::cin >> n;
  for (int i = 0; i < n; ++i) {
    std::vector<int> line;
    for (int j = 0; j < n; ++j) {
      line.push_back(0);
    }
    matrix.push_back(line);
  }

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

    std::cin >> u;
    std::cin >> k;

    for (int j = 0; j < k; ++j) {
      int tmp;
      std::cin >> tmp;
      matrix[u - 1][tmp - 1] = 1;
    }
  }

  breadthFirstSearch();

  for (int i = 0; i < n; ++i) {
    std::cout << (i + 1) << " " << d[i] << std::endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

const int MAX_N = 101;
const int DUMMY = 10000;

int N, u, k, v = 0;

vector<int> V[MAX_N];
int d[MAX_N];
bool visited[MAX_N];

bool bfs(int u, int depth) {
  for (int i = 0; i < V[u].size(); ++i) {
    if (V[u][i] == 1)
      continue;
    bfs(V[u][i] - 1, depth + 1);
  }

  d[u] = min(d[u], depth);

  if (V[u].size() == 0 || u == 0)
    return true;
}

int main() {
  // initialize
  for (int i = 0; i < MAX_N; ++i) {
    d[i] = DUMMY;
    visited[i] = false;
  }

  // input
  scanf("%d", &N);
  for (int i = 0; i < N; ++i) {
    scanf("%d %d", &u, &k);
    for (int j = 0; j < k; ++j) {
      scanf("%d", &v);
      V[i].push_back(v);
    }
    sort(V[i].begin(), V[i].end());
  }

  // serach
  bfs(1 - 1, 0);

  // output
  for (int i = 0; i < N; ++i) {
    if (d[i] == DUMMY)
      cout << i + 1 << ' ' << -1 << endl;
    else
      cout << i + 1 << ' ' << d[i] << endl;
  }

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

#include <iostream>
#include <queue>

using namespace std;
static const int N = 100;
static const int INFTY = (1 << 21);
int n, M[N][N];
int d[N];

void bfs(int s) {
  queue<int> q;
  q.push(s);
  for (int i = 0; i < n; i++) {
    d[i] = INFTY;
  }
  d[s] = 0;
  int u;
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (int v = 0; v < n; v++) {
      if (M[u][v] == 0)
        continue;
      if (d[v] != INFTY)
        continue;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
  for (int i = 0; i < n; i++) {
    cout << i + 1 << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl;
  }
}

int main() {
  int u, k, v;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      M[i][j] = 0;
  }
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    u--;
    for (int j = 0; j < k; j++) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }
  bfs(0);
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <queue>

using namespace std;
static const int N = 100;
static const int INFTY = (1 << 21);
int n, M[N][N];
int d[N];

void bfs(int s) {
  queue<int> q;
  q.push(s);
  for (int i = 0; i < n; i++) {
    d[i] = INFTY;
  }
  d[s] = 0;
  int u;
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (int v = 0; v < n; v++) {
      if (M[u][v] == 0)
        continue;
      if (d[v] != INFTY)
        continue;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
  for (int i = 0; i < n; i++) {
    cout << i + 1 << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl;
  }
}

int main() {
  int u, k, v;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++)
      M[i][j] = 0;
  }
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    u--;
    for (int j = 0; j < k; j++) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }
  bfs(0);
  return 0;
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>

void numbering(int id, int *g, int num, std::vector<int> *a) {
  int tmp;
  for (int i = 0; i < a[id].size(); i++) {
    tmp = a[id][i];
    if (g[tmp])
      continue;
    g[tmp] = num;
    numbering(tmp, g, num, a);
  }
}

void grouping(std::vector<int> *a, int *g, int n) {
  int num = 1;
  for (int i = 0; i < n; i++) {
    if (g[i])
      continue;
    g[i] = num;
    numbering(i, g, num, a);
    num++;
  }
}

int main() {
  int i, n, m, q, s, t;
  int *g;
  scanf("%d %d", &n, &m);
  g = (int *)malloc(sizeof(int) * n);
  memset(g, 0, sizeof(int) * n);
  std::vector<int> a[n];
  for (i = 0; i < m; i++) {
    scanf("%d %d", &s, &t);
    a[s].push_back(t);
    a[t].push_back(s);
  }
  scanf("%d", &q);
  grouping(a, g, n);
  for (i = 0; i < q; i++) {
    scanf("%d %d", &s, &t);
    if (g[s] == g[t])
      printf("yes\\n");
    else
      printf("no\\n");
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>

bool isFriend(std::vector<int> *a, int s, int t, int *checked, int c) {
  int tmp;
  checked[s] = c;
  if (s == t)
    return true;
  for (int i = 0; i < a[s].size(); i++) {
    tmp = a[s][i];
    if (checked[tmp] >= c)
      continue;
    if (isFriend(a, tmp, t, checked, c))
      return true;
  }
  return false;
}

int main() {
  int i, n, m, q, s, t;
  int *checked;
  scanf("%d %d", &n, &m);
  checked = (int *)malloc(sizeof(int) * n);
  memset(checked, 0, sizeof(int) * n);
  std::vector<int> a[n];
  for (i = 0; i < m; i++) {
    scanf("%d %d", &s, &t);
    a[s].push_back(t);
    a[t].push_back(s);
  }
  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    scanf("%d %d", &s, &t);
    if (isFriend(a, s, t, checked, i + 1))
      printf("yes\\n");
    else
      printf("no\\n");
  }
  return 0;
}
Problem p02240 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
const int INF = 1e9;

using namespace std;
int n, m;
struct node {
  int id;
  int parent;
};

node person[100000];
vector<int> path;

int find(node x) {
  if (x.parent == -1) {
    REP(i, path.size())
    person[path[i]].parent = x.id;
    path.clear();
    return x.id;
  } else {
    path.push_back(x.id);
    return find(person[x.parent]);
  }
}

void marge(node x, node y) {
  int x_id = find(x);
  int y_id = find(y);
  if (x_id == y_id)
    return;
  else
    person[y_id].parent = x_id;
  return;
}

bool judge(node x, node y) {
  vector<int> xpath, ypath;
  return find(x) == find(y);
}

int main() {
  cin >> n >> m;
  REP(i, n) {
    person[i].id = i;
    person[i].parent = -1;
  }
  REP(i, m) {
    int s, t;
    cin >> s >> t;
    if (person[t].parent == -1)
      person[t].parent = person[s].id;
    else if (person[s].parent == -1)
      person[s].parent = person[t].id;
    else
      marge(person[s], person[t]);
  }
  int q;
  cin >> q;
  REP(i, q) {
    int s, t;
    cin >> s >> t;
    if (judge(person[s], person[t]))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
const int INF = 1e9;

using namespace std;
int n, m;
struct node {
  int id;
  int parent;
};

node person[100000];

int find(node x, vector<int> path) {
  if (x.parent == -1) {
    REP(i, path.size())
    person[path[i]].parent = x.id;
    return x.id;
  } else {
    path.push_back(x.id);
    return find(person[x.parent], path);
  }
}

void marge(node x, node y) {
  vector<int> xpath, ypath;
  int x_id = find(x, xpath);
  int y_id = find(y, ypath);
  if (x_id == y_id)
    return;
  else
    person[y_id].parent = x_id;
  return;
}

bool judge(node x, node y) {
  vector<int> xpath, ypath;
  return find(x, xpath) == find(y, ypath);
}

int main() {
  cin >> n >> m;
  REP(i, n) {
    person[i].id = i;
    person[i].parent = -1;
  }
  REP(i, m) {
    int s, t;
    cin >> s >> t;
    if (person[t].parent == -1)
      person[t].parent = person[s].id;
    else if (person[s].parent == -1)
      person[s].parent = person[t].id;
    else
      marge(person[s], person[t]);
  }
  int q;
  cin >> q;
  REP(i, q) {
    int s, t;
    cin >> s >> t;
    if (judge(person[s], person[t]))
      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;
#define MAX 100000
#define 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 assignColor() {
  int id = 1;
  for (int i = 0; i < n; i++) {
    color[i] = NIL;
  }
  for (int u = 0; u < n; u++) {
    if (color[u] == NIL) {
      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 <string>
#include <vector>
using namespace std;
#define N 100000
#define WHITE 0
#define GRAY 1
#define BLACK 2

vector<int> G[100];
int color[N], num_members;
int cnt[N];

string dfs(int r, int target) {
  stack<int> S;
  S.push(r);
  color[r] = GRAY;

  while (!S.empty()) {
    int u = S.top();
    int v = G[u][cnt[u]];
    if (u == target) {
      return "yes";
    }
    cnt[u]++;

    if (v != -1) {
      if (color[v] == WHITE) {
        color[v] = GRAY;
        S.push(v);
      }
    } else {
      S.pop();
      color[u] = BLACK;
    }
  }

  return "no";
}

int main() {
  int num_relations, from_id, to_id;
  int num_tests, test_from_id, test_to_id;

  cin >> num_members >> num_relations;
  for (int i = 0; i < num_relations; i++) {
    cin >> from_id >> to_id;
    G[from_id].push_back(to_id);
    G[to_id].push_back(from_id);
  }
  // 最後に番兵的な何かを仕込んでおく
  for (int i = 0; i < num_members; i++) {
    G[i].push_back(-1);
  }

  cin >> num_tests;
  for (int i = 0; i < num_tests; i++) {
    for (int j = 0; j < num_members; j++) {
      color[j] = WHITE;
      cnt[j] = 0;
    }
    cin >> test_from_id >> test_to_id;
    cout << dfs(test_from_id, test_to_id) << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define MAX 100005
#define INFTY -1

int n, m, st[MAX];
vector<vector<int>> G, C;

void bfs(int s) {
  if (st[s] != 0)
    return;
  vector<int> ve;
  queue<int> Q;
  Q.push(s);
  st[s] = 1;
  ve.push_back(s);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int i = 0; i < G[u].size(); i++) {
      int v = G[u][i];
      if (st[v] != 0)
        continue;
      Q.push(v);
      st[v] = 1;
      ve.push_back(v);
    }
  }
  C.push_back(ve);
}

void solution() {
  int i;
  for (i = 0; i < n; i++) {
    bfs(i);
  }
  for (i = 0; i < C.size(); i++) {
    sort(C[i].begin(), C[i].end());
  }
}

int main(void) {
  int i, j, a, b, q;
  for (i = 0; i < MAX; i++) {
    st[i] = 0;
  }
  cin >> n >> m;
  G.resize(n);
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  solution();
  cin >> q;
  while (q--) {
    bool flag = false;
    cin >> a >> b;
    for (i = 0; i < C.size(); i++) {
      if (*(lower_bound(C[i].begin(), C[i].end(), a)) != a)
        continue;
      if (*(lower_bound(C[i].begin(), C[i].end(), b)) != b)
        continue;
      flag = true;
    }
    if (flag)
      cout << "yes\\n";
    else
      cout << "no\\n";
  }
}


Error: TLE

Fixed submission:

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

vector<vector<int>> G, R;
vector<int> ve;
queue<int> Q;
int choose[MAX];
int n, m, i, j;

int main(void) {
  cin >> n >> m;
  G.resize(n);
  int a, b;
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  for (i = 0; i < n; i++) {
    choose[i] = 0;
  }
  for (i = 0; i < n; i++) {
    if (choose[i] != 0)
      continue;
    Q.push(i);
    ve.clear();
    ve.push_back(i);
    choose[i] = 1;
    int now, cnt, next;
    while (!Q.empty()) {
      now = Q.front();
      Q.pop();
      for (cnt = 0; cnt < G[now].size(); cnt++) {
        next = G[now][cnt];
        if (choose[next] == 0) {
          Q.push(next);
          ve.push_back(next);
          choose[next] = 1;
        }
      }
    }
    R.push_back(ve);
    int dis = R.size() - 1;
    sort(R[dis].begin(), R[dis].end());
  }
  cin >> m;
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    for (j = 0; j < R.size(); j++) {
      if (*(lower_bound(R[j].begin(), R[j].end(), a)) == a) {
        if (*(lower_bound(R[j].begin(), R[j].end(), b)) == b)
          cout << "yes" << endl;
        else
          cout << "no" << endl;
      }
    }
  }
}