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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//ANMHLIJKTJIY!
#pragma GCC optimize(2)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector")
#pragma GCC diagnostic error "-fwhole-program"
#pragma GCC diagnostic error "-fcse-skip-blocks"
#pragma GCC diagnostic error "-funsafe-loop-optimizations"
#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 998244353
#define F first
#define S second
#define ll int
#define N 1300010
using namespace std;
ll n,q,fa[N],idx[N],sz[N],szed[N],lft[N];
string ans="";
ll getf(ll x){return x==fa[x]?x:fa[x]=getf(fa[x]);}
int main(){
	ll i,x,y;
	scanf("%d%d",&n,&q);
	for(i=0;i<n+q;i++)
	{
		fa[i]=idx[i]=i;
		sz[i]=lft[i]=1,szed[i]=0;
	}
	ll cnt=n;
	while(q--)
	{
		char op[3];
		scanf("%s",&op);
		if(op[0]=='+')
		{
			scanf("%d%d",&x,&y);
			x--,y--;
			x=getf(idx[x]),y=getf(idx[y]);
			if(x!=y)
			{
				fa[x]=y;
				lft[y]+=lft[x];
				sz[y]+=sz[x];
				szed[y]+=szed[x]+1;
				sz[x]=szed[x]=lft[x]=0;
			}
			else
			{
				szed[x]++;
			}
		}
		else if(op[0]=='-')
		{
			scanf("%d",&x);
			x--;
			lft[getf(idx[x])]--;
			idx[x]=cnt++;
		}
		else
		{
			scanf("%d",&x);
			x--;
			x=getf(idx[x]);
			if(sz[x]==szed[x])
			{
				ans+='1';
			}
			else if(lft[x]==1)
			{
				ans+='0';
			}
			else
			{
				ans+='?';
			}
		}
	}
	printf("%s\n",ans.c_str());
	return 0;
}