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



string pc="";
vector<int> onion [300007];
vector<int> pco;


void merge(int a,int b)
{
    int old = pco[b];
    int naw = pco[a];
    for(int i=0;i<onion[old].size();i++)
    {
            onion[naw].push_back(onion[old][i]);
            pco[onion[old][i]]=naw;
    }
    onion[old].clear();
}

void div(int a)
{
    int old = pco[a];
    if(onion[old].size()==1) return;
    for(int i=0;i<onion[old].size();i++)
    {
        if (onion[onion[old][i]].size()==0)
        {
            pco[a]=onion[old][i];
            onion[onion[old][i]].push_back(a);
            for(int j=0;j<onion[old].size();j++)
            {
                if(onion[old][j]==a)
                {
                    onion[old].erase(onion[old].begin()+j);
                    break;
                }
            }
            break;
        }
    }
}

void add(int a,int b)
{
    if(pco[a]==pco[b])
    {
        for(int i=0;i<onion[pco[a]].size();i++)
        {
            pc[onion[pco[a]][i]]='1';
        }
        return;
    }
    if(pc[a]=='1' || pc[b]=='1')
    {
        pc[a]='1';
        pc[b]='1';
        if(onion[pc[a]].size()>=onion[pc[b]].size())
        {
            merge(a,b);
        }
        else merge(b,a);
        return;
    }
    if(onion[pc[a]].size()>=onion[pc[b]].size())
    {
        merge(a,b);
    }
    else merge(b,a);
    pc[a]='?';
    pc[b]='?';
    return;
}
void sub(int a)
{
    pc[a]='0';
    div(a);
    return;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    string res="";
    int N,Q;cin>>N>>Q;
    pco.resize(N,0);
    for(int i=0;i<N;i++)
    {
        pc+='0';
        pco[i]=i;
        onion[i].push_back(i);
    }
    while(Q--)
    {
        char q;cin>>q;
        if(q=='+')
        {
            int a, b;cin>>a>>b;
            add(a,b);
        }
        if(q=='-')
        {
            int a;cin>>a;
            sub(a);
        }
        if(q=='?')
        {
            int a;cin>>a;
            res+=pc[a];
        }
    }
    cout<<res;
}