#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
vector<int> ans, adj[maxn];
int m, n, p[20][maxn], w[20][maxn], who[maxn], h[maxn], mail = 1, T[maxn];
void dfs(int root) {
  for (auto x : adj[root]) {
    h[x] = h[root] + 1;
    dfs(x);
  }
}
void get(int cur) {
  if (cur == m + 1) {
    for (int(i) = (0); (i) < (n); (i)++)
      if (p[0][i] == i) dfs(i);
    for (int(k) = (1); (k) < (20); (k)++)
      for (int(i) = (0); (i) < (n); (i)++) {
        p[k][i] = p[k - 1][p[k - 1][i]];
        w[k][i] = max(w[k - 1][i], w[k - 1][p[k - 1][i]]);
      }
    return;
  }
  int t, x, y;
  cin >> t >> x;
  x--;
  if (t == 1) {
    cin >> y;
    y--;
    h[x] = 0;
    adj[y].push_back(x);
    p[0][x] = y;
    w[0][x] = cur;
  }
  if (t == 2) {
    who[mail] = x;
    T[mail] = cur;
    mail++;
  }
  if (t == 3) cin >> y;
  get(cur + 1);
  if (t == 3) {
    int packat = y;
    y = who[y];
    int lca = y;
    int tm = 0;
    for (int(k) = (0); (k) < (20); (k)++)
      if ((h[y] - h[x]) & (1 << k)) {
        tm = max(tm, w[k][lca]);
        lca = p[k][lca];
      }
    if (lca != x || tm > T[packat] || h[y] < h[x])
      ans.push_back(1);
    else
      ans.push_back(0);
  }
}
int main() {
  cin >> n >> m;
  for (int(i) = (0); (i) < (n); (i)++) p[0][i] = i;
  get(1);
  for (int i = ans.size() - 1; i >= 0; i--) {
    if (!ans[i])
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
}
