#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int n, m;
int son[MAXN], fa[MAXN], wtot;
int q[MAXN][2], qtot;
int Read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x * 10) + (c ^ 48);
    c = getchar();
  }
  return x * f;
}
void Put1(int x) {
  if (x > 9) Put1(x / 10);
  putchar(x % 10 ^ 48);
}
void Put(int x, char c = -1) {
  if (x < 0) putchar('-'), x = -x;
  Put1(x);
  if (c >= 0) putchar(c);
}
template <typename T>
T Max(T x, T y) {
  return x > y ? x : y;
}
template <typename T>
T Min(T x, T y) {
  return x < y ? x : y;
}
template <typename T>
T Abs(T x) {
  return x < 0 ? -x : x;
}
int FA[MAXN];
int findSet(int x) {
  if (x != FA[x]) FA[x] = findSet(FA[x]);
  return FA[x];
}
int head[MAXN], tot, d[MAXN], f[MAXN][17];
struct edge {
  int v, nxt;
} e[MAXN];
void Add_Edge(int x, int y) {
  FA[findSet(y)] = findSet(x);
  e[++tot].v = y;
  e[tot].nxt = head[x];
  head[x] = tot;
}
void dfs(int x, int F) {
  d[x] = d[F] + 1;
  f[x][0] = F;
  for (int i = 1; i <= 16; ++i) f[x][i] = f[f[x][i - 1]][i - 1];
  for (int i = head[x]; i; i = e[i].nxt) dfs(e[i].v, x);
}
int main() {
  n = Read();
  m = Read();
  for (int i = 1; i <= n; ++i) FA[i] = i;
  for (int i = 1; i <= m; ++i) {
    int opt = Read(), x = Read();
    if (opt == 1)
      Add_Edge(Read(), x);
    else if (opt == 2)
      son[++wtot] = x, fa[wtot] = findSet(x);
    else
      q[++qtot][0] = x, q[qtot][1] = Read();
  }
  for (int i = 1; i <= n; ++i)
    if (!d[findSet(i)]) dfs(findSet(i), 0);
  for (int i = 1; i <= qtot; ++i) {
    int u = q[i][0], v = q[i][1];
    if (d[son[v]] >= d[u] && d[fa[v]] <= d[u]) {
      int dz = son[v];
      for (int j = 16; j >= 0; --j)
        if (d[f[dz][j]] >= d[u]) dz = f[dz][j];
      if (dz == u)
        printf("YES\n");
      else
        printf("NO\n");
    } else
      printf("NO\n");
  }
  return 0;
}
