#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5 + 5;
struct Query {
  int second, x, y;
};
int n, m;
Query q[maxN];
vector<int> head[maxN];
vector<pair<int, int> > pos[maxN];
int tin[maxN], tout[maxN], cnt;
int num[maxN], sl;
int lab[maxN], deg[maxN];
bool res[maxN];
void DFS(int u) {
  tin[u] = ++cnt;
  for (int i = 0; i < (int)head[u].size(); ++i) {
    int first = head[u][i];
    DFS(first);
  }
  tout[u] = ++cnt;
}
int GetRoot(int u) {
  if (lab[u] <= 0)
    return u;
  else {
    int res = GetRoot(lab[u]);
    lab[u] = res;
    return res;
  }
}
void Union(int r1, int r2) {
  if (lab[r1] == lab[r2]) --lab[r2];
  if (lab[r1] > lab[r2])
    lab[r1] = r2;
  else
    lab[r2] = r1;
}
bool Check(int u, int first) {
  return (tin[first] <= tin[u] && tout[first] >= tout[u]);
}
int main() {
  ios_base::sync_with_stdio(false);
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d", &q[i].second);
    if (q[i].second == 2)
      scanf("%d", &q[i].x);
    else
      scanf("%d%d", &q[i].x, &q[i].y);
    if (q[i].second == 1) {
      head[q[i].y].push_back(q[i].x);
      ++deg[q[i].x];
    }
    if (q[i].second == 2) {
      num[i] = ++sl;
    }
    if (q[i].second == 3) {
      pos[q[i].y].push_back(pair<int, int>(q[i].x, i));
    }
  }
  for (int i = 1; i <= n; ++i)
    if (tin[i] == 0 && deg[i] == 0) DFS(i);
  memset(res, false, sizeof res);
  for (int i = 1; i <= m; ++i) {
    if (q[i].second == 1) {
      int r1 = GetRoot(q[i].x), r2 = GetRoot(q[i].y);
      if (r1 != r2) Union(r1, r2);
      continue;
    }
    if (q[i].second == 3) continue;
    int u = q[i].x;
    for (int j = 0; j < (int)pos[num[i]].size(); ++j) {
      int first = pos[num[i]][j].first, vt = pos[num[i]][j].second;
      int r1 = GetRoot(u), r2 = GetRoot(first);
      if (r1 == r2 && Check(u, first))
        res[vt] = true;
      else
        res[vt] = false;
    }
  }
  for (int i = 1; i <= m; ++i)
    if (q[i].second == 3)
      if (res[i])
        printf("YES\n");
      else
        printf("NO\n");
  return 0;
}
