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
#include <iostream>
#include <vector>
#include <unordered_set>

int n, q, a, b, len;
char c;
short status[300001]; // 0 - na pewno nie ma | 1 - na pewno jest | 2 - nie wiadomo
std::vector<int> graph[300001];

bool DFS(int parent, int x, int y)
{
    for (int i = 0; i < graph[x].size(); i++)
    {
        if (graph[x][i] == x) return true;
        if (graph[x][i] == parent) continue;
        if (graph[x][i] == y) return true;
        else if (DFS(x, graph[x][i], y)) return true;
    }
    return false;
}

std::unordered_set<int> set;

void eval(int x)
{
    set.insert(x);
    for (int i = graph[x].size() - 1; i >= 0; i--)
    {
        if (set.find(graph[x][i]) == set.end())
            eval(graph[x][i]);
    }
}

int main()
{
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);

    std::cin >> n >> q;

    for (int i = 0; i < q; i++)
    {
        /*for (int j = 1; j <= n; j++)
        {
            b = status[j];
            if (b < 2) std::cout << b;
            else std::cout << "?";
        }
        std::cout << "\n";*/

        std::cin >> c >> a;
        if (c == '?') 
        {
            b = status[a];
            if (b < 2) std::cout << b;
            else std::cout << "?";
        //std::cout << "\n";
        }
        else if (c == '-')
        {
            status[a] = 0;
            if (graph[a].size() == 0) continue;
            eval(a);
            b = 0;
            for (auto node : set)
            {
                if (status[node] == 2 && ++b == 2) break;
            }
            if (b == 2) continue;
            for (auto node : set)
            {
                status[node] = 0;
                graph[node].clear();
            }
        }
        else if (c == '+')
        {
            std::cin >> b;
            if (status[a] == 1) { a = b; }
            if (status[b] == 1) { b = a; }
            status[a] = 2; graph[a].push_back(b);
            status[b] = 2; graph[b].push_back(a);
            if (DFS(a, b, b))
            {
                set.clear();
                eval(a);
                for (auto node : set)
                {
                    if (status[node] != 0)
                        status[node] = 1;
                    graph[node].clear();
                }
            }
        }
    }

    return 0;
}