#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int parent[N], tin[N], tout[N], disc, t[N], x[N], y[N], ans[N];
vector<int> doc[N], edges[N], Q[N];
int par(int x) {
  if (parent[x] == x) return x;
  parent[x] = par(parent[x]);
  return parent[x];
}
void Union(int x, int y) {
  int px = par(x), py = par(y);
  if (px != py) {
    parent[px] = py;
  }
}
void dfs(int u, int p) {
  tin[u] = ++disc;
  int v;
  for (int i = 0; i < edges[u].size(); ++i) {
    v = edges[u][i];
    if (v != p) {
      dfs(v, u);
    }
  }
  tout[u] = disc;
}
int main() {
  int n, m, docNum = 0;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    parent[i] = i;
  }
  for (int i = 0; i < m; ++i) {
    scanf("%d", t + i);
    if (t[i] == 2) {
      scanf("%d", x + i);
      y[i] = ++docNum;
    } else {
      scanf("%d %d", x + i, y + i);
      if (t[i] == 1) {
        edges[x[i]].push_back(y[i]);
        edges[y[i]].push_back(x[i]);
        parent[x[i]] = y[i];
      } else {
        doc[y[i]].push_back(x[i]);
        Q[y[i]].push_back(i);
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (parent[i] == i) {
      dfs(i, 0);
    }
  }
  for (int i = 1; i <= n; ++i) {
    parent[i] = i;
  }
  for (int i = 0; i < m; ++i) {
    if (t[i] == 1) {
      Union(x[i], y[i]);
    } else if (t[i] == 2) {
      int v = y[i];
      for (int j = 0; j < doc[v].size(); ++j) {
        if (par(doc[v][j]) != par(x[i])) {
          ans[Q[v][j]] = 0;
        } else {
          if (tin[x[i]] >= tin[doc[v][j]] && tout[x[i]] <= tout[doc[v][j]]) {
            ans[Q[v][j]] = 1;
          } else {
            ans[Q[v][j]] = 0;
          }
        }
      }
    } else {
      puts(ans[i] ? "YES" : "NO");
    }
  }
  return 0;
}
