#include <bits/stdc++.h>
using namespace std;
const int MX = 100009;
int n, m, doc[MX], when[MX], who[MX], sign[MX], tas, type[MX], depth[MX],
    dp[30][MX], par[30][MX];
vector<int> v[MX];
void dfs(int x) {
  for (auto nxt : v[x]) {
    depth[nxt] = depth[x] + 1;
    dfs(nxt);
  }
}
int check(int x, int y) {
  int dis = depth[x] - depth[y];
  int ret = 0;
  for (int i = 0; i < 20; i++) {
    if ((dis & (1 << i))) {
      ret = max(ret, dp[i][x]);
      x = par[i][x];
    }
  }
  if (x != y) ret = (1 << 30);
  return ret;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> type[i];
    if (type[i] == 1) {
      int x, y;
      cin >> x >> y;
      par[0][x] = y;
      dp[0][x] = i;
      v[y].push_back(x);
    }
    if (type[i] == 2) {
      cin >> who[++tas];
      when[tas] = i;
    }
    if (type[i] == 3) {
      cin >> sign[i] >> doc[i];
    }
  }
  for (int i = 1; i <= 20; i++) {
    for (int j = 1; j <= n; j++) {
      int c = par[i - 1][j];
      par[i][j] = par[i - 1][c];
      dp[i][j] = max(dp[i - 1][j], dp[i - 1][c]);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (par[0][i] == 0) {
      depth[i] = 1;
      dfs(i);
    }
  }
  for (int i = 1; i <= m; i++) {
    if (type[i] != 3) continue;
    int boss = sign[i], son = who[doc[i]];
    if (depth[boss] > depth[son]) {
      puts("NO");
      continue;
    }
    if (check(son, boss) < when[doc[i]])
      puts("YES");
    else
      puts("NO");
  }
}
