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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include<bits/stdc++.h>
using namespace std;

int n,q,a,b;
char symbol;
set<int> sets[1000009];
int states[300009];
int currentSet[300009];
int currentFreeSetIndex=1;
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cin>>n>>q;
	for(int i=0;i<q;i++)
	{
		cin>>symbol;
		switch(symbol)
		{
			case '?':
			{
				cin>>a;
				if(states[a]==2)
					cout<<'?';
				else
					cout<<states[a];
				break;
			}
			case '+':
			{
				cin>>a>>b;
				if(a==b)
				{
					//cerr<<"a==b"<<endl;
					states[a]=1;
					if(currentSet[a]!=0)
					{
						for(int m:sets[currentSet[a]])
						{
							states[m]=1;
							currentSet[m]=0;
						}
					}
				}
				else if(states[a]==1)
				{
					if(currentSet[b]!=0)
					{
						//cerr<<"adding 1 to set of b"<<endl;
						for(int m : sets[currentSet[b]])
						{
							currentSet[m]=0;
							states[m]=1;
						}
					}
					states[b]=1;
				}
				else if(states[b]==1)
				{
					if(currentSet[a]!=0)
					{
						//cerr<<"adding 1 to set of a"<<endl;
						for(int m : sets[currentSet[a]])
						{
							currentSet[m]=0;
							states[m]=1;
						}
					}
					states[a]=1;
				}
				else if(currentSet[a]!=0)
				{
					if(currentSet[b]==0)
					{
						//cerr<<"adding 0 to set of a"<<endl;
						currentSet[b]=currentSet[a];
						sets[currentSet[a]].insert(b);
						states[b]=2;
					}
					else if(currentSet[b]==currentSet[a])
					{
						//cerr<<"closing cycle"<<endl;
						for(int m : sets[currentSet[a]])
						{
							states[m]=1;
							currentSet[m]=0;
						}
					}
					else
					{
						for(int m:sets[currentSet[a]])
						{
							//cerr<<"joining sets"<<endl;
						    sets[currentSet[b]].insert(m);
							currentSet[m]=currentSet[b];
						}
					}

				}
				else if(currentSet[b]!=0)				//currentSet[a]==0 and state[a]!=1
				{
					//cerr<<"adding 0 to set of b"<<endl;
					states[a]=2;
					currentSet[a]=currentSet[b];
					sets[currentSet[b]].insert(a);
				}
				else if(states[b]==1) //both a and b have no sets 
				{
					//cerr<<"setting a to 1"<<endl;;
					states[a]=1;
				}
				else //both a and b has state=0
				{
					//cerr<<"creating new set"<<endl;
					currentSet[a]=currentFreeSetIndex;
					currentSet[b]=currentFreeSetIndex;
					sets[currentFreeSetIndex].insert(a);
					sets[currentFreeSetIndex].insert(b);
					currentFreeSetIndex++;
					states[a]=states[b]=2;
				}
				break;
			}
			case '-':
			{
				cin>>a;
				states[a]=0;
				if(0!=currentSet[a])
				{
					sets[currentSet[a]].erase(a);
				}
				if(sets[currentSet[a]].size()==1)
				{
					for(int m : sets[currentSet[a]])
					{
						currentSet[m]=0;
						states[m]=0;
					}
				}
				currentSet[a]=0;
				break;
			}
		}
	}
	return 0;
}