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
#include <iostream>
#include <vector>

using namespace std;

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

    int n, q;
    cin >> n >> q;
    vector<int> domy(n+1);
    vector<vector<bool>> plusy(n+1, vector<bool>(n+1));
    int niewiadome = 0, komputery = 0;
    for (int i = 0; i < q; i++) {
        char quest;
        cin >> quest;
        if (quest == '+') {
            int a, b;
            cin >> a >> b;
            if (plusy[a][b] == true) {
                domy[a] = 1;
                domy[b] = 1;
                niewiadome -= 2;
                komputery--;
            } else {
                plusy[a][b] = true;
                plusy[b][a] = true;
                if (a == b) {
                    domy[a] = 1;
                } else if (domy[a] == 0 && domy[b] == 0) {
                    domy[a] = 2;
                    domy[b] = 2;
                    niewiadome += 2;
                    komputery++;
                } else if (domy[a] == 1) {
                    domy[b] = 1;
                } else if (domy[b] == 1) {
                    domy[a] = 1;
                } else if (domy[a] == 0) {
                    domy[a] = 2;
                    niewiadome++;
                    komputery++;
                } else if (domy[b] == 0) {
                    domy[b] = 2;
                    niewiadome++;
                    komputery++;
                } else if (domy[a] == 2 && domy[b] == 2) {
                    komputery++;
                }
            }
        } else if (quest == '-') {
            int a;
            cin >> a;
            if (domy[a] == 1) {
                domy[a] = 0;
            } else {
                domy[a] = 0;
                komputery--;
                niewiadome--;
            }
            for (int i = 1; i <= n; i++) {
                if (plusy[a][i] == true) {
                    plusy[a][i] = false;
                    plusy[i][a] = false;
                }
            }
        } else {
            int a;
            cin >> a;
            if (domy[a] == 2) {
                cout << "?";
            } else {
                cout << domy[a];
            }
        }
        if (niewiadome == komputery && komputery != 0) {
            for (int i = 1; i <= n; i++) {
                if (domy[i] == 2) {
                    domy[i] = 1;
                }
            }
            niewiadome = 0;
            komputery = 0;
        }
    }

    return 0;
}