#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m;
int packet[N];
int parent[N], rankd[N];
vector<int> nb[N];
int inorder[N], outorder[N];
int timein, timeout;
bool vis[N];
bool noparent[N];
struct query {
  int t;
  int x;
  int y;
} q[N];
vector<int> qwith[N];
bool ans[N];
int get_root(int v) {
  if (parent[v] == v) return v;
  return parent[v] = get_root(parent[v]);
}
void unify(int u, int v) {
  int root_u, root_v;
  root_u = get_root(u);
  root_v = get_root(v);
  if (rankd[root_v] > rankd[root_u]) parent[root_u] = root_v;
  if (rankd[root_v] < rankd[root_u]) parent[root_v] = root_u;
  if (rankd[root_v] == rankd[root_u]) {
    rankd[root_u]++;
    parent[root_v] = root_u;
  }
}
void DFS(int v) {
  vis[v] = true;
  timein++;
  inorder[v] = timein;
  int i;
  for (i = 0; i < nb[v].size(); i++) {
    if (!vis[nb[v][i]]) DFS(nb[v][i]);
  }
  timeout++;
  outorder[v] = timeout;
}
int main() {
  int i, j;
  int packets = 0;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    parent[i] = i;
    rankd[i] = 1;
  }
  memset(noparent, true, sizeof(noparent));
  for (i = 1; i <= m; i++) {
    scanf("%d", &q[i].t);
    if (q[i].t == 1) {
      scanf("%d%d", &q[i].x, &q[i].y);
      nb[q[i].y].push_back(q[i].x);
      noparent[q[i].x] = false;
    }
    if (q[i].t == 2) scanf("%d", &q[i].x);
    if (q[i].t == 3) {
      scanf("%d%d", &q[i].x, &q[i].y);
      qwith[q[i].y].push_back(i);
    }
  }
  for (i = 1; i <= n; i++) {
    if (noparent[i]) DFS(i);
  }
  int u, v;
  for (i = 1; i <= m; i++) {
    if (q[i].t == 1) unify(q[i].y, q[i].x);
    if (q[i].t == 2) {
      packets++;
      packet[packets] = q[i].x;
      for (j = 0; j < qwith[packets].size(); j++) {
        u = q[qwith[packets][j]].x;
        v = packet[packets];
        if (get_root(u) == get_root(v))
          if (inorder[u] <= inorder[v])
            if (outorder[u] >= outorder[v]) {
              ans[qwith[packets][j]] = true;
              continue;
            }
        ans[qwith[packets][j]] = false;
      }
    }
    if (q[i].t == 3) {
      if (ans[i])
        cout << "YES" << endl;
      else
        cout << "NO" << endl;
    }
  }
  return 0;
}
