#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long N = 1e5 + 10;
const long long LOG = 18;
vector<long long> g[N];
long long up[N][LOG], mx[N][LOG], tin[N], tout[N], h[N], timer;
void dfs(long long v, long long curh = 0) {
  tin[v] = ++timer;
  h[v] = curh;
  for (auto u : g[v]) {
    dfs(u, curh + 1);
  }
  tout[v] = ++timer;
}
bool ancestor(long long a, long long b) {
  return tin[a] <= tin[b] && tout[a] >= tout[b];
}
long long get(long long u, long long v) {
  long long vl = h[v] - h[u], res = -1;
  for (long long i = 0; i < LOG; i++) {
    if ((vl >> i) & 1) {
      res = max(res, mx[v][i]);
      v = up[v][i];
    }
  }
  return res;
}
signed main() {
  long long n, m;
  cin >> n >> m;
  vector<pair<long long, long long>> arr, q;
  for (long long i = 0; i < m; i++) {
    long long type;
    cin >> type;
    if (type == 1) {
      long long u, v;
      cin >> u >> v;
      swap(u, v);
      g[u].push_back(v);
      up[v][0] = u;
      mx[v][0] = i;
    } else if (type == 2) {
      long long v;
      cin >> v;
      arr.push_back({v, i});
    } else {
      long long u, num;
      cin >> u >> num;
      num--;
      q.push_back({u, num});
    }
  }
  for (long long i = 1; i <= n; i++) {
    if (up[i][0] == 0) {
      dfs(i);
    }
  }
  for (long long i = 1; i < LOG; i++) {
    for (long long j = 1; j <= n; j++) {
      up[j][i] = up[up[j][i - 1]][i - 1];
      mx[j][i] = max(mx[j][i - 1], mx[up[j][i - 1]][i - 1]);
    }
  }
  for (auto it : q) {
    if (!ancestor(it.first, arr[it.second].first)) {
      cout << "NO\n";
    } else {
      if (get(it.first, arr[it.second].first) <= arr[it.second].second) {
        cout << "YES\n";
      } else {
        cout << "NO\n";
      }
    }
  }
}
