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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

class Friend {
public:
  int solve();
  void setGroup(vector<vector<int>> &adj, vector<int> &group, int u, int grpid);
};

void Friend::setGroup(vector<vector<int>> &adj, vector<int> &group, int u,
                      int grpid) {
  if (group[u])
    return;

  group[u] = grpid;

  int n = adj[u].size();

  for (auto i = 0; i < n; ++i) {
    int v = adj[u][i];
    setGroup(adj, group, v, grpid);
  }

  return;
}

int Friend::solve() {
  int n, m;
  cin >> n >> m;

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

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

  int grpid = 1;

  vector<int> group(n, 0);

  for (auto i = 0; i < n; ++i) {
    if (group[i] == 0) {
      setGroup(adj, group, i, grpid++);
    }
  }

  int q;
  cin >> q;

  for (auto i = 0; i < q; ++i) {
    int u, v;
    cin >> u >> v;

    if (group[u] == group[v])
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }

  return 0;
}

int main() {
  Friend f;

  f.solve();

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

class Friend {
public:
  vector<int> v;
  int solve();
  bool isFriend(vector<vector<int>> &adj, vector<int> &visited, int u, int v);
};

bool Friend::isFriend(vector<vector<int>> &adj, vector<int> &visited, int u,
                      int v) {
  if (u == v)
    return true;

  if (visited[u])
    return false;

  visited[u] = 1;

  int n = adj[u].size();

  for (auto i = 0; i < n; ++i) {
    if (isFriend(adj, visited, adj[u][i], v)) {
      adj[adj[u][i]].push_back(v);
      adj[v].push_back(adj[u][i]);
      return true;
    }
  }

  return false;
}

int Friend::solve() {
  int n, m;
  cin >> n >> m;

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

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

  int q;
  cin >> q;

  for (auto i = 0; i < q; ++i) {
    int u, v;
    cin >> u >> v;
    vector<int> visited(n, 0);

    if (isFriend(adj, visited, u, v))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }

  return 0;
}

int main() {
  Friend f;

  f.solve();

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

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
  int n, m, t1, t2, c[100000] = {};
  vector<int> g[100000];
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> t1 >> t2;
    g[t1].push_back(t2);
    g[t2].push_back(t1);
  }
  int l;
  cin >> l;
  for (int i = 0; i < n; i++)
    if (!c[i]) {
      queue<int> q;
      bool f[100000] = {};
      q.push(i);
      f[i] = true;
      c[i] = i + 1;
      for (; !q.empty();) {
        for (int j = 0; j < g[q.front()].size(); j++) {
          int v = g[q.front()][j];
          if (!f[v]) {
            f[v] = true;
            c[v] = i + 1;
            q.push(v);
          }
        }
        q.pop();
      }
    }
  for (int i = 0; i < l; i++) {
    cin >> t1 >> t2;
    if (c[t1] == c[t2])
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
using namespace std;
int main() {
  int n, m, t1, t2, c[100000] = {};
  map<int, bool> g[100000];
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> t1 >> t2;
    g[t1][t2] = 1;
    g[t2][t1] = 1;
  }
  int l;
  cin >> l;
  for (int i = 0; i < l; i++) {
    queue<int> q;
    bool f[100000] = {}, find = false;
    cin >> t1 >> t2;
    if (c[t1] && c[t2]) {
      if (c[t1] == c[t2])
        cout << "yes" << endl;
      else
        cout << "no" << endl;
      continue;
    }
    q.push(t1);
    f[t1] = true;
    c[t1] = i + 1;
    for (; !q.empty();) {
      for (int j = 0; j < n; j++)
        if (g[q.front()][j] && !f[j]) {
          if (j == t2)
            find = true;
          f[j] = true;
          c[j] = i + 1;
          q.push(j);
        }
      q.pop();
    }
    if (find)
      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, color[MAX];
vector<int> G[MAX];

void dfs(int r, int c) {
  stack<int> S;
  int u, i, v;

  S.push(r);
  color[r] = c;

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

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

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

  assignColor();

  cin >> q;

  for (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, color[MAX];
vector<int> G[MAX];

void dfs(int r, int c) {
  stack<int> S;
  int u, i, v;

  S.push(r);
  color[r] = c;

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

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

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

  assignColor();

  cin >> q;

  for (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

#ifdef LOCAL_BUILD
#include "pch.h"
#define DLOG(msg) cout << "#" << __LINE__ << ":" << msg << endl;
#define DLOG_V(var)                                                            \\
  cout << "#" << __LINE__ << ":" << #var << " : " << var << endl;
#else
#include <bits/stdc++.h>
#define DLOG(msg)
#define DLOG_V(var)
#endif
using namespace std;

struct Solver {
  explicit Solver(int n) {
    parent_index_.reserve(n);
    for (int i = 0; i < n; ++i) {
      parent_index_.push_back(i);
    }
  }
  void register_friend(int s, int t) {
    auto r1 = get_root(s);
    auto r2 = get_root(t);
    if (r1 != r2) {
      parent_index_[r2] = r1;
    }
  }
  bool can_reach(int s, int t) { return get_root(s) == get_root(t); }

private:
  int get_root(int i) {
    for (auto cur = i;;) {
      auto next = parent_index_[cur];
      if (next == cur) {
        return cur;
      }
      parent_index_[cur] = parent_index_[next];
      cur = next;
    }
  }

  vector<int> parent_index_;
};

int main() {
  int n, m;
  cin >> n >> m;
  Solver sol(n);
  for (int i = 0; i < m; ++i) {
    int s, t;
    cin >> s >> t;
    sol.register_friend(s, t);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int s, t;
    cin >> s >> t;
    auto ok = sol.can_reach(s, t);
    cout << (ok ? "yes" : "no") << endl;
  }
}

Error: TLE

Fixed submission:

#ifdef LOCAL_BUILD
#include "pch.h"
#define DLOG(msg) cout << "#" << __LINE__ << ":" << msg << endl;
#define DLOG_V(var)                                                            \\
  cout << "#" << __LINE__ << ":" << #var << " : " << var << endl;
#else
#include <bits/stdc++.h>
#define DLOG(msg)
#define DLOG_V(var)
#endif
using namespace std;

struct Solver {
  explicit Solver(int n) : connected_(n, vector<bool>(n, false)) {}
  void register_friend(int s, int t) {
    connected_[s][t] = connected_[t][s] = true;
  }
  bool can_reach(int s, int t) {
    if (s == t)
      return true;
    vector<bool> done(connected_.size(), false);
    queue<int> q;
    q.push(s);
    while (!q.empty()) {
      auto v = q.front();
      q.pop();
      if (v == t)
        return true;
      if (done[v])
        continue;
      done[v] = true;
      auto &con = connected_[v];
      for (size_t i = 0; i < con.size(); ++i) {
        if (con[i]) {
          auto v2 = static_cast<int>(i);
          if (v2 == t)
            return true;
          if (!done[v2]) {
            q.push(v2);
          }
        }
      }
    }
    return false;
  }

private:
  vector<vector<bool>> connected_;
};

int main() {
  int n, m;
  cin >> n >> m;
  Solver sol(n);
  for (int i = 0; i < m; ++i) {
    int s, t;
    cin >> s >> t;
    sol.register_friend(s, t);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int s, t;
    cin >> s >> t;
    auto ok = sol.can_reach(s, t);
    cout << (ok ? "yes" : "no") << endl;
  }
}
Problem p02240 (C++) — buggy submission, verdict: Runtime Error

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define MAX (int)1e5
#define NIL -1
int flag = 0;
vector<int> G[MAX];
queue<int> qu;
int vis[MAX];
int n;
int ind, tmp;

void bfs(int u, int k) {
  qu.push(u);
  vis[u] = k;
  while (not qu.empty()) {
    int u = qu.front();
    qu.pop();
    for (int j = 0; j < G[u].size(); j++) {
      int v = G[u][j];
      if (vis[v] == NIL) {
        vis[v] = k;
        qu.push(v);
      }
    }
  }
}

void calc() {
  int id = 1;
  for (int i = 0; i < n; i++)
    vis[i] = NIL;
  for (int i = 0; i < n; i++) {
    if (vis[i] == NIL)
      bfs(i, id++);
  }
}

int main() {
  int m, q;
  cin >> n >> m;
  bool flag = 0;
  for (int i = 0; i < m; i++) {
    cin >> ind >> tmp;
    G[ind].push_back(tmp);
    G[tmp].push_back(ind);
  }
  calc();
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> ind >> tmp;
    if (vis[ind] == vis[tmp])
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define MAX (int)1e5

vector<int> G[MAX];
queue<int> qu;

int main() {
  int n, m, q;
  int ind, tmp;
  cin >> n >> m;
  bool flag = 0;
  for (int i = 0; i < m; i++) {
    cin >> ind >> tmp;
    G[ind].push_back(tmp);
    G[tmp].push_back(ind);
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> ind >> tmp;
    flag = 0;
    qu.push(ind);
    while (not qu.empty()) {
      int u = qu.front();
      qu.pop();
      if (u == tmp) {
        flag = 1;
        break;
      }
      for (int j = 0; j < G[u].size(); j++) {
        qu.push(G[u][j]);
      }
    }
    if (flag)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
}
Problem p02240 (C++) — buggy submission, verdict: Memory Limit Exceeded

#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 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: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
#define MAX 100000

struct Node {
  int group;
  queue<int> fr;
};

int N, M, Q, group_num = 0;
Node person[MAX];

void isFriend(int a, int b) {
  if (person[a].group < 0 && person[b].group < 0) {
    int p = a, next, m;
    queue<int> move;
    group_num++;
    person[p].group = group_num;
    // cout << a << " is group " << person[a].group << endl;
    move.push(p);

    while (!move.empty()) {
      p = move.front();
      if (person[p].fr.empty()) {
        move.pop();
      } else {
        m = person[p].fr.size();
        while (m--) {
          next = person[p].fr.front();
          person[p].fr.pop();
          if (person[next].group < 0) {
            person[next].group = group_num;
            // cout << next << " is group " << person[next].group << endl;
            move.push(next);
          }
        }
        move.pop();
      }
    }

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

int main() {
  cin >> N >> M;
  for (int i = 0; i < N; i++)
    person[i].group = -1;
  while (M--) {
    int a, b;
    cin >> a >> b;
    person[a].fr.push(b);
    person[b].fr.push(a);
  }
  cin >> Q;
  while (Q--) {
    int a, b;
    cin >> a >> b;
    isFriend(a, b);
  }
  return 0;
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#define N 100000

using namespace std;
vector<int> v[N];
int group[N] = {};
int g = 0;

bool search(int s, int t) {
  if (group[s] == group[t] && group[s] > 0)
    return true;
  if (group[s] > 0)
    return false;
  group[s] = ++g;
  queue<int> q;
  q.push(s);
  while (!q.empty()) {
    int a = q.front();
    for (auto i = v[a].begin(); i != v[a].end(); i++) {
      if (group[*i] == 0) {
        group[*i] = g;
        q.push(*i);
      }
    }
    q.pop();
  }
  if (group[s] == group[t] && group[s] > 0) {
    return true;
  } else {
    return false;
  }
}

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

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

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#define MAX 100000

using namespace std;

int main() {
  int a[MAX] = {};
  int n, m;
  cin >> n >> m;
  int s, t;
  int group = 1;
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &s, &t);
    if (a[s] == 0 && a[t] == 0) {
      a[s] = a[t] = group;
      group++;
    } else if (a[s] == 0) {
      a[s] = a[t];
    } else if (a[t] == 0) {
      a[t] = a[s];
    } else {
      int tmp = a[t];
      a[t] = a[s];
      for (int j = 0; j < n; j++) {
        if (a[j] == tmp)
          a[j] = a[s];
      }
    }
  }

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

#include <iostream>
#include <vector>
#define N_MAX 100000
using namespace std;

vector<int> sns[N_MAX];
int *group;
int g = 0;

void dfs(int s) {
  if (group[s] != 0)
    return;
  group[s] = g;

  int len = sns[s].size();
  for (int i = 0; i < len; i++) {
    dfs(sns[s][i]);
  }
}

int main() {
  int n, m, q, s, t;
  cin >> n >> m;

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

    sns[s].push_back(t);
    sns[t].push_back(s);
  }

  // dfs
  group = new int[n];
  for (int i = 0; i < n; i++) {
    if (group[i] == 0) {
      g++;
      dfs(i);
    }
  }

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

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
#define eol '\\n'
using namespace std;

vector<int> group;

void bfs(int v, int g, vector<vector<int>> list) {
  if (group[v] != 0) {
    return;
  }
  group[v] = g;
  for (int i = 0; i < list[v].size(); i++) {
    bfs(list[v][i], g, list);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

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

  vector<vector<int>> list(n, vector<int>());
  group = vector<int>(n, 0);

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

  int g = 1;
  for (int i = 0; i < n; i++) {
    if (group[i] == 0) {
      bfs(i, g, list);
      g++;
    }
  }

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

/*
 * ALDS1_11_D.cpp
 *
 *  Created on: May 8, 2018
 *      Author: 13743
 */

#include <iostream>
#include <vector>

using namespace std;

int visited[100000];
int la = 1;

void dfsRec(vector<int> *a, int v) {
  if (visited[v] != 0)
    return;
  visited[v] = la;
  for (int &x : a[v]) {
    dfsRec(a, x);
  }
}

int main() {
  int n, m;
  vector<int> adj[100000];

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

  for (int i = 0; i < n; i++) {
    if (visited[i] == 0) {
      dfsRec(adj, i);
      la++;
    }
  }

  int q;
  cin >> q;
  while (q--) {
    cin >> u >> v;
    (visited[u] == visited[v]) ? printf("yes\\n") : printf("no\\n");
  }
}


Error: TLE

Fixed submission:

/*
 * ALDS1_11_D.cpp
 *
 *  Created on: May 8, 2018
 *      Author: 13743
 */

#include <iostream>
#include <vector>

using namespace std;

bool visited[100000];

void dfsRec(vector<int> *a, int v) {
  if (visited[v])
    return;
  visited[v] = 1;
  for (int &x : a[v]) {
    dfsRec(a, x);
  }
}

int main() {
  int n, m;
  vector<int> adj[100000];

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

  int q;
  cin >> q;
  while (q--) {
    for (int i = 0; i < n; i++) {
      visited[i] = 0;
    }
    cin >> u >> v;
    dfsRec(adj, u);
    (visited[v]) ? printf("yes\\n") : printf("no\\n");
  }
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

struct UnionFindTree {
  size_t n;
  vector<int> tree;
  UnionFindTree(size_t n) : n(n) { tree = vector<int>(n, -1); }
  int find_root(size_t v) {
    if (tree[v] < 0)
      return v;
    return tree[v] = find_root(tree[v]);
  }
  bool unite(size_t x, size_t y) {
    x = find_root(x);
    y = find_root(y);
    if (x == y)
      return false;
    if (rank(x) < rank(y))
      swap(x, y);
    tree[x] += tree[y];
    tree[y] = x;
    return true;
  }
  bool has_same_root(size_t x, size_t y) {
    return find_root(x) == find_root(y);
  }
  size_t rank(size_t v) { return -tree[find_root(v)]; }
};

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

  UnionFindTree tree(n);
  for (size_t i = 0; i < m; ++i) {
    size_t s, t;
    scanf("%zu %zu", &s, &t);

    tree.unite(s, t);
  }

  size_t q;
  scanf("%zu", &q);

  for (size_t i = 0; i < q; ++i) {
    size_t s, t;
    scanf("%zu %zu", &s, &t);

    printf("%s\\n", tree.has_same_root(s, t) ? "yes" : "no");
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

template <class T> using lp_queue = priority_queue<T, vector<T>, greater<T>>;

using Weight = int;
using DArray = vector<Weight>;
using DMatrix = vector<DArray>;

static const Weight INF = 1 << 29;

struct Edge {
  size_t src, dst;
  Weight weight;
  Edge() {}
  Edge(size_t src, size_t dst, Weight weight = 1)
      : src(src), dst(dst), weight(weight) {}
};

bool operator<(const Edge &e, const Edge &f) {
  if (e.weight != f.weight) {
    return e.weight > f.weight;
  } else {
    return e.src != f.src ? e.src < f.src : e.dst < f.dst;
  }
}

using Edges = vector<Edge>;
using Vertex = vector<Edge>;
using Graph = vector<Vertex>;

void join(Graph &g, size_t s, size_t d, Weight w = 1) {
  // non-directed
  g[s].push_back(Edge(s, d, w));
  g[d].push_back(Edge(d, s, w));
}

void connect(Graph &g, size_t s, size_t d, Weight w = 1) {
  // directed
  g[s].push_back(Edge(s, d, w));
}

vector<Weight> shortest_distance(const Graph &g, size_t s) {
  /* Dijkstra */
  size_t V = g.size();
  vector<Weight> d(V, INF);
  d[s] = 0;

  lp_queue<pair<Weight, size_t>> q;
  q.push(make_pair(0, s));
  while (!q.empty()) {
    pair<Weight, size_t> p = q.top();
    q.pop();
    size_t v = p.second;
    if (d[v] < p.first)
      continue;

    for (const Edge &e : g[v])
      if (d[e.dst] > d[v] + e.weight) {
        d[e.dst] = d[v] + e.weight;
        q.push(make_pair(d[e.dst], e.dst));
      }
  }
  return d;
}

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

  Graph g(n);
  for (size_t i = 0; i < m; ++i) {
    size_t s, t;
    scanf("%zu %zu", &s, &t);

    join(g, s, t);
  }

  size_t q;
  scanf("%zu", &q);

  for (size_t i = 0; i < q; ++i) {
    size_t s, t;
    scanf("%zu %zu", &s, &t);

    printf("%s\\n", shortest_distance(g, s)[t] < INF ? "yes" : "no");
  }

  return 0;
}