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
81
82
83
#include <bits/stdc++.h>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(NULL);
	cin.tie(NULL);
	cout.tie(NULL);
	int n,q;
	cin>>n>>q;
	vector<int>repw(n+1,-1);
	vector<int>rep(q*2+1);
	vector<int>st(q*2+1,0);
	vector<int>roz(q*2+1,1);
	for(int i=1; i<=q; i++)
		rep[i]=i;
	auto fnd=[&](int w)
	{
		while(rep[w]!=w)
			w=rep[w];
		return w;
	};
	int a,b,ra,rb;
	char z;
	int id=0;
	for(int i=0; i<q; i++)
	{
		cin>>z;
		if(z=='?')
		{
			cin>>a;
			if(repw[a]==-1)
				cout<<"0";
			else
				if(roz[fnd(repw[a])]==1&&st[fnd(repw[a])]==0)
					cout<<"0";
				else
					if(st[fnd(repw[a])]==0)
						cout<<"?";
					else
						cout<<"1";
		}
		if(z=='-')
		{
			cin>>a;
			roz[fnd(repw[a])]--;
			repw[a]=-1;
		}
		if(z=='+')
		{
			cin>>a>>b;
			if(repw[a]==-1)
			{
				id++;
				repw[a]=id;
			}
			if(repw[b]==-1)
			{
				id++;
				repw[b]=id;
			}
			ra=fnd(repw[a]);
			rb=fnd(repw[b]);
			if(ra==rb)
				st[ra]=1;
			if(st[ra]==1&&st[rb]==0)
			{
				rep[rb]=ra;
				roz[ra]+=roz[rb];
			}
			if(st[ra]==0&&st[rb]==1)
			{
				rep[ra]=rb;
				roz[rb]+=roz[ra];
			}
			if(st[ra]==0&&st[rb]==0)
			{
				rep[ra]=rb;
				roz[rb]+=roz[ra];
			}
		}
	}
	cout<<"\n";
}