#include <bits/stdc++.h>
using namespace std;
struct KSD {
  int v, len, next;
} e[1001000], eq[1001000];
int head[1001000], headq[1001000], cnt, cntq, dcount;
int crs[1001000], ans[1001000];
bool visit[1001000];
int n, m;
void add(int u, int v) {
  e[++cnt].v = v;
  e[cnt].len = dcount;
  e[cnt].next = head[u];
  head[u] = cnt;
}
void addq(int u, int v, int len) {
  eq[++cntq].v = v;
  eq[cntq].len = len;
  eq[cntq].next = headq[u];
  headq[u] = cntq;
}
int f[1001000], l[1001000];
inline int find(int x) {
  if (f[x] == x) return x;
  int t = f[x];
  f[x] = find(t);
  l[x] = max(l[x], l[t]);
  return f[x];
}
inline void dfs(int x, int p, int ll) {
  int i;
  for (i = head[x]; i; i = e[i].next) {
    dfs(e[i].v, x, e[i].len);
  }
  for (i = headq[x]; i; i = eq[i].next) {
    if (find(eq[i].v) == x && l[eq[i].v] <= eq[i].len) {
      ans[i] = 1;
    }
  }
  f[x] = p;
  l[x] = ll;
}
int main() {
  int i, j, k;
  int a, b, c;
  scanf("%d%d", &n, &m);
  dcount = 1;
  for (i = 1; i <= m; i++) {
    scanf("%d", &a);
    if (a == 1) {
      scanf("%d%d", &b, &c);
      add(c, b);
      visit[b] = 1;
    }
    if (a == 2) {
      scanf("%d", &b);
      crs[dcount++] = b;
    }
    if (a == 3) {
      scanf("%d%d", &b, &c);
      if (c >= dcount)
        ++cntq;
      else
        addq(b, crs[c], c);
    }
  }
  for (i = 1; i <= n; i++) f[i] = i;
  for (i = 1; i <= n; i++) {
    if (!visit[i]) dfs(i, i, 0);
  }
  for (i = 1; i <= cntq; i++) {
    if (ans[i])
      puts("YES");
    else
      puts("NO");
  }
  return 0;
}
