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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <bits/stdc++.h>
using namespace std;
#define DebugOn false
#define err if constexpr(DebugOn) cerr
int n;
bitset<300'001> owned;
bitset<300'001> unowned;
vector<set<int>> sets;
map<int,int> belongto;
int main(){
    sets.push_back({});
    std::ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int q;
    cin >> n >> q;
    string out="";
    /*for(int i = 0; i < q; i++){
        out[i] = '2'; //clever debug I did that shown error
    }*/
    //out.resize(q);
    for(int i = 1; i <= 300'000; i++){
        unowned[i]=1;
        owned[i]=0;
    }
    for(int qi = 0; qi < q; qi++){
        char qc;
        cin >> qc;
        if(qc == '+'){
            int a, b;
            cin >> a >> b;
            if(a==b){
                if(unowned[a]){
                    err << "that one is alone\n";
                    owned[a]=1;
                }else{
                    err << "that one is alone (in set)\n";
                    //owned[a]=1;
                    auto setp = belongto[a];
                    for(auto k : sets[setp]){
                        belongto.erase(k);
                        owned[k]=1;
                        unowned[k]=0;
                    }
                    sets[setp].clear();
                }
                continue;
            }
            if(owned[a]){
            
                int tmp = b;
                b=a;
                a=tmp;
            }
            if(owned[b]){
                if(belongto.count(a)==0){
                    err << "one is owned, though second unowned\n";
                    owned[a]=1;}
                else{
                    err << "one is owned but second in set\n";
                    err << "owned: " << b << '\n';
                    err << "in set: " << a << ' ' << owned[a] << ' ' << unowned[a] <<'\n';
                    auto setp = belongto[a];
                    for(auto k : sets[setp]){
                        belongto.erase(k);
                        owned[k]=1;
                        unowned[k]=0;
                    }
                    sets[setp].clear();
                }
                continue;
            }
            if(unowned[a]){
                int tmp = b;
                b=a;
                a=tmp;
            }
            if(unowned[b]){
                if(unowned[a]){
                    err << "both are unowned\n";
                    unowned[b]=0;
                    unowned[a]=0;
                    //new set
                    sets.push_back({});
                    sets[sets.size()-1].insert(a);
                    sets[sets.size()-1].insert(b);
                    belongto[a] = sets.size()-1;
                    belongto[b] = sets.size()-1;
                }else{
                    err << "one is unowned\n";
                    //add b to set a
                    unowned[b]=0;
                    sets[belongto[a]].insert(b);
                    belongto[b]=belongto[a];
                }
                continue;
            }
            if(belongto[a]==belongto[b]){
                err << "both belong to same set\n";
                auto setp = belongto[a];
                err << belongto[a] <<'\n';
                err << sets[belongto[a]].size() <<'\n';
                for(auto k : sets[setp]){
                    err << "H: " << k << '\n';
                    owned[k]=1;
                    unowned[k]=0;
                    belongto.erase(k);//belongto[k]=-1;
                }
                sets[setp].clear();
                //TODO: make sets number owned
                continue;
            }
            if(belongto[a]!=belongto[b]){
                err << "both belong to various sets\n";
                //TODO: merge sets
                if(sets[belongto[a]].size()<sets[belongto[b]].size()){
                    int tmp=b;
                    b=a;
                    a=tmp;
                }

                auto& seta=sets[belongto[a]];
                auto& oldset=sets[belongto[b]];
                //auto seta2=belongto[a];
                for(auto k : sets[belongto[b]]){
                    belongto[k]=belongto[a];
                    seta/*sets[seta2]*/.insert(k);
                }
                oldset/*sets[belongto[b]]*/.clear();
                continue;
            }
            err << "no option was choosen :((\n";
        }else if(qc == '-'){
            int c;
            cin >> c;
            if(belongto.count(c)){
                if(sets[belongto[c]].size()==2){
                    auto sp = belongto[c];
                    for(auto k : sets[sp]){
                        unowned[k]=1;
                        owned[k]=0;
                        belongto.erase(k);
                    }
                    sets[sp].clear();
                    //owned[c]=1;
                    //unowned[c]=0;
                }else {sets[belongto[c]].erase(c);
                    belongto.erase(c);
                }
            }
            unowned[c]=1;
            owned[c]=0;
        }else if(qc == '?'){
            //err << "outputing!!\n";
            int d;
            cin >> d;
            if(owned[d]) out.push_back('1');
            else if(unowned[d]) out.push_back('0');
            else out.push_back('?');
        }
    }
    cout << out << std::flush;
}