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
// Fryderyk Brajer
#include <bits/stdc++.h>
using namespace std;

using llong = long long;
using pii = pair<int, int>;
constexpr int max_n = 3e5 + 5;
constexpr int mod = 1e9 + 7;

int n, q;
char arr[max_n];

vector<int> adj[max_n];

struct DSU
{
    int par[max_n];
    int rank[max_n]{};

    DSU()
    {
        iota(par, par + max_n, 0);
    }

    int find(int a)
    {
        if (par[a] == a)
            return a;

        return par[a] = find(par[a]);
    }

    void uni(int a, int b)
    {
        a = find(a);
        b = find(b);

        if (a != b)
        {
            if (rank[a] < rank[b])
                swap(a, b);
            par[b] = a;
            if (rank[a] == rank[b])
                rank[a]++;
        }
    }

    void unset(int a)
    {
        par[a] = a;
    }

} us;

void propagate(int a, int p)
{
    for (int e : adj[a])
        if (e != p)
            propagate(e, a);

    adj[a].clear();
    arr[a] = '1';
    us.unset(a);
}

void try_prop(int a)
{
    if (arr[a] == '?')
        propagate(a, 0);
    else
        arr[a] = '1';
}

void add(int a, int b)
{
    if (a == b)
    {
        try_prop(a);
        return;
    }

    if (arr[a] == '1' || arr[b] == '1')
    {
        try_prop(a);
        try_prop(b);
        return;
    }

    int pa = us.find(a);
    int pb = us.find(b);

    if (pa != pb)
    {
        adj[a].emplace_back(b);
        adj[b].emplace_back(a);
        us.uni(a, b);
        arr[a] = arr[b] = '?';
    }
    else
    {
        propagate(a, 0);
    }
}

void sub(int a)
{
    try_prop(a);
    arr[a] = '0';
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> q;
    fill(arr, arr + n + 1, '0');

    while (q--)
    {
        int a, b;
        char op;
        cin >> op >> a;

        if (op == '+')
        {
            cin >> b;
            add(a, b);
        }
        else if (op == '-')
        {
            sub(a);
        }
        else
        {
            cout << arr[a];
        }
    }
}