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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <iostream>
#include <map>
#include <set>

using namespace std;

void update_graph(int* state, int* minus_edges, map<int, set<int>>& graph, int start, int extra=0) {
    // Extra means additional edge to count. For example a double edge between
    // point or an edge leading back to point.

    set<int> visited{start};
    set<int> to_visit = graph.at(start);
    set<pair<int,int>> edges;

    while(to_visit.size() > 0) {
        int v = *(to_visit.begin());

        visited.insert(v);
        to_visit.erase(v);

        auto ns = graph.at(v);

        for(auto nbor : ns) {
            if(!visited.count(nbor))
                to_visit.insert(nbor);
            edges.insert({min(v, nbor), max(v, nbor)});
        }
    }

    int edges_n = edges.size() + extra;
    int vert_n = 0;
    
    for(auto v : visited) {
        if(minus_edges[v]) {
            edges_n -= minus_edges[v];
            //cout << v << " subtracts " << minus_edges[v] << " edges\n";
        }
        
        if(state[v])
            vert_n++;
    }

    //if(visited.size() < edges_n)
    //     cout << "\nUnexpeceted. More edges than vertices\n";
    
    if(vert_n <= edges_n) {
        for(auto v : visited) {
            if(state[v]) {
                state[v] = 1;
            }
            graph.erase(v);

            //state[v] = 1;
            minus_edges[v] = 0;
        }
    }
    
    //if(visited.size() <= edges_n) {
    //    for(auto v : visited) {
    //        graph.erase(v);
    //        //if(!minus_edges[v])
    //            state[v] = 1;
    //        minus_edges[v] = 0;
    //    }
    //}
    
    if(edges_n == 0) {
    	for(auto v : visited) {
    	    graph.erase(v);
    	    state[v] = 0;
    	    minus_edges[v] = 0;
    	}
    }
    
    // cout << "visited: ";
    // for(auto v: visited) {
    //     cout << v << " ";
    // }
    // cout << endl;

    // cout << "edges: ";
    // for(auto p : edges)
    //     cout << "(" << p.first << " " << p.second<< ") ";
    // cout << endl;
     
    // cout << "edges_n: " << edges_n << endl;
}

int main() {
    int n, q;
    cin >> n >> q;
    int state[n+1] = {}; // 0 no pc, 1 pc, 3 don't know
    int minus_edges[n+1] = {}; // how many edges to subtract when updating
    map<int, set<int>> graph;

    char c;
    for(int i = 0; i < q; i++) {
        cin >> c;

        if(c == '?') {
            int v;
            cin >> v;

            if(state[v] == 3)
                cout << '?';
            else
                cout << state[v];

        } else if(c == '+') {
            int v1, v2;
            cin >> v1 >> v2;
            // state[v1] = state[v2] = 3;
            bool is_v1 = graph.count(v1), is_v2 = graph.count(v2);
            if(v1 == v2) {
            	state[v1] = state[v2] = 3;
                if(is_v1)
                    update_graph(state, minus_edges, graph, v1, 1);
                else
                    state[v1] = 1;
            } else if(is_v1 && is_v2) {
            	state[v1] = state[v2] = 3;
                graph.at(v1).insert(v2);
                graph.at(v2).insert(v1);

                update_graph(state, minus_edges, graph, v1, 0);
            } else {
                if(is_v1)
                    graph.at(v1).insert(v2);
                else
                    graph.insert({v1, {v2}});
                
                if(is_v2)
                    graph.at(v2).insert(v1);
                else
                    graph.insert({v2, {v1}});
                    
                if(state[v1] == 1 || state[v2] == 1) {
                    state[v1] = state[v1] == 1 ? 1 : 3;
                    state[v2] = state[v2] == 1 ? 1 : 3;
            	    update_graph(state, minus_edges, graph, v1, 1);
            	} else
            	    state[v1] = state[v2] = 3;
            }
        } else { // c == '-'
            int v;
            cin >> v;

            if(graph.count(v)) {
                auto ns = graph.at(v);
                if(ns.size() == 1) {
                    int nbor = *(ns.begin());
                    if(graph.at(nbor).size() == 1) {
                        // A 1 - 1 pair. Means the other didn't get PC.
                        graph.erase(nbor);
                        graph.erase(v);
                        state[v] = 0;
                        state[nbor] = 0;
                    } else {
                        graph.erase(v);
                        graph.at(nbor).erase(v);
                        state[v] = 0;
                        
                        update_graph(state, minus_edges, graph, nbor, 0);
                    }
                    
                } else {
                    // let's try not deleting v here. We will mark its removal by
                    // updating state and minus_edges hehe
                    state[v] = 0;
                    minus_edges[v]++;
                    //cout << "minus_edges[" << v << "] increased\n";
                    
                    update_graph(state, minus_edges, graph, v, 0);


                    // for(auto nbor : ns)
                    //     graph.at(nbor).erase(v);
                    // graph.erase(v);
                }
            } else {
                state[v] = 0;
            }
        }
    }
}