#include <bits/stdc++.h>
using namespace std;
int timer = 0;
vector<int> tin, tout, parent;
struct str {
  int t, x, y;
} ex;
void Dfs(int v, vector<vector<int>> &graph) {
  tin[v] = timer++;
  for (vector<int>::iterator to = graph[v].begin(); to != graph[v].end();
       to++) {
    if (!tin[*to]) Dfs(*to, graph);
  }
  tout[v] = timer++;
}
int find_set(int v) {
  if (v == parent[v]) return v;
  return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) parent[b] = a;
}
bool par(int v, int u) { return ((tin[v] <= tin[u]) && (tout[v] >= tout[u])); }
int main() {
  setlocale(LC_ALL, "Russian");
  int N, M;
  cin >> N >> M;
  vector<vector<int>> g(N);
  vector<vector<pair<int, int>>> ask(M + 1);
  vector<int> vhod(N, 0), answer(M, -1);
  vector<str> sp;
  tin.assign(N, 0);
  tout.assign(N, 0);
  for (int i = 0; i < N; i++) parent.push_back(i);
  int nomer = 0;
  for (int i = 0; i < M; i++) {
    int t, x, y;
    cin >> t >> x;
    switch (t) {
      case 1:
        cin >> y;
        g[y - 1].push_back(x - 1);
        vhod[x - 1] = 1;
        ex.t = t;
        ex.x = x - 1;
        ex.y = y - 1;
        sp.push_back(ex);
        break;
      case 2:
        nomer++;
        ;
        ex.t = t;
        ex.x = x - 1;
        ex.y = nomer;
        sp.push_back(ex);
        break;
      case 3:
        cin >> y;
        ask[y].push_back(make_pair(x - 1, i));
        break;
      default:;
    }
  }
  for (int i = 0; i < N; i++) {
    if (!vhod[i] && !tin[i]) Dfs(i, g);
  }
  for (vector<str>::iterator to = sp.begin(); to != sp.end(); to++) {
    if ((*to).t == 1)
      union_sets((*to).x, (*to).y);
    else {
      nomer = (*to).y;
      for (vector<pair<int, int>>::iterator t = ask[nomer].begin();
           t != ask[nomer].end(); t++) {
        if ((find_set((*to).x) == find_set((*t).first)) &&
            (par((*t).first, (*to).x)))
          answer[(*t).second] = 1;
        else
          answer[(*t).second] = 0;
      }
    }
  }
  for (int i = 0; i < M; i++)
    if (answer[i] == 1)
      cout << "YES" << endl;
    else if (!answer[i])
      cout << "NO" << endl;
  return 0;
}
