#include <bits/stdc++.h>
using namespace std;
template <class T1>
void deb(T1 e1) {
  cout << e1 << endl;
}
template <class T1, class T2>
void deb(T1 e1, T2 e2) {
  cout << e1 << " " << e2 << endl;
}
template <class T1, class T2, class T3>
void deb(T1 e1, T2 e2, T3 e3) {
  cout << e1 << " " << e2 << " " << e3 << endl;
}
template <class T1, class T2, class T3, class T4>
void deb(T1 e1, T2 e2, T3 e3, T4 e4) {
  cout << e1 << " " << e2 << " " << e3 << " " << e4 << endl;
}
template <class T1, class T2, class T3, class T4, class T5>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) {
  cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << endl;
}
template <class T1, class T2, class T3, class T4, class T5, class T6>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) {
  cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << " " << e6
       << endl;
}
int dx[] = {1, -1, 0, 0, -1, -1, 1, 1};
int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};
int pos = 0, st[450000], en[450000], par[450000], in[450000], ans[450000];
vector<int> vc[450000];
vector<pair<int, int> > my[450000];
int findo(int v) {
  if (v == par[v]) return v;
  return par[v] = findo(par[v]);
}
void dfs(int v) {
  st[v] = ++pos;
  for (int i = 0; i < vc[v].size(); i++) {
    int w = vc[v][i];
    dfs(w);
  }
  en[v] = pos;
}
struct node {
  int x, y, v, id, ty;
};
node gr[450000];
int main() {
  int i, j, k, l, m, n, qes = 0, id = 0;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) par[i] = i;
  for (int i = 1; i <= m; i++) {
    scanf("%d", &gr[i].ty);
    if (gr[i].ty == 1) {
      scanf("%d%d", &gr[i].x, &gr[i].y);
      vc[gr[i].y].push_back(gr[i].x);
      in[gr[i].x] = 1;
      continue;
    }
    if (gr[i].ty == 2) {
      cin >> gr[i].v;
      gr[i].id = ++id;
      continue;
    }
    scanf("%d%d", &gr[i].v, &gr[i].id);
    qes++;
    my[gr[i].id].push_back(make_pair(gr[i].v, qes));
  }
  for (int i = 1; i <= n; i++) {
    if (in[i] == 1) continue;
    dfs(i);
  }
  for (int i = 1; i <= m; i++) {
    if (gr[i].ty == 1) {
      int p = findo(gr[i].x);
      int q = findo(gr[i].y);
      par[p] = q;
      continue;
    }
    if (gr[i].ty == 2) {
      int x = gr[i].v;
      int red = gr[i].id;
      for (int j = 0; j < my[red].size(); j++) {
        int q = my[red][j].second;
        int v = my[red][j].first;
        if (st[v] <= st[x] && en[v] >= st[x] && findo(v) == findo(x))
          ans[q] = 1;
      }
    }
  }
  for (int i = 1; i <= qes; i++) {
    if (ans[i])
      printf("YES\n");
    else
      printf("NO\n");
  }
}
