#include <bits/stdc++.h>
using namespace std;
inline int Get() {
  int res = 0, q = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') q = -1, ch = getchar();
  while (ch >= '0' && ch <= '9') res = res * 10 + ch - '0', ch = getchar();
  return res * q;
}
const double eps = 1e-12, pi = M_PI;
const int oo = (int)2e9, mod = (int)1e9 + 7;
const long long INF = (long long)1e17;
const int N = (int)1e5, LG = 20;
typedef int arrv[N + 10];
typedef int arre[2 * N + 10];
arrv f[LG + 1], h[LG + 1], c, bel, d, g, tme;
arre pt, nt;
bool v[N + 10];
int n, m, j, k, cnt, tot, all;
struct node {
  int t, x, y;
} q[N + 10];
struct queue {
  arrv v;
  int f, r;
} Q;
void Link(int x, int y) { pt[++tot] = y, nt[tot] = g[x], g[x] = tot; }
void Bfs(int st) {
  Q.v[1] = st, Q.f = Q.r = 1;
  bel[st] = ++all, d[st] = 1, v[st] = 1;
  while (Q.f <= Q.r) {
    int x = Q.v[Q.f++];
    bel[x] = all;
    for (int c = 1; d[x] - (1 << c) > 0; ++c)
      f[c][x] = f[c - 1][f[c - 1][x]],
      h[c][x] = max(h[c - 1][x], h[c - 1][f[c - 1][x]]);
    for (int i = g[x]; i; i = nt[i]) Q.v[++Q.r] = pt[i], d[pt[i]] = d[x] + 1;
  }
}
int Lca(int x, int y) {
  int c = 0, res = 0;
  while (d[y] > d[x]) {
    for (c = 0; d[y] - (1 << c) >= d[x]; ++c)
      ;
    --c, res = max(res, h[c][y]), y = f[c][y];
  }
  if (x == y) return res;
  return -1;
}
int main() {
  scanf(
      "%d"
      "%d"
      "\n",
      &n, &m);
  for (int i = (1), end = (m); i <= end; ++i) {
    int tp;
    scanf("%d", &tp);
    if (tp == 1)
      scanf(
          "%d"
          "%d"
          "\n",
          &j, &k),
          f[0][j] = k, h[0][j] = i, Link(k, j), v[j] = 1;
    else if (tp == 2)
      scanf(
          "%d"
          "\n",
          &j),
          c[++cnt] = j, tme[cnt] = i;
    else
      scanf(
          "%d"
          "%d"
          "\n",
          &q[i].x, &q[i].y),
          q[i].t = i;
  }
  for (int i = (1), end = (n); i <= end; ++i)
    if (!v[i]) Bfs(i);
  for (int i = (1), end = (m); i <= end; ++i)
    if (q[i].t) {
      int qt = c[q[i].y];
      if (bel[qt] != bel[q[i].x])
        printf("NO\n");
      else {
        int ck = Lca(q[i].x, qt);
        if (ck < 0 || ck > tme[q[i].y])
          printf("NO\n");
        else
          printf("YES\n");
      }
    }
  return 0;
}
