#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 4;
int n;
int t[2 * N];
int par[N];
int signer[N];
int letter;
int jt[N];
vector<pair<int, pair<int, int> > > quer[N];
long long ans[N];
vector<int> adjlist[N];
int level[N];
int h[N];
int jt_letter[N];
int merge(int a, int b) { return max(a, b); }
void build() {
  for (int i = n - 1; i > 0; --i) t[i] = merge(t[i << 1], t[i << 1 | 1]);
}
void modify(int p, int value) {
  for (t[p += n] = value; p > 1; p >>= 1) t[p >> 1] = merge(t[p], t[p ^ 1]);
}
int query(int l, int r) {
  ++r;
  int res = 0;
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1) res = merge(res, t[l++]);
    if (r & 1) res = merge(res, t[--r]);
  }
  return res;
}
void dfs(int node) {
  if (par[node] == -1) {
    level[node] = 0;
  } else
    level[node] = level[par[node]] + 1;
  for (int i : adjlist[node]) {
    dfs(i);
  }
}
void dfs1(int node) {
  h[level[node]] = node;
  modify(level[node], jt[node]);
  for (auto i : quer[node]) {
    int x = i.first;
    int y = i.second.first;
    int idx = i.second.second;
    if (node == x) {
      ans[idx] = 1;
    } else if (level[x] < level[node] and h[level[x]] == x) {
      if (query(level[x] + 1, level[node]) <= jt_letter[y])
        ans[idx] = 1;
      else
        ans[idx] = 0;
    } else
      ans[idx] = 0;
  }
  for (auto i : adjlist[node]) {
    dfs1(i);
  }
}
void solve() {
  memset(ans, -1, sizeof ans);
  int m;
  cin >> n >> m;
  memset(par, -1, sizeof par);
  for (int i = 0; i < m; ++i) {
    int type;
    cin >> type;
    if (type == 1) {
      int x, y;
      cin >> x >> y;
      par[x] = y;
      adjlist[y].push_back(x);
      jt[x] = i + 1;
    } else if (type == 2) {
      int author;
      cin >> author;
      ++letter;
      jt_letter[letter] = i + 1;
      signer[letter] = author;
    } else {
      int x, y;
      cin >> x >> y;
      quer[signer[y]].push_back(make_pair(x, make_pair(y, i + 1)));
    }
  }
  for (int i = 1; i < n + 1; ++i) {
    if (par[i] == -1) {
      dfs(i);
    }
  }
  for (int i = 1; i < n + 1; ++i) {
    if (par[i] == -1) {
      dfs1(i);
    }
  }
  for (int i = 1; i < m + 1; ++i) {
    if (ans[i] == 1) {
      cout << "YES" << '\n';
    } else if (ans[i] == 0)
      cout << "NO" << '\n';
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
