#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 10;
vector<vector<int>> adjList(MAX);
vector<int> tin(MAX, 0), tout(MAX, 0);
int timer = 0;
void dfs(int nd) {
  tin[nd] = timer++;
  for (int nxt : adjList[nd]) {
    dfs(nxt);
  }
  tout[nd] = timer++;
}
class Dsu {
 public:
  int n;
  vector<int> p, rnk;
  Dsu(int _n) : n(_n) {
    p = rnk = vector<int>(n, 0);
    iota(p.begin(), p.end(), 0);
  }
  int getPar(int x) { return p[x] == x ? x : p[x] = getPar(p[x]); }
  void unite(int a, int b) {
    int x = getPar(a), y = getPar(b);
    if (x != y) {
      if (rnk[x] < rnk[y]) swap(x, y);
      p[y] = x;
      if (rnk[x] == rnk[y]) rnk[x]++;
    }
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> packets, par(n);
  vector<vector<pair<int, int>>> qq(m);
  vector<tuple<int, int, int>> upd;
  iota(par.begin(), par.end(), 0);
  int qsz = 0;
  for (int i = 0, psz = 0; i < m; i++) {
    int t;
    cin >> t;
    if (t == 1) {
      int x, y;
      cin >> x >> y;
      x--, y--;
      adjList[y].push_back(x);
      par[x] = y;
      upd.emplace_back(t, x, y);
    } else if (t == 2) {
      int x;
      cin >> x;
      x--;
      packets.push_back(x);
      upd.emplace_back(t, x, psz++);
    } else {
      int x, i;
      cin >> x >> i;
      x--, i--;
      qq[i].push_back({x, qsz++});
    }
  }
  timer = 1;
  for (int i = 0; i < n; i++) {
    if (par[i] == i) {
      dfs(i);
    }
  }
  Dsu dsu(n);
  vector<int> ans(qsz);
  for (auto cur : upd) {
    int t, x, y;
    tie(t, x, y) = cur;
    if (t == 1) {
      dsu.unite(x, y);
    } else {
      for (int i = 0; i < (int)qq[y].size(); i++) {
        int xx = qq[y][i].first;
        int pos = qq[y][i].second;
        int a = dsu.getPar(xx), b = dsu.getPar(x);
        if (a != b)
          ans[pos] = 0;
        else if (tin[xx] <= tin[x] && tout[xx] >= tout[x])
          ans[pos] = 1;
        else
          ans[pos] = 0;
      }
    }
  }
  for (int ok : ans) {
    if (ok)
      cout << "YES\n";
    else
      cout << "NO\n";
  }
  return 0;
}
