#include <bits/stdc++.h>
using namespace std;
const int maxn = 112345;
vector<pair<int, int> > ch[maxn];
vector<pair<int, int> > q;
int n, m, tot, cc[maxn], dep[maxn], anc[maxn][17], clk[maxn][17], bit[17];
pair<int, int> evt[maxn];
bool nr[maxn];
void dfs(int u, int par, int t) {
  dep[u] = dep[par] + 1;
  cc[u] = cc[par];
  anc[u][0] = par;
  clk[u][0] = t;
  for (int(i) = (1); (i) <= (16); (i)++) {
    anc[u][i] = anc[anc[u][i - 1]][i - 1];
    clk[u][i] = max(clk[u][i - 1], clk[anc[u][i - 1]][i - 1]);
  }
  for (auto _ : ch[u]) {
    int v = _.first, tt = _.second;
    dfs(v, u, tt);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int(i) = (1); (i) <= (m); (i)++) {
    int op;
    scanf("%d", &op);
    if (op == 1) {
      int u, v;
      scanf("%d%d", &u, &v);
      ch[v].emplace_back(u, i);
      nr[u] = true;
    } else if (op == 2) {
      int u;
      scanf("%d", &u);
      evt[++tot] = {u, i};
    } else {
      int u, idx;
      scanf("%d%d", &u, &idx);
      q.emplace_back(u, idx);
    }
  }
  for (int(i) = (0); (i) <= ((17) - 1); (i)++) bit[i] = 1 << i;
  int ncc = 0;
  for (int(i) = (1); (i) <= (n); (i)++)
    if (!nr[i]) {
      cc[0] = ++ncc;
      dfs(i, 0, 0);
    }
  for (auto it : q) {
    int u = it.first, v = evt[it.second].first, lim = evt[it.second].second;
    if (cc[u] != cc[v] || dep[u] > dep[v]) {
      puts("NO");
      continue;
    }
    int last = 0;
    for (int(i) = (16); (i) >= (0); (i)--)
      if (dep[v] - dep[u] >= bit[i]) {
        last = max(last, clk[v][i]);
        v = anc[v][i];
      }
    puts(u == v && last <= lim ? "YES" : "NO");
  }
}
