#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m, k, fa[N], cnt[N], vis[N];
vector<int> G[N];
vector<pair<int, int> > p, q[N];
int findset(int x) { return x == fa[x] ? x : fa[x] = findset(fa[x]); }
void dfs(int x) {
  vis[x] = 1;
  for (auto it : G[x]) dfs(it);
  for (auto it : q[x])
    if (vis[it.first]) cnt[it.second]++;
  vis[x] = 0;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i <= n; i++) fa[i] = i;
  int op, x, y;
  while (m--) {
    scanf("%d", &op);
    if (op == 1) {
      scanf("%d%d", &x, &y);
      fa[x] = y;
      G[y].push_back(x);
    } else if (op == 2) {
      scanf("%d", &x);
      p.push_back(make_pair(findset(x), x));
    } else {
      scanf("%d%d", &x, &y);
      pair<int, int> u = p[y - 1];
      q[x].push_back(make_pair(u.first, k));
      q[u.second].push_back(make_pair(x, k));
      k++;
    }
  }
  for (int i = 1; i <= n; i++)
    if (fa[i] == i) dfs(i);
  for (int i = 0; i < k; i++) printf("%s\n", cnt[i] == 2 ? "YES" : "NO");
  return 0;
}
