#include <bits/stdc++.h>
typedef struct treenode {
  int first_child, next_sibling;
} treenode;
treenode tree[100001];
int p[100001][20], max[100001][20];
int luu1[100001], luu2[100001], dd[100001], dd2[100001], level[100001];
void dfs(int u) {
  if (tree[u].first_child == -1) return;
  int nut = tree[u].first_child;
  while (nut != -1) {
    level[nut] = level[u] + 1;
    dfs(nut);
    nut = tree[nut].next_sibling;
  }
}
int main() {
  int n, m, x, y;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    tree[i].first_child = -1;
    tree[i].next_sibling = -1;
    p[i][0] = -1;
    max[i][0] = -1;
  }
  int dem = 0, dem2 = -1, t;
  for (int i = 0; i < m; i++) {
    scanf("%d", &t);
    if (t == 1) {
      scanf("%d%d", &x, &y);
      p[x][0] = y;
      if (tree[y].first_child == -1) {
        tree[y].first_child = x;
      } else {
        tree[x].next_sibling = tree[y].first_child;
        tree[y].first_child = x;
      }
      max[x][0] = i;
    } else if (t == 2) {
      scanf("%d", &x);
      dem++;
      dd[dem] = x;
      dd2[dem] = i;
    } else if (t == 3) {
      dem2++;
      scanf("%d%d", &luu1[dem2], &luu2[dem2]);
    }
  }
  for (int i = 1; i <= n; i++)
    if (p[i][0] == -1) {
      level[i] = 1;
      dfs(i);
    }
  for (int cs = 1; cs < 20; cs++)
    for (int i = 1; i <= n; i++) {
      p[i][cs] = p[p[i][cs - 1]][cs - 1];
      max[i][cs] = (((max[p[i][cs - 1]][cs - 1]) > (max[i][cs - 1]))
                        ? (max[p[i][cs - 1]][cs - 1])
                        : (max[i][cs - 1]));
    }
  int i;
  for (i = 0; i <= dem2; i++) {
    int des = luu1[i];
    int start = dd[luu2[i]];
    if (level[des] > level[start]) {
      printf("NO\n");
    } else {
      int max_s = -1;
      for (int j = 19; j >= 0; j--)
        if (level[des] == level[start])
          break;
        else if (p[start][j] != -1 && level[des] <= level[p[start][j]]) {
          max_s = (((max_s) > (max[start][j])) ? (max_s) : (max[start][j]));
          start = p[start][j];
        }
      if (des == start) {
        if (dd2[luu2[i]] > max_s)
          printf("YES\n");
        else
          printf("NO\n");
      } else
        printf("NO\n");
    }
  }
}
