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
#include <iostream>
#include <stdlib.h>
using namespace std;
int n,m,a,a2,cu;
char o;
void searc(int mi[],pair<int,int> za[],int v)
{
    for(int i = 0; i < cu; i++)
    {

        if(za[i].first == a2 || za[i].first == a)
        {
            mi[za[i].second] = v;
            za[i] = {-1,-1};
        }
        if(za[i].second == a || za[i].second == a2)
        {
            mi[za[i].first] = v;
            za[i] = {-1,-1};
        }
    }
}

int main()
{
    cin >> n >> m;
    int mi[n] = {0};
    pair<int,int> za[m] = {{-1,-1}};
    for(int i = 0; i < m; i++)
    {
        cin >> o;
        switch(o)
        {
        case '?':
            cin >> a;
            a--;

            if(mi[a] == 2)
            {
                cout << '?' ;
            }
            else{
                cout << mi[a];
            }
            break;
        case'+':
            cin >> a >> a2;
            a--;
            a2--;
            if(mi[a] == 1 || mi[a2] == 1)
            {
                if(mi[a] != 1)
                {
                    mi[a] = 1;
                    mi[a2] = 1;
                    a2 = a;
                    searc(mi,za,1);

                }
                else if(mi[a2] != 1)
                {
                    mi[a] = 1;
                    mi[a2] = 1;
                    a = a2;
                    searc(mi,za,1);
                }
                break;
            }
            if(mi[a] == 2 || mi[a2] == 2)
            {
                searc(mi,za,1);
            }
            if(a == a2)
            {
                mi[a] = 1;
            }
            else
            {
                mi[a] = 2;
                mi[a2] = 2;
                za[cu] = {a,a2};
                cu++;

            }
            break;
        case '-':
            cin >> a;
            a--;
            a2 = a;
            if(mi[a] == 2)
            {
                searc(mi,za,0);
            }
            mi[a] = 0;
            break;
        }
    }
return 0;
}