alexjercan/bugnet huggingface dataset:
Problem p02240 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
#define maxn 100005
#define NIL -1

int n, m;
vector<int> G[maxn]; // 邻接表
int color[maxn]; // 编号（可以互相链接的两个节点编号相同）

void dfs(int r, int c) // 给节点r分配编号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 dfs_(int r, int c) // 给节点r分配编号c（深搜实现）（递归）
{
  color[r] = c;
  for (int i = 0; i < G[r].size(); i++) // 注意这里代码于上面的不同
  {
    int v = G[r][i];
    if (color[v] == NIL) {
      color[v] = c;
      dfs_(v, c);
    }
  }
}

void bfs(int r, int c) // 给节点r分配编号c（广搜实现）
{
  queue<int> S;
  S.push(r);
  color[r] = c;
  while (!S.empty()) {
    int u = S.front();
    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;
  memset(color, NIL, sizeof(color)); // 这里要记得初始化
  for (int u = 0; u < n; u++)
    if (color[u] == NIL)
      dfs_(u, id++); // 这里选取了广搜的方式给各个节点编号
}

int main() {
  int s, t, q;
  cin >> n >> m;
  while (m--) {
    cin >> s >> t;
    G[s].push_back(t); // 从顶点s向t画边
    G[t].push_back(s); // 反过来连边
  }
  assigncolor();
  cin >> q;
  while (q--) {
    cin >> s >> t;
    if (color[s] == color[t])
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
#include <vector>

#include <stdio.h>

#include <queue>
using namespace std;
int main() {
  int person_number, relation_number;

  cin >> person_number >> relation_number;

  vector<vector<int>> list(person_number);
  for (int i = 0; i < relation_number; i++) {
    int start, end;
    scanf("%d %d ", &start, end);
    int flag = 0;
    for (auto n : list[start]) {
      if (n == end)
        flag = 1;
    }
    if (flag == 0) {
      list[start].push_back(end);
      list[end].push_back(start);
    }
  }

  int test_number;
  scanf("%d", &test_number);
  for (int i = 0; i < test_number; i++) {
    int from, to;
    scanf("%d %d ", &from, &to);
    queue<int> q;
    q.push(from);
    vector<int> ban_list;
    for (int i = 0; i < person_number; i++) {
      ban_list.push_back(0);
    }
    ban_list[from] = 1;
    for (; !q.empty(); q.pop()) {
      int flag = 0;
      for (auto n : list[q.front()]) {
        if (n == to) {
          flag = 1;
          break;
        }
        if (ban_list[n] == 0) {
          ban_list[n] = 1;
          q.push(n);
        }
      }
      if (flag == 1)
        break;
    }
    if (!q.empty())
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}
Problem p02240 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
// cout<<setprecision(1)<<fixed<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N = 100000;
const int INF = 0x3f3f3f3f;
#define eps 1e-7
// #define Kongxiangzhouye
// int n,m;
// int e;
// int begin[301010];
// int next[301010];
// int to[301010];

int par[101010];

int find(int x) {
  if (x != par[x])
    par[x] = find(par[x]);
  return par[x];
}

int unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x != y)
    par[y] = x;
}

void init(int n) {
  for (int i = 0; i <= n; i++) {
    par[i] = i;
  }
}
int main() {
  ios::sync_with_stdio(false);
#ifdef Kongxiangzhouye
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  int n, m, q;
  while (cin >> n >> m) {
    init(n);
    for (int i = 0; i < m; i++) {
      int a, b;
      cin >> a >> b;
      unite(a, b);
    }
    cin >> q;
    for (int i = 0; i < q; i++) {
      int a, b;
      cin >> a >> b;
      if (find(a) == find(b))
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

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

using namespace std;

#define MAX 10000

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] == -1) {
        color[v] = c;
        S.push(v);
      }
    }
  }
}

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

int main(int argc, char const *argv[]) {
  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 <iostream>
#include <vector>

#define MAX 100000

using namespace std;

int visited[MAX];
// adjacent list
vector<int> G[MAX];

// can go from s to t or not
int color[MAX];

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

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }

  // coloring
  for (int i = 0; i < n; i++) {
    visited[i] = 0;
    color[i] = -1;
  }
  int c = 0;
  for (int i = 0; i < n; i++) {
    if (color[i] == -1) {
      dfs(i, c);
      c++;
    }
  }

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

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

#define MAX 100000

using namespace std;

int visited[MAX];
// adjacent list
vector<int> G[MAX];

// can go from s to t or not
int color[MAX];

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

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    color[i] = -1;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    for (int j = 0; j < n; j++)
      visited[j] = 0;
    int s, t;
    cin >> s >> t;
    dfs(s, i);
    if (color[s] == color[t])
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define nil -1
vector<vector<int>> makelist(int n, int m) {
  vector<vector<int>> Adjlist(n);
  int a, b;

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

  return Adjlist;
}

void dfs_run(int start, int id, vector<vector<int>> &adj, vector<int> &color) {
  // cout << start << endl;
  while (!adj[start].empty()) {
    int tmp;
    tmp = adj[start].back();
    adj[start].pop_back();
    if (color[tmp] != nil) {
      continue;
    }
    color[tmp] = id;
    dfs_run(tmp, id, adj, color);
  }
}

void dfs(int n, vector<vector<int>> &adj, vector<int> &color) {
  int id = 0;
  for (int i = 0; i < n; i++) {
    if (color[i] == nil) {
      color[i] = i;
      dfs_run(i, id, adj, color);
    }
    id++;
  }
  /*for(int i = 0; i < n; i++) {
          cout << color[i] << endl;
  }*/
}

int main(void) {
  int n, m, q;

  cin >> n >> m;
  vector<vector<int>> Adjlist = makelist(n, m);
  vector<int> color(n, nil);

  dfs(n, Adjlist, color);
  cin >> q;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;

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

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
bool frag;

vector<vector<int>> makelist(int n, int m) {
  vector<vector<int>> Adjlist(n);
  int a, b;

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

  return Adjlist;
}

void dfs_run(int a, int b, vector<vector<int>> &adj, vector<bool> &checker) {
  // cout << a << endl;
  while (!adj[a].empty()) {
    int tmp;
    tmp = adj[a].back();
    adj[a].pop_back();
    if (checker[tmp] == false) {
      continue;
    }
    checker[tmp] = false;
    if (tmp == b) {
      frag = true;
    } else {
      if (frag == false) {
        dfs_run(tmp, b, adj, checker);
      }
    }
  }
}

bool dfs(int n, int a, int b, vector<vector<int>> adj) {
  vector<bool> checker(n, true);

  checker[a] = false;
  dfs_run(a, b, adj, checker);
  return frag;
}

int main(void) {
  int n, m, q;

  cin >> n >> m;
  vector<vector<int>> Adjlist = makelist(n, m);

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

    frag = false;
    if (dfs(n, a, b, Adjlist)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

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

#include <iostream>
#include <stack>
#include <vector>
using namespace std;
static const int MAX = 100000;
int i, j, v, num, color[MAX];
vector<int> list[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 (i = 0; i < list[u].size(); i++) {
      v = list[u][i];
      if (color[v] == -1) {
        color[v] = c;
        S.push(v);
      }
    }
  }
}

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

int main() {
  int a, b, trial, start, goal, kaisuu;
  cin >> num >> kaisuu;

  for (i = 0; i < kaisuu; i++) {
    cin >> a >> b;
    list[a].push_back(b);
    list[b].push_back(a);
  }

  assigncolor();

  cin >> trial;

  for (i = 1; i <= trial; i++) {
    cin >> start >> goal;
    if (color[start] == color[goal]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int i, j, v, num, color[100000];
vector<int> list[100000];

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 (i = 0; i < list[u].size(); i++) {
      v = list[u][i];
      if (color[v] == -1) {
        color[v] = c;
        S.push(v);
      }
    }
  }
}

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

int main() {
  int a, b, trial, start, goal, kaisuu;
  cin >> num >> kaisuu;

  for (i = 0; i < kaisuu; i++) {
    cin >> a >> b;
    list[a].push_back(b);
    list[b].push_back(a);
  }

  assigncolor();

  cin >> trial;

  for (i = 1; i <= trial; i++) {
    cin >> start >> goal;
    if (color[start] == color[goal]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

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

#include <climits>
#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_color() {
  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);
  }
  assign_color();
  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 <climits>
#include <iostream>
#include <stack>
#include <vector>

using namespace std;
static const int MAX = 100;
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_color() {
  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);
  }
  assign_color();
  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 <iostream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, j, n) for (int i = j; i < n; i++)
#define NOT_FOUND 0
using namespace std;

vector<int> graph[100001];
int memo[100001];

int dfs(int start, int label) {
  stack<int> stack;
  stack.push(start);
  memo[start] = label;

  while (!stack.empty()) {
    int v = stack.top();
    stack.pop();
    vector<int> edges = graph[v];

    rep(i, edges.size()) {
      int next = edges[i];
      if (memo[next] == 0) {
        memo[next] = label;
        stack.push(next);
      }
    }
  }
  return 1;
}

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

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

  memset(memo, 0, sizeof(memo));
  int id = 1;
  rep(i, n) {
    if (memo[i] == 0) {
      dfs(i, id++);
    }
  }

  int q;
  cin >> q;
  rep(i, q) {
    int s, d;
    cin >> s >> d;
    if (memo[s] == memo[d]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define NOT_FOUND 0
#define ENQUEUED 1
using namespace std;

vector<int> graph[100001];
int memo[100001];

int dfs(int start, int end) {
  if (start == end) {
    return 1;
  }
  memset(memo, NOT_FOUND, sizeof(memo));
  stack<int> stack;
  stack.push(start);
  memo[start] = ENQUEUED;

  while (!stack.empty()) {
    int v = stack.top();
    stack.pop();
    vector<int> edges = graph[v];

    rep(i, edges.size()) {
      int next = edges[i];
      if (next == end) {
        return 1;
      }
      if (memo[next] == NOT_FOUND) {
        memo[next] = ENQUEUED;
        stack.push(next);
      }
    }
  }
  return 0;
}

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

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

  int q;
  cin >> q;

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

#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// #include "../cxx-prettyprint/prettyprint.hpp"

std::vector<std::vector<int>> Adj;
std::unordered_set<int> nodes_unvisited;
std::unordered_map<int, int> node_component_map;

void mark_nodes(int node, int component_id) {
  node_component_map[node] = component_id;
  nodes_unvisited.erase(node);
  for (int &next : Adj[node]) {
    if (nodes_unvisited.find(next) != nodes_unvisited.end()) {
      mark_nodes(next, component_id);
    }
  }
}

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

  Adj.assign(n, std::vector<int>());
  for (int i = 0; i < m; i++) {
    int s, t;
    std::cin >> s >> t;
    Adj[s].push_back(t);
    Adj[t].push_back(s);
  }

  for (int i = 0; i < n; i++) {
    nodes_unvisited.insert(i);
  }

  int component_id = 0;
  while (!nodes_unvisited.empty()) {
    int start = *nodes_unvisited.begin();
    mark_nodes(start, component_id);
    component_id++;
  }

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <unordered_set>
#include <vector>
// #include "../cxx-prettyprint/prettyprint.hpp"

std::vector<std::vector<int>> Adj;
std::unordered_set<int> nodes_visited;

bool is_reachable(int s, int t) {
  if (std::find(Adj[s].begin(), Adj[s].end(), t) != Adj[s].end()) {
    return true;
  } else if (nodes_visited.find(s) != nodes_visited.end()) {
    return false;
  } else {
    nodes_visited.insert(s);
    bool value = std::any_of(Adj[s].begin(), Adj[s].end(),
                             [&](int next) { return is_reachable(next, t); });
    return value;
  }
}

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

  Adj.assign(n, std::vector<int>());
  for (int i = 0; i < m; i++) {
    int s, t;
    std::cin >> s >> t;
    Adj[s].push_back(t);
    Adj[t].push_back(s);
  }

  int q;
  std::cin >> q;
  for (int i = 0; i < q; i++) {
    int s, t;
    std::cin >> s >> t;
    std::cout << (is_reachable(s, t) ? "yes" : "no") << std::endl;
    nodes_visited.clear();
  }
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void color(std::vector<std::vector<int>> n) {
  std::vector<int> color(n.size(), 0);
  std::stack<int> s;

  int count = 1;
  for (size_t i = 0; i < n.size(); i++) {
    if (color[i] != 0)
      continue;
    color[i] = count;
    s.push(i);
    while (s.size() != 0) {
      int num = s.top();
      s.pop();
      for (std::vector<int>::iterator it = n[num].begin(); it != n[num].end();
           it++) {
        if (color[*it] == 0) {
          s.push(*it);
          color[*it] = count;
        }
      }
    }
    count++;
  }

  int question;
  std::cin >> question;
  for (int i = 0; i < question; i++) {
    int from, to;
    std::cin >> from >> to;
    if (color[from] == color[to]) {
      std::cout << "yes" << std::endl;
    } else {
      std::cout << "no" << std::endl;
    }
  }
}

int main() {
  int people, relation;

  std::cin >> people >> relation;

  std::vector<std::vector<int>> n(people);

  for (int i = 0; i < relation; i++) {
    int from, to;
    std::cin >> from >> to;
    n[from].push_back(to);
    n[to].push_back(from);
  }

  color(n);

  return 0;
}

Error: TLE

Fixed submission:

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

bool dfs(std::vector<std::vector<int>> n, int from, int to) {
  std::stack<int> s;
  std::vector<bool> went(n.size(), false);

  s.push(from);
  while (s.size() != 0) {
    int num = s.top();
    s.pop();
    for (std::vector<int>::iterator it = n[num].begin(); it != n[num].end();
         it++) {
      if (*it == to)
        return true;
      if (!went[*it]) {
        s.push(*it);
        went[*it] = true;
      }
    }
  }
  return false;
}

int main() {
  int people, relation;

  std::cin >> people >> relation;

  std::vector<std::vector<int>> n(people);

  for (int i = 0; i < relation; i++) {
    int from, to;
    std::cin >> from >> to;
    n[from].push_back(to);
    n[to].push_back(from);
  }

  int question;
  std::cin >> question;
  for (int i = 0; i < question; i++) {
    int from, to;
    std::cin >> from >> to;
    if (dfs(n, from, to)) {
      std::cout << "yes" << std::endl;
    } else {
      std::cout << "no" << std::endl;
    }
  }

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

#include <stdio.h>
int ancestor[100000];
int find(int x) {
  if (ancestor[x] == x)
    return x;
  return ancestor[x] = find(ancestor[x]);
}
int min(int a, int b) {
  if (a < b)
    return a;
  return b;
}
int max(int a, int b) {
  if (a > b)
    return a;
  return b;
}
int main() {
  int n, q, m, temp, x, y;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++)
    ancestor[i] = i;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    temp = find(x);
    q = find(y);
    ancestor[max(temp, q)] = min(temp, q);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d%d", &x, &y);
    if (find(x) != find(y))
      printf("no\\n");
    else
      printf("yes\\n");
  }
}

Error: 0

Fixed submission:

#include <stdio.h>
int ancestor[10];
int find(int x) {
  if (ancestor[x] == x)
    return x;
  return ancestor[x] = find(ancestor[x]);
}
int min(int a, int b) {
  if (a < b)
    return a;
  return b;
}
int max(int a, int b) {
  if (a > b)
    return a;
  return b;
}
int main() {
  int n, q, m, temp, x, y;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++)
    ancestor[i] = i;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    temp = find(x);
    q = find(y);
    ancestor[max(temp, q)] = min(temp, q);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d%d", &x, &y);
    if (find(x) != find(y))
      printf("no\\n");
    else
      printf("yes\\n");
  }
}