#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e13 + 7;
const long long dd = 1e5 + 7;
const long long mod = 1e9 + 7;
vector<bool> isr(dd, 1), go(dd);
vector<pair<int, int> > P[dd], Z[dd];
vector<pair<int, pair<int, int> > > Q[dd];
int pred[dd][32];
int gpred[dd][32];
int h[dd];
long long getm(int v, int u) {
  int c = -1;
  for (int i = 31; i >= 0; i--)
    if (h[pred[u][i]] >= h[v]) c = max(c, gpred[u][i]), u = pred[u][i];
  if (u == v) return c;
  return inf;
}
vector<vector<pair<pair<int, int>, int> > > Zs(dd);
void dfs(int v) {
  for (int i = 0; i < (int)Q[v].size(); i++)
    Zs[Q[v][i].first].push_back(make_pair(Q[v][i].second, v));
  for (int i = 0; i < (int)Z[v].size(); i++)
    for (int j = 0; j < (int)Zs[Z[v][i].first].size(); j++) {
      long long qw = getm(Zs[Z[v][i].first][j].second, v);
      if (qw < Zs[Z[v][i].first][j].first.first && qw < Z[v][i].second)
        go[Zs[Z[v][i].first][j].first.second] = 1;
    }
  for (int i = 0; i < (int)P[v].size(); i++) dfs(P[v][i].first);
  for (int i = 0; i < (int)Q[v].size(); i++) Zs[Q[v][i].first].pop_back();
}
void dfsq(int a, int b, int c, int hh) {
  pred[a][0] = b;
  h[a] = hh;
  gpred[a][0] = c;
  for (int i = 0; i < P[a].size(); i++)
    dfsq(P[a][i].first, a, P[a][i].second, hh + 1);
}
int main() {
  ios_base::sync_with_stdio(0);
  srand(time(0));
  int n, m, qq = 0, l = 0;
  cin >> n >> m;
  for (int i = 0; i < (int)m; i++) {
    int t;
    cin >> t;
    if (t == 1) {
      int x, y;
      cin >> x >> y;
      x--, y--;
      P[y].push_back(make_pair(x, i));
      isr[x] = 0;
    }
    if (t == 2) {
      int x;
      cin >> x;
      x--;
      Z[x].push_back(make_pair(qq++, i));
    }
    if (t == 3) {
      int x, y;
      cin >> x >> y;
      x--, y--;
      Q[x].push_back(make_pair(y, make_pair(i, l++)));
    }
  }
  for (int i = 0; i < (int)n; i++)
    if (isr[i]) dfsq(i, i, -1, 0);
  for (int i = 1; i < 32; i++)
    for (int j = 0; j < n; j++)
      pred[j][i] = pred[pred[j][i - 1]][i - 1],
      gpred[j][i] = max(gpred[j][i - 1], gpred[pred[j][i - 1]][i - 1]);
  for (int i = 0; i < (int)n; i++)
    if (isr[i]) dfs(i);
  for (int i = 0; i < (int)l; i++)
    if (go[i])
      cout << "YES\n";
    else
      cout << "NO\n";
}
