#include <bits/stdc++.h>
using namespace std;
int e[120000], p[120000];
int GetP(int a) {
  while (p[a] != 0) a = p[a] = GetP(p[a]);
  return a;
}
int main() {
  int n, m;
  while (~scanf("%d%d", &n, &m)) {
    int K, I;
    int x, y, py;
    vector<int> curr_q, curr_p;
    memset(p, 0, sizeof(p));
    memset(e, 0, sizeof(e));
    for (int i = 0; i < m; i++) {
      scanf("%d", &K);
      if (K == 1) {
        scanf("%d%d", &x, &y);
        e[x] = y;
        p[x] = GetP(y);
      } else if (K == 2) {
        scanf("%d", &x);
        curr_q.push_back(x);
        curr_p.push_back(GetP(x));
      } else if (K == 3) {
        scanf("%d%d", &x, &I);
        y = curr_q[I - 1];
        py = curr_p[I - 1];
        while (y != py) {
          if (y == x) break;
          y = e[y];
        }
        if (y == x)
          printf("YES\n");
        else
          printf("NO\n");
      }
    }
  }
  return 0;
}
