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

constexpr int maxn=300000+7;
constexpr int maxq=1000000+7;

int n;
int q;
char x;
int a,b;

set<int> ss[maxn+maxq];//jaka wielkosc ma spojna
int which_ss[maxn];//do ktorej spojnej naleze

int computers[maxn+maxq];//ile komputerow ma spojna
int akt_ss;

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

    cin>>n>>q;
    for(int i=1;i<=n;i++)
    {
        ss[i].insert(i);
        which_ss[i]=i;
    }
        
    akt_ss=n;

    for(int iter=1;iter<=q;iter++)
    {
        cin>>x;
        if(x=='+')
        {
            cin>>a>>b;
            if(which_ss[a]==which_ss[b])
                computers[which_ss[a]]++;
            else
            {
                int spojna1=which_ss[a];
                int spojna2=which_ss[b];

                if(ss[spojna1].size()>ss[spojna2].size()) swap(spojna1,spojna2); //ss[spojna1].size() <= ss[spojna2].size()
                
                // cout<<spojna1<<" <= "<<spojna2<<endl;
                while(!ss[spojna1].empty())
                {
                    which_ss[*ss[spojna1].begin()]=spojna2;
                    ss[spojna2].insert(*ss[spojna1].begin());
                    // cout<<"spojna: "<<spojna2<<" add: "<<*ss[spojna1].begin()<<endl;
                    ss[spojna1].erase(ss[spojna1].begin());
                }
                computers[spojna2]++;
                computers[spojna2]+=computers[spojna1];
            }

        }
        else if(x=='-')
        {
            cin>>a;
            computers[which_ss[a]]--;
            ss[which_ss[a]].erase(a);

            akt_ss++;
            which_ss[a]=akt_ss;
            ss[akt_ss].insert(a);
        }
        else if(x=='?')
        {
            cin>>a;
            if(computers[which_ss[a]]==0)
                cout<<0;
            else if(computers[which_ss[a]]==ss[which_ss[a]].size())
                cout<<1;
            else
                cout<<'?';
            // cout<<endl;
        }
    }
    return 0;
}