#include <bits/stdc++.h>
using namespace std;
int qr[100050][5];
int srt[100050], ed[100050], pr[100050];
int cur;
int ans[100050];
int deg[100050];
vector<pair<int, int> > temp[100050];
vector<int> vc[100050];
void dfs(int u) {
  srt[u] = cur++;
  for (int i = 0; i < vc[u].size(); i++) {
    dfs(vc[u][i]);
  }
  cur++;
  ed[u] = cur;
  cur++;
}
int _find(int x) {
  if (pr[x] == x) return x;
  return pr[x] = _find(pr[x]);
}
int main() {
  int n, m, tp, x, y;
  scanf("%d %d", &n, &m);
  int q = 1;
  for (int i = 1; i <= m; i++) {
    scanf("%d", &tp);
    qr[i][0] = tp;
    if (tp == 1) {
      scanf("%d %d", &x, &y);
      vc[y].push_back(x);
      deg[x]++;
      qr[i][1] = x, qr[i][2] = y;
    } else if (tp == 2) {
      scanf("%d", &x);
      qr[i][1] = x, qr[i][2] = q++;
    } else {
      scanf("%d %d", &x, &y);
      qr[i][1] = x, qr[i][2] = y;
      temp[y].push_back(make_pair(x, i));
    }
  }
  for (int i = 1; i <= n; i++) {
    if (deg[i] == 0) dfs(i);
  }
  for (int i = 1; i <= n; i++) pr[i] = i;
  memset(ans, -1, sizeof ans);
  for (int i = 1; i <= m; i++) {
    if (qr[i][0] == 1) {
      x = _find(qr[i][1]);
      y = _find(qr[i][2]);
      pr[y] = x;
    } else if (qr[i][0] == 2) {
      q = qr[i][2];
      for (int j = 0; j < temp[q].size(); j++) {
        int tx = _find(temp[q][j].first);
        int ty = _find(qr[i][1]);
        if (tx != ty) {
          ans[temp[q][j].second] = 0;
          continue;
        }
        x = temp[q][j].first;
        y = qr[i][1];
        if (srt[y] >= srt[x] && srt[y] <= ed[x])
          ans[temp[q][j].second] = 1;
        else
          ans[temp[q][j].second] = 0;
      }
    }
  }
  for (int i = 1; i <= m; i++) {
    if (ans[i] == 1)
      printf("YES\n");
    else if (ans[i] == 0)
      printf("NO\n");
  }
}
