#include <bits/stdc++.h>
using namespace std;
const int inf = 1e5 + 9;
int n, m, type[inf], x[inf], y[inf], in[inf], out[inf],
    timer = 1, par[inf], sz[inf], deg[inf], ans[inf];
vector<int> v[inf];
vector<pair<int, int> > packet[inf];
void dfs(int node, int par) {
  in[node] = timer++;
  for (auto o : v[node])
    if (o != par && in[o] == 0) dfs(o, node);
  out[node] = timer++;
}
int root(int x) {
  if (x == par[x]) return x;
  return par[x] = root(par[x]);
}
void connect(int x, int y) {
  x = root(x), y = root(y);
  if (x == y) return;
  if (sz[x] > sz[y]) swap(x, y);
  par[x] = y;
  sz[y] += sz[x];
}
bool parent(int a, int b) { return (in[a] <= in[b] && out[a] >= out[b]); }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) par[i] = i, sz[i] = 1;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &type[i], &x[i]);
    if (type[i] != 2) scanf("%d", &y[i]);
    if (type[i] == 1) v[y[i]].push_back(x[i]), deg[x[i]]++;
    if (type[i] == 3) packet[y[i]].push_back(make_pair(i, x[i]));
  }
  for (int i = 1; i <= n; i++)
    if (in[i] == 0 && deg[i] == 0) dfs(i, 0);
  int cnt_packet = 0;
  for (int i = 1; i <= m; i++) {
    if (type[i] == 1) connect(x[i], y[i]);
    if (type[i] != 2) continue;
    cnt_packet++;
    for (auto o : packet[cnt_packet])
      ans[o.first] = (root(x[i]) == root(o.second) && parent(o.second, x[i]));
  }
  for (int i = 1; i <= m; i++)
    if (type[i] == 3) cout << (ans[i] ? "YES" : "NO") << endl;
}
