#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10, lg = 18;
int lca[MAXN][lg], n, m, h[MAXN], par[MAXN];
vector<pair<int, int> > q, inf;
vector<int> ch[MAXN];
void topar(int v, int p) {
  h[v] = h[p] + 1;
  for (int i = 0; i < ch[v].size(); i++) topar(ch[v][i], v);
}
int getpar(int v) {
  if (par[v] == v) return v;
  par[v] = getpar(par[v]);
  return par[v];
}
bool getlca(int v, int u) {
  if (getpar(v) != getpar(u)) return false;
  int d = h[u] - h[v], pw = 0;
  if (d < 0) return false;
  while (d) {
    if (d % 2) u = lca[u][pw];
    pw++;
    d /= 2;
  }
  return v == u;
}
int main() {
  int x, y, t;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) par[i] = i;
  for (int i = 0; i < m; i++) {
    cin >> t;
    if (t != 2)
      cin >> x >> y;
    else
      cin >> x;
    if (t == 1)
      lca[x][0] = y, par[x] = y, ch[y].push_back(x);
    else if (t == 2)
      inf.push_back({x, getpar(x)});
    else
      q.push_back({x, y - 1});
  }
  for (int j = 1; j < lg; j++)
    for (int i = 1; i <= n; i++) lca[i][j] = lca[lca[i][j - 1]][j - 1];
  for (int i = 1; i <= n; i++)
    if (par[i] == i) topar(i, i);
  for (int i = 0; i < q.size(); i++) {
    t = q[i].first;
    x = q[i].second;
    if (getlca(t, inf[x].first) && getlca(inf[x].second, t))
      cout << "YES";
    else
      cout << "NO";
    cout << endl;
  }
}
