1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<stdio.h>
#include<set>
#define N 300009
using namespace std;
inline char nc()
{
	static char buf[99999],*l,*r;
	return l==r&&(r=(l=buf)+fread(buf,1,99999,stdin),l==r)?EOF:*l++;
}
inline void read(int&x)
{
	char c=nc();for(;c<'0'||'9'<c;c=nc());
	for(x=0;'0'<=c&&c<='9';x=(x<<3)+(x<<1)+(c^48),c=nc());
}
struct node
{
	set<int>a;bool full;
	inline node(){a.clear();full=0;}
	inline node(int x){a.clear();a.emplace(x);full=0;}
}*a[N];
int n,q;
main()
{
	read(n);read(q);
	for(int o,u,v;q--;)
	{
		for(;o=nc(),(o^'?')&&(o^'+')&&(o^'-'););
		if(o=='?')
		{
			read(u);--u;
			if(a[u])if(!a[u]->full)if(a[u]->a.size()==1)a[u]=0;
			if(!a[u]){putchar('0');continue;}
			if(a[u]->full)putchar('1');
			else putchar('?');
		}
		else if(o=='-')
		{
			read(u);--u;
			node*i=a[u];
			i->a.erase(u);a[u]=0;
		}
		else
		{
			read(u);read(v);--u;--v;
			if(!a[u])a[u]=new node(u);
			if(!a[v])a[v]=new node(v);
			if(a[u]==a[v]){a[u]->full=1;continue;}
			node*x=a[u],*y=a[v];
			if(x->a.size()<y->a.size())swap(x,y);
			x->full|=y->full;
			for(int i:y->a)x->a.emplace(i),a[i]=x;
		}
	}
}