#include <bits/stdc++.h>
using namespace std;
const int N(1e5 + 200);
const int D(19);
struct hh {
  int next, v, w;
} edges[N << 1];
struct hhh {
  int x, y;
} ask[N];
int n, m, num, p[N], t[N], top, deg[N], dep[N], point[N], f[N][20], ff[N][20];
inline void addedge(int u, int v, int w) {
  static int en(0);
  edges[++en].next = point[u];
  point[u] = en;
  edges[en].v = v;
  edges[en].w = w;
}
inline void dfs(int x) {
  for (int i(point[x]); i; i = edges[i].next) {
    int v(edges[i].v);
    f[v][0] = x;
    ff[v][0] = edges[i].w;
    dep[v] = dep[x] + 1;
    dfs(v);
  }
}
inline int get(int x, int y) {
  if (dep[x] < dep[y]) return m + 1000;
  if (x == y) return 0;
  int h(dep[x] - dep[y]), res(0);
  for (int i(D); i >= 0; i--) {
    if (h & (1 << i)) res = max(res, ff[x][i]), x = f[x][i];
  }
  if (x != y) return m + 200;
  return res;
}
int main() {
  cin >> n >> m;
  for (int i(1), k, x, y; i <= m; i++) {
    scanf("%d%d", &k, &x);
    if (k == 1) {
      scanf("%d", &y);
      addedge(y, x, i);
      deg[x]++;
    } else if (k == 2)
      p[++num] = x, t[num] = i;
    else {
      scanf("%d", &y);
      ask[++top].x = x;
      ask[top].y = y;
    }
  }
  for (int i(1); i <= n; i++)
    if (!deg[i]) dfs(i);
  for (int j(1); j <= D; j++)
    for (int i(1); i <= n; i++) {
      f[i][j] = f[f[i][j - 1]][j - 1];
      ff[i][j] = max(ff[i][j - 1], ff[f[i][j - 1]][j - 1]);
    }
  for (int i(1), x, y; i <= top; i++) {
    x = ask[i].x;
    y = p[ask[i].y];
    int db = get(y, x);
    if (db <= t[ask[i].y])
      puts("YES");
    else
      puts("NO");
  }
  return 0;
}
