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
#include <string>
#include <iostream>
#include <set>

using namespace std;

enum State { unknown, present, missing };

constexpr const char StateToString(State e) throw() {
    switch (e) {
        case unknown: return '?';
        case present: return '1';
        case missing: return '0';
    }
    throw invalid_argument("Invalid state: " + e);
}

class Bajtoszyce
{
    int N;
    set<int>* adj;
    State *status;

    void makePresent(int x);
    bool findCycle(int x, int end, int parent);

public:
    Bajtoszyce(int n);
    void delivery(int a, int b);
    State query(int x);
    void crash(int x);
};

Bajtoszyce::Bajtoszyce(int n) {
    N = n;
    adj = new set<int>[N];
    status = new State[N]{missing,};
}

void Bajtoszyce::crash(int x) {
    makePresent(x);
    status[x] = missing;
}

void Bajtoszyce::delivery(int a, int b) {
    if (a == b) {
        makePresent(a);
        return;
    }
    if (status[a] == present) {
        makePresent(b);
        return;
    }
    if (status[b] == present) {
        makePresent(a);
        return;
    }

    if (adj[a].find(b) != adj[a].end()) {
        makePresent(a);
        return;
    }

    adj[a].insert(b);
    adj[b].insert(a);
    status[a] = unknown;
    status[b] = unknown;
    if (findCycle(b, a, a)) {
        makePresent(a);
    }
}

State Bajtoszyce::query(int x) {
    return status[x];
}

bool Bajtoszyce::findCycle(int x, int end, int parent) {
    if (parent != end && adj[x].find(end) != adj[x].end()) {
        return true;
    }
    for (auto it=adj[x].begin(); it!=adj[x].end(); ++it) {
        if (*it != parent && findCycle(*it, end, x))
            return true;
    }
    return false;
}

void Bajtoszyce::makePresent(int x) {
    //cerr << "makePresent(" << x <<")" << endl;
    status[x] = present;
    set<int> t(adj[x]);
    adj[x] = set<int>();
    for (auto it=t.begin(); it!=t.end(); ++it) {
        makePresent(*it);
    }
}

int main() {
    int n,q,x,y;
    char c;
    cin>>n>>q;
    Bajtoszyce b = Bajtoszyce(n);
    for (int i=0; i<q; ++i) {
        cin >> c;
        switch (c) {
        case '?':
            cin >> x;
            //cerr << "query(" << x << ")" << endl;
            cout << StateToString(b.query(x-1));
            break;
        case '+':
            cin >> x >> y;
            // << "delivery(" << x << "," << y << ")" << endl;
            b.delivery(x-1,y-1);
            break;
        case '-':
            cin >> x;
            //cerr << "crash(" << x << ")" << endl;
            b.crash(x-1);
            break;
        default:
            throw invalid_argument("Invalid command: " + c);
            break;
        }
    }
    return 0;
}