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
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 3e5 + 7;
int rep[MAXN];
set<int> g[MAXN];
int is[MAXN];
int parent[MAXN];
int n, q;

int Find(int a){
	if(a == rep[a]){
		return a;
	}
	rep[a] = Find(rep[a]);
	return rep[a];
}

vector<pair<int, int>> aktEdges;
void dfs1(int v, int p, int source){
	rep[v] = v;
	for(auto u : g[v]){
		if(u != p){
			if(u != source && v != source){
				aktEdges.push_back({u, v});
				dfs1(u, v, source);
			}
		}
	}
}

void usun(int v){
	aktEdges.clear();
	dfs1(v, v, v);
	for(auto ele : aktEdges){
		rep[Find(ele.first)] = Find(ele.second);
	}

	is[v] = 0;
	for(auto ele : g[v]){
		g[ele].erase(v);
	}

	g[v].clear();
}

void query(int v){
	if(is[v]){
		cout << "1";
	}else if(g[v].size() > 0){
		cout << "?";
	}else{
		cout << "0";
	}
}

void dfs2(int v, int p){
	parent[v] = p;
	for(auto u : g[v]){
		if(u != p){
			dfs2(u, v);
		}
	}
}

void Union(int a, int b){
	int repa = Find(a);
	int repb = Find(b);

	if(repa != repb){
		g[a].insert(b);
		g[b].insert(a);
		rep[repa] = repb;
		return;
	}

	dfs2(a, a);
	vector<int> cycle;
	while(parent[b] != b){
		cycle.push_back(b);
		b = parent[b];
	}
	cycle.push_back(a);

	for(auto u : cycle){
		is[u] = true;
		for(auto s : g[u]){
			g[s].erase(u);
		}
		g[u].clear();
	}
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> n >> q;
	for(int i = 1; i <= n; i++){
		rep[i] = i;
	}
	
	while(q--){
		char c;
		cin >> c;
		if(c == '-'){
			int v;
			cin >> v;

			usun(v);
		}else if(c == '?'){
			int v;
			cin >> v;
			query(v);
		}else{
			int a, b;
			cin >> a >> b;

			Union(a, b);
		}
	}
	cout << '\n';

	return 0;
}