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

vector<unordered_set<int>>v;
vector<bool>all;
vector<int>idx;
int curnum;

void unia(int a, int b)
{
    if(idx[a]==idx[b])
        all[idx[a]]=1;
    else
    {
        if(v[idx[a]].size()<v[idx[b]].size())
            swap(a, b);
        if(all[idx[a]]||all[idx[b]])
            all[idx[a]]=1;
        int temp=idx[b];
        for(auto x:v[temp])
        {
            idx[x]=idx[a];
            v[idx[a]].insert(x);
            //cout<<"AAAAA "<<x<<" "<<a<<" "<<b<<endl;
        }
        v[temp].clear();
    }
}


int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, q;
    cin>>n>>q;
    curnum=n+1;
    for(int i=0; i<=n; i++)
    {
        unordered_set<int> s;
        s.insert(i);
        v.push_back(s);
        idx.push_back(i);
        all.push_back(0);
    }
    curnum=n+1;
    char cur;
    int a, b;
    for(int i=0; i<q; i++)
    {
        cin>>cur;
        if(cur=='?')
        {
            cin>>a;

            if(all[idx[a]])
                cout<<1;
            else if(v[idx[a]].size()!=1)
                cout<<"?";
            else
                cout<<0;
        }
        else if(cur=='-')
        {
            cin>>a;
            v[idx[a]].erase(a);
            idx[a]=curnum;
            unordered_set<int> s;
            s.insert(a);
            v.push_back(s);
            curnum++;
            all.push_back(0);
        }
        else
        {
            cin>>a>>b;
            unia(a, b);
        }
        /*
        int j=0;
        for(auto x:v)
        {
            cout<<j<<": ";
            for(auto y:x)
                cout<<y<<" ";
            cout<<endl;
            j++;
        }
        cout<<"p: "<<curnum<<" ";
        for(auto x:idx)
            cout<<x<<" ";
        cout<<endl;
        for(auto x:all)
            cout<<x<<" ";
        cout<<endl;
*/
    }
    cout<<endl;
}