#include <bits/stdc++.h>
using namespace std;
int i, j, k, l, m, n, s, ans, num, fa, numgen, nowma, a, b;
int typ[100005], xin[100005], x[100005], y[100005], f[100005], gen[100005],
    head[100005], dep[100005], p[100005][22], ma[100005][22], tim[100005],
    fachu[100005];
bool use[100005], vis[100005];
inline int getf(int u) {
  if (f[u] == u) return u;
  return f[u] = getf(f[u]);
}
struct node {
  int v, nex, va;
} gg[100005];
void add(int a, int b, int c) {
  ++k;
  gg[k].v = b;
  gg[k].nex = head[a];
  gg[k].va = c;
  head[a] = k;
}
void dfs(int u) {
  for (register int i = head[u]; i; i = gg[i].nex) {
    int v = gg[i].v;
    if (!vis[v]) {
      vis[v] = 1;
      dep[v] = dep[u] + 1;
      p[v][0] = u;
      ma[v][0] = gg[i].va;
      dfs(v);
    }
  }
}
int main() {
  cin >> n >> m;
  num = 0;
  for (i = 1; i <= n; ++i) f[i] = i;
  for (i = 1; i <= m; ++i) {
    scanf("%d", &typ[i]);
    if (typ[i] == 1) {
      scanf("%d%d", &x[i], &y[i]);
      add(y[i], x[i], i);
      f[x[i]] = y[i];
    }
    if (typ[i] == 2) {
      scanf("%d", &x[i]);
      fachu[++num] = x[i];
      tim[num] = i;
    }
    if (typ[i] == 3) {
      scanf("%d%d", &x[i], &y[i]);
    }
  }
  for (i = 1; i <= n; ++i) {
    fa = getf(i);
    if (!use[fa]) {
      use[fa] = 1;
      gen[++numgen] = fa;
      dep[fa] = 1;
      dfs(fa);
    }
  }
  for (i = 1; i <= 20; ++i) {
    for (j = 1; j <= n; ++j) {
      p[j][i] = p[p[j][i - 1]][i - 1];
      ma[j][i] = max(ma[p[j][i - 1]][i - 1], ma[j][i - 1]);
    }
  }
  for (i = 1; i <= m; ++i) {
    if (typ[i] == 3) {
      if (f[fachu[y[i]]] != f[x[i]]) {
        printf("NO\n");
      } else {
        nowma = 0;
        a = fachu[y[i]];
        b = x[i];
        for (int ii = 20; ii >= 0; --ii) {
          if (dep[a] == dep[b]) break;
          if (dep[p[a][ii]] >= dep[b]) {
            nowma = max(nowma, ma[a][ii]);
            a = p[a][ii];
            if (nowma > tim[y[i]]) break;
          }
        }
        if (nowma > tim[y[i]] || a != b)
          printf("NO\n");
        else
          printf("YES\n");
      }
    }
  }
  return 0;
}
