#include <bits/stdc++.h>
using namespace std;
 
#define MAXN 100013
int N, Q;
 
struct node {
	node *c[2], *p;
	bool flip;
 
	bool r() { return !p || (this != p->c[0] && this != p->c[1]); }
	int d() { return r() ? -1 : this == p->c[1]; }
 
	void push() {
		if (flip) {
			swap(c[0], c[1]);
			if (c[0]) c[0]->flip ^= 1;
			if (c[1]) c[1]->flip ^= 1;
			flip ^= 1;
		}
	}
 
	static void connect(node* pa, node* ch, int d) {
		if (d != -1) pa->c[d] = ch;
		if (ch) ch->p = pa;
	}
 
	void rot() {
		assert(!r());
 
		node* pa = p;
		int x = d();
 
		connect(pa->p, this, pa->d());
		connect(pa, c[!x], x);
		connect(this, pa, !x);
	}
 
	void splay() {
		while (!r() && !p->r()) {
			p->p->push();
			p->push();
			push();
			if (p->d() == d()) p->rot();
			else rot();
			rot();
		}
		if (!r()) {
			p->push();
			push();
			rot();
		}
		push();
	}
 
	void expose() {
		node* pre = nullptr;
		for (node* v = this; v; v = v->p) {
			v->splay();
			v->c[1] = pre;
			pre = v;
		}
		splay();
	}
 
	void make_root() {
		expose();
		flip ^= 1;
	}
 
} verts[MAXN];
 
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
 
	cin >> N >> Q;
 
	while (Q--) {
		string s; cin >> s;
		int a, b;
		cin >> a >> b;
		--a, --b;
		if (s[0] == 'c') {
			if (a == b)
				cout << "YES\n";
			else {
				verts[a].expose();
				verts[b].expose();
				cout << (verts[a].p ? "YES" : "NO") << '\n';
			}
		}
		else if (s[0] == 'a') {
			verts[a].make_root();
			verts[a].p = &verts[b];
		}
		else {
			verts[a].make_root();
			verts[b].expose();
			verts[b].c[0] = nullptr;
			verts[a].p = nullptr;
		}
	}
 
	cout.flush();
	return 0;
}
