#include <bits/stdc++.h>
using namespace std;
void read_file(bool outToFile = true) {}
const int MAXN = 100000 + 9;
int n, nq;
vector<vector<int> > G;
int P[MAXN][(17 + 9)];
int L[MAXN];
void init() { memset(P, -1, sizeof P); }
void DFS(int u = 0, int p = -1) {
  P[u][0] = p;
  L[u] = p == -1 ? 0 : L[p] + 1;
  for (int i = 0, sz = G[u].size(); i < sz; i++) {
    int v = G[u][i];
    DFS(v, u);
  }
}
void build_P() {
  for (int j = 1; (1 << j) < n; j++) {
    for (int i = 0; i < n; i++) {
      if (P[i][j - 1] != -1) {
        P[i][j] = P[P[i][j - 1]][j - 1];
      }
    }
  }
}
int LCA(int u, int v) {
  if (L[u] < L[v]) swap(u, v);
  int lg = 0;
  while ((1 << lg) <= L[u]) lg++;
  lg--;
  for (int j = lg; j >= 0; j--)
    if (P[u][j] != -1 && L[P[u][j]] >= L[v]) u = P[u][j];
  if (u == v) return u;
  for (int j = lg; j >= 0; j--)
    if (P[u][j] != -1 && P[u][j] != P[v][j]) u = P[u][j], v = P[v][j];
  return P[u][0];
}
struct Query {
  int node, file;
  Query() {}
  Query(int nnode, int nfile) { node = nnode, file = nfile; }
};
Query qu[MAXN];
int rep[MAXN];
int lower[MAXN], upper[MAXN];
void join(int x, int y) {
  assert(rep[x] == x);
  rep[x] = y;
}
int find_rep(int x) {
  if (x == rep[x]) return x;
  return rep[x] = find_rep(rep[x]);
}
void sign(int x, int f) {
  lower[f] = x;
  x = find_rep(x);
  upper[f] = x;
}
bool areTogether(int x, int y) { return find_rep(x) == find_rep(y); }
int main() {
  read_file();
  while (scanf("%d%d", &n, &nq) != -1) {
    for (int i = 0; i < n; i++) rep[i] = i;
    G.assign(n, vector<int>());
    int curFile = 0;
    int m = 0;
    while (nq--) {
      int type;
      scanf("%d", &type);
      if (type == 1) {
        int x, y;
        scanf("%d%d", &x, &y);
        x--, y--;
        join(x, y);
        G[y].push_back(x);
      } else if (type == 2) {
        int x;
        scanf("%d", &x);
        x--;
        sign(x, curFile);
        curFile++;
      } else {
        int x, file;
        scanf("%d%d", &x, &file);
        x--, file--;
        qu[m] = Query(x, file);
        m++;
      }
    }
    init();
    for (int i = 0; i < n; i++) {
      if (rep[i] == i) DFS(i, -1);
    }
    build_P();
    for (int i = 0; i < m; i++) {
      int node = qu[i].node;
      int file = qu[i].file;
      string ans = "NO";
      int lo = lower[file];
      int hi = upper[file];
      if (LCA(lo, node) == node) {
        if (L[node] <= L[lo] && L[node] >= L[hi]) ans = "YES";
      }
      printf("%s\n", ans.c_str());
    }
  }
}
