#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
template <class T>
typename T::value_type arr_sum(const T &v, int n) {
  typename T::value_type sum = 0;
  for (int i = (0); i < (n); ++i) sum += v[i];
  return sum;
}
struct Sync_stdio {
  Sync_stdio() {
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
  }
} _sync_stdio;
struct Z {
  int t;
  int x;
  int y;
};
struct Q {
  int i;
  int x;
  int num;
  int ans;
};
vector<vector<int>> up;
vector<vector<int>> g;
int timer = 0;
vector<int> used;
vector<int> tin;
vector<int> tout;
void dfs(int t, int p = 0) {
  tin[t] = ++timer;
  up[t][0] = p;
  for (int i = (1); i < (up[t].size()); ++i) {
    up[t][i] = up[up[t][i - 1]][i - 1];
  }
  for (auto i : g[t]) {
    dfs(i, t);
  }
  tout[t] = ++timer;
}
int upper(int a, int b) { return tout[a] >= tout[b] && tin[a] <= tin[b]; }
int lca(int a, int b) {
  if (upper(a, b)) {
    return a;
  }
  if (upper(b, a)) {
    return b;
  }
  for (int i = (up[a].size() - 1); i > (-1); --i) {
    if (!upper(up[a][i], b)) {
      a = up[a][i];
    }
  }
  return up[a][0];
}
void lca_init() {
  timer = 0;
  used.assign(g.size(), 0);
  tin.resize(g.size());
  tout.resize(g.size());
  up.resize(g.size());
  int l = 1;
  while ((1 << l) <= g.size()) {
    ++l;
  }
  for (auto &i : up) {
    i.resize(l + 1);
  }
  dfs(0);
}
vector<int> parent;
vector<int> r;
void make_set(int v) {
  parent[v] = v;
  r[v] = 1;
}
int find_set(int t) {
  if (t == parent[t]) {
    return t;
  }
  return parent[t] = find_set(parent[t]);
}
void union_sets(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) {
    if (r[a] < r[b]) {
      swap(a, b);
    }
    parent[b] = a;
    if (r[a] == r[b]) {
      ++r[a];
    }
  }
}
int main() {
  int n, q;
  cin >> n >> q;
  r.resize(n + 1);
  parent.resize(n + 1);
  vector<Z> v;
  vector<Q> w;
  g.resize(n + 1);
  for (int i = (0); i < (q); ++i) {
    int t, x, y;
    cin >> t;
    if (t == 1) {
      cin >> x >> y;
      v.push_back({t, x, y});
    } else if (t == 2) {
      cin >> x;
      v.push_back({t, x, 0});
    } else {
      cin >> x >> y;
      w.push_back({i, x, y, -1});
    }
  }
  vector<int> p(n + 1, -1);
  for (auto i : v) {
    if (i.t == 1) {
      p[i.x] = i.y;
      g[i.y].push_back(i.x);
    }
  }
  for (int i = (1); i < (n + 1); ++i) {
    if (p[i] == -1) {
      p[i] = 0;
      g[0].push_back(i);
    }
  }
  lca_init();
  for (int i = (0); i < (n + 1); ++i) {
    make_set(i);
  }
  sort(w.begin(), w.end(),
       [](const Q &x, const Q &y) { return x.num < y.num; });
  int j = 0;
  int t = 1;
  for (int i = (0); i < (v.size()); ++i) {
    if (v[i].t == 1) {
      union_sets(v[i].x, v[i].y);
    } else {
      while (j < w.size() && w[j].num == t) {
        if (lca(v[i].x, w[j].x) == w[j].x &&
            find_set(v[i].x) == find_set(w[j].x)) {
          w[j].ans = 1;
        } else {
          w[j].ans = 0;
        }
        ++j;
      }
      ++t;
    }
  }
  sort(w.begin(), w.end(), [](const Q &x, const Q &y) { return x.i < y.i; });
  for (auto i : w) {
    cout << (i.ans == 1 ? "YES" : "NO") << "\n";
  }
}
