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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include<bits/stdc++.h>
using namespace std;
int legenda[300010];
struct node{
	bool czy;
	vector<int>graf;
	int father;
	int r = 1;
	bool usu;
	int nr;
};
node tab[1000010+300010];
int it;

void dfs(int x, int o){
	tab[x].czy = 1;
	for(auto j: tab[x].graf)
		if(j!=o)
			dfs(j, x);
	tab[x].graf.clear();
}
int find(int x){
	if(tab[x].father==x)return x;
	return tab[x].father = find(tab[x].father);
}

int znajdz;
void dfs2(int x, int o){
	if(tab[x].usu==0)znajdz = tab[x].nr;
	for(auto j: tab[x].graf)
		if(j!=o)
			dfs2(j,x);
	tab[x].graf.clear();
}
int main()
{
	int n, i, q;
	scanf("%d%d", &n, &q);
	for(i=1;i<=n;i++){
		legenda[i] = it++;
	}
	for(i=0;i<n;i++){
		tab[i].father = i;
		tab[i].nr = i+1;
	}
	while(q--){
		char znak;
		scanf(" %c", &znak);
		if(znak=='?'){
			int a;
			scanf("%d", &a);
			if(tab[legenda[a]].czy==1)printf("1");
			else if(tab[legenda[a]].graf.size()>0)printf("?");
			else printf("0");
		}
		else if(znak=='-'){
			int a;
			scanf("%d", &a);
			int b = legenda[a];
			tab[b].usu = 1;
			legenda[a] = it++;
			tab[legenda[a]].father = legenda[a];
			tab[legenda[a]].nr = a;
//			printf("%d\n", tab[find(b)].r);
			if(--tab[find(b)].r==1){
				znajdz = -1;
				dfs2(b,-1);
//				printf("znajdz: %d ", znajdz);
				legenda[znajdz] = it++;
				tab[legenda[znajdz]].father = legenda[znajdz];
				tab[legenda[znajdz]].nr = znajdz;
			}
		}
		else if(znak=='+'){
			int a, b;
			scanf("%d%d", &a, &b);
			if(tab[legenda[a]].czy){
//				printf("a ustalony\n");
				dfs(legenda[b], -1);
				continue;
			}
			if(tab[legenda[b]].czy){
//				printf("b ustalony\n");
				dfs(legenda[a],-1);
				continue;
			}
			if(find(legenda[a])==find(legenda[b])){
//				printf("jedno drzewo %d %d\n", a, b);
				dfs(legenda[a], -1);
				continue;
			}
			tab[find(legenda[b])].r+=tab[find(legenda[a])].r;
			tab[find(legenda[a])].father = find(legenda[b]);
			tab[legenda[a]].graf.push_back(legenda[b]);
			tab[legenda[b]].graf.push_back(legenda[a]);
		}
	}
}