#include <bits/stdc++.h>
using namespace std;
template <class T>
T sqr(T x) {
  return x * x;
}
inline int getBit(long long x, int pos) { return ((x >> pos) & 1); }
const double eps = 1e-9;
const double pi = acos(-1.0);
const int MAXN = (int)(1e5) + 10;
const int MAXM = (int)(1e5) + 10;
int n, m;
int tp[MAXM], X[MAXM], Y[MAXM];
int pack[MAXM], cntPacks;
vector<int> g[MAXN];
pair<int, int> pred[20][MAXN];
int tin[MAXN], tout[MAXN], timer;
void go(int v) {
  tin[v] = timer++;
  for (int i = 1; i < 20; i++) {
    pred[i][v].first = pred[i - 1][pred[i - 1][v].first].first;
    pred[i][v].second =
        max(pred[i - 1][v].second, pred[i - 1][pred[i - 1][v].first].second);
  }
  for (int i = 0; i < ((int)(g[v]).size()); i++) {
    go(g[v][i]);
  }
  tout[v] = timer++;
}
inline bool isAncestor(int v, int u) {
  return tin[v] <= tin[u] && tout[u] <= tout[v];
}
int getMax(int v, int u) {
  int mx = -1;
  for (int i = 19; i >= 0; --i) {
    if (isAncestor(u, pred[i][v].first)) {
      mx = max(mx, pred[i][v].second);
      v = pred[i][v].first;
    }
  }
  return mx;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    pred[0][i] = make_pair(-1, -1);
  }
  cntPacks = 0;
  for (int i = 0; i < m; i++) {
    scanf("%d", &tp[i]);
    tp[i]--;
    if (tp[i] == 0) {
      scanf("%d%d", &X[i], &Y[i]);
      --X[i];
      --Y[i];
      pred[0][X[i]] = make_pair(Y[i], i);
      g[Y[i]].push_back(X[i]);
    } else if (tp[i] == 1) {
      scanf("%d", &X[i]);
      --X[i];
      pack[cntPacks++] = i;
    } else {
      scanf("%d%d", &X[i], &Y[i]);
      --X[i];
      --Y[i];
    }
  }
  for (int i = 0; i < n; i++) {
    if (pred[0][i].first == -1) {
      pred[0][i].first = i;
      pred[0][i].second = -1;
      go(i);
    }
  }
  for (int i = 0; i < m; i++) {
    if (tp[i] == 2) {
      int v = X[i];
      int num = Y[i];
      int u = X[pack[num]];
      if (isAncestor(v, u)) {
        int mx = getMax(u, v);
        if (mx < pack[num]) {
          puts("YES");
        } else {
          puts("NO");
        }
      } else {
        puts("NO");
      }
    }
  }
  return 0;
}
