#include <iostream> #include <vector> #include <set> #include <algorithm> #include <ctime> #include <cmath> #include <chrono> #include <map> #include <assert.h> #include <fstream> #include <cstdlib> #include <random> #include <iomanip> #include <queue> #include <random> #include <unordered_map> using namespace std; #define sqr(a) ((a)*(a)) #define all(a) (a).begin(), (a).end() const int MOD = (int) 1e9 + 7; struct Node { int pr; int num; int ver; int diss = 0; bool lastStatus = true; Node(int pr, int num, int ver) : pr(pr), num(num), ver(ver) { } } ; struct DSU { vector<Node> nodes; int n; DSU(int n) : n(n) { for (int i = 0; i < n; ++i) { nodes.push_back(Node(i, 0, 1)); } } int f(int v) { if (nodes[v].pr != v) { nodes[v].pr = f(nodes[v].pr); } return nodes[v].pr; } void connect(int x, int y) { int num = 0; if (!nodes[x].lastStatus) { nodes[x].lastStatus = true; ++num; } if (!nodes[y].lastStatus) { nodes[y].lastStatus = true; ++num; } x = f(x); y = f(y); if (x == y) { nodes[x].num += 1; nodes[x].ver += num; } else { nodes[x].pr = y; nodes[y].num += nodes[x].num + 1; nodes[y].ver += nodes[x].ver + num; } } void dec(int x) { nodes[x].lastStatus = false; x = f(x); --nodes[x].ver; --nodes[x].num; } Node get(int x) { x = f(x); return nodes[x]; } } ; void solve() { int n, m; cin >> n >> m; DSU t(n); for (int i = 0; i < m; ++i) { char type; cin >> type; if (type == '+') { int x, y; cin >> x >> y; --x; --y; t.connect(x, y); } else if (type == '-') { int x; cin >> x; --x; t.dec(x); } else { int x; cin >> x; --x; Node f = t.get(x); if (t.nodes[x].lastStatus == false) { cout << "0"; } else if (f.num >= f.ver) { cout << "1"; } else if (f.num == 0) { cout << "0"; } else { cout << "?"; } } } cout << "\n"; } int main() { // freopen("input.txt", "r", stdin); ios_base::sync_with_stdio(0); cin.tie(0); int tests = 1; // cin >> tests; for (int i = 0; i < tests; ++i) { solve(); } }
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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | #include <iostream> #include <vector> #include <set> #include <algorithm> #include <ctime> #include <cmath> #include <chrono> #include <map> #include <assert.h> #include <fstream> #include <cstdlib> #include <random> #include <iomanip> #include <queue> #include <random> #include <unordered_map> using namespace std; #define sqr(a) ((a)*(a)) #define all(a) (a).begin(), (a).end() const int MOD = (int) 1e9 + 7; struct Node { int pr; int num; int ver; int diss = 0; bool lastStatus = true; Node(int pr, int num, int ver) : pr(pr), num(num), ver(ver) { } } ; struct DSU { vector<Node> nodes; int n; DSU(int n) : n(n) { for (int i = 0; i < n; ++i) { nodes.push_back(Node(i, 0, 1)); } } int f(int v) { if (nodes[v].pr != v) { nodes[v].pr = f(nodes[v].pr); } return nodes[v].pr; } void connect(int x, int y) { int num = 0; if (!nodes[x].lastStatus) { nodes[x].lastStatus = true; ++num; } if (!nodes[y].lastStatus) { nodes[y].lastStatus = true; ++num; } x = f(x); y = f(y); if (x == y) { nodes[x].num += 1; nodes[x].ver += num; } else { nodes[x].pr = y; nodes[y].num += nodes[x].num + 1; nodes[y].ver += nodes[x].ver + num; } } void dec(int x) { nodes[x].lastStatus = false; x = f(x); --nodes[x].ver; --nodes[x].num; } Node get(int x) { x = f(x); return nodes[x]; } } ; void solve() { int n, m; cin >> n >> m; DSU t(n); for (int i = 0; i < m; ++i) { char type; cin >> type; if (type == '+') { int x, y; cin >> x >> y; --x; --y; t.connect(x, y); } else if (type == '-') { int x; cin >> x; --x; t.dec(x); } else { int x; cin >> x; --x; Node f = t.get(x); if (t.nodes[x].lastStatus == false) { cout << "0"; } else if (f.num >= f.ver) { cout << "1"; } else if (f.num == 0) { cout << "0"; } else { cout << "?"; } } } cout << "\n"; } int main() { // freopen("input.txt", "r", stdin); ios_base::sync_with_stdio(0); cin.tie(0); int tests = 1; // cin >> tests; for (int i = 0; i < tests; ++i) { solve(); } } |