#include <cstdio> #include <vector> #include <unordered_set> using namespace std; vector<unordered_set<int>> groups; int nextFreeSetID = 0; int knowledge[300001]; // node knowledge status. 2 = UNKNOWN int membership[300001]; int fusedInto[2000001]; // basically a find-union of groups void newGroup(int, int); void groupAdd(int, int); void groupRemove(int); void joinGroups(int, int); void knowAll(int); int getGroupID(int); int getRealGroupID(int); // create new group including a and b and return its ID void newGroup(int a, int b) { membership[a] = nextFreeSetID; membership[b] = nextFreeSetID; knowledge[a] = 2; knowledge[b] = 2; unordered_set<int> setty{a, b}; groups.push_back(setty); fusedInto[nextFreeSetID] = nextFreeSetID; nextFreeSetID++; } // add B to A's group void groupAdd(int a, int b) { int gid = getGroupID(a); groups[gid].insert(b); membership[b] = gid; knowledge[b] = 2; } // remove A from its group // if only 1 is left, dissolve group void groupRemove(int a) { if (knowledge[a] == 2) { int gid = getGroupID(a); groups[gid].erase(a); if (groups[gid].size() == 1) { for (int i : groups[gid]) { knowledge[i] = 0; } // not sure if this is needed and might be slow tbh // fusedInto[gid] = -1; // groups[gid].clear(); } } knowledge[a] = 0; } // joins A's and B's groups // if they are the same groups, void joinGroups(int a, int b) { int groupA = getGroupID(a); int groupB = getGroupID(b); if (groupA == groupB) { knowAll(a); } else { // cout << "JOINING GROUP " << groupA << " AND GROUP " << groupB << endl; // cout << "SIZES: " << groups[groupA].size() << " | " << groups[groupB].size() << endl; if (groups[groupA].size() > groups[groupB].size()) { groups[groupA].insert(groups[groupB].begin(), groups[groupB].end()); fusedInto[groupB] = groupA; // groups[groupB].clear(); } else { groups[groupB].insert(groups[groupA].begin(), groups[groupA].end()); fusedInto[groupA] = groupB; // groups[groupA].clear(); } } } // dissolves A's group // sets knowledge of all members to 1 void knowAll(int a) { int gid = getGroupID(a); for (int i : groups[gid]) { knowledge[i] = 1; } // not sure if this is needed and might be slow tbh // fusedInto[gid] = -1; // groups[gid].clear(); } // return ID of A's group int getGroupID(int a) { int gid = getRealGroupID(membership[a]); membership[a] = gid; return gid; } // get group ID of g after fuses int getRealGroupID(int g) { if (fusedInto[g] == g) { return g; } else { int realGroup = getRealGroupID(fusedInto[g]); fusedInto[g] = realGroup; return realGroup; } } int main() { int n, q; scanf("%d %d\n", &n, &q); char c; int a, b; for (int i = 0; i < q; ++i) { // cout << "PRINT ALL GROUPS" << endl; // for (int k = 0; k < nextFreeSetID; ++k) { // if (fusedInto[k] == k) { // cout << "GROUP " << k << endl; // for (int j : groups[k]) { // cout << j << " "; // } // cout << endl; // } // } // cout << "PRINTED ALL GROUPS" << endl; scanf("%c", &c); if (c == '?') { scanf("%d\n", &a); if (knowledge[a] < 2) { printf("%d", knowledge[a]); } else { printf("\?"); } } else if (c == '+') { scanf("%d %d\n", &a, &b); if (a == b) { if (knowledge[a] == 0) { knowledge[a] = 1; } else /* knowledge[a] == 2 */ { knowAll(a); } } if (knowledge[a] < knowledge[b]) { // eliminate symmetrical cases int temp = a; a = b; b = temp; } if (knowledge[a] == 0) /* and knowledge[b] == 0 */ { newGroup(a, b); } else if (knowledge[a] == 1) { if (knowledge[b] == 0) { knowledge[b] = 1; } else { // THIS SHOULD NEVER HAPPEN! } } else /* knowledge[a] == 2 */ { if (knowledge[b] == 0) { groupAdd(a, b); } else if (knowledge[b] == 1) { knowAll(a); } else { // both A and B unknown. joinGroups(a, b); } } } else if (c == '-') { scanf("%d\n", &a); groupRemove(a); } } printf("\n"); return 0; }
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | #include <cstdio> #include <vector> #include <unordered_set> using namespace std; vector<unordered_set<int>> groups; int nextFreeSetID = 0; int knowledge[300001]; // node knowledge status. 2 = UNKNOWN int membership[300001]; int fusedInto[2000001]; // basically a find-union of groups void newGroup(int, int); void groupAdd(int, int); void groupRemove(int); void joinGroups(int, int); void knowAll(int); int getGroupID(int); int getRealGroupID(int); // create new group including a and b and return its ID void newGroup(int a, int b) { membership[a] = nextFreeSetID; membership[b] = nextFreeSetID; knowledge[a] = 2; knowledge[b] = 2; unordered_set<int> setty{a, b}; groups.push_back(setty); fusedInto[nextFreeSetID] = nextFreeSetID; nextFreeSetID++; } // add B to A's group void groupAdd(int a, int b) { int gid = getGroupID(a); groups[gid].insert(b); membership[b] = gid; knowledge[b] = 2; } // remove A from its group // if only 1 is left, dissolve group void groupRemove(int a) { if (knowledge[a] == 2) { int gid = getGroupID(a); groups[gid].erase(a); if (groups[gid].size() == 1) { for (int i : groups[gid]) { knowledge[i] = 0; } // not sure if this is needed and might be slow tbh // fusedInto[gid] = -1; // groups[gid].clear(); } } knowledge[a] = 0; } // joins A's and B's groups // if they are the same groups, void joinGroups(int a, int b) { int groupA = getGroupID(a); int groupB = getGroupID(b); if (groupA == groupB) { knowAll(a); } else { // cout << "JOINING GROUP " << groupA << " AND GROUP " << groupB << endl; // cout << "SIZES: " << groups[groupA].size() << " | " << groups[groupB].size() << endl; if (groups[groupA].size() > groups[groupB].size()) { groups[groupA].insert(groups[groupB].begin(), groups[groupB].end()); fusedInto[groupB] = groupA; // groups[groupB].clear(); } else { groups[groupB].insert(groups[groupA].begin(), groups[groupA].end()); fusedInto[groupA] = groupB; // groups[groupA].clear(); } } } // dissolves A's group // sets knowledge of all members to 1 void knowAll(int a) { int gid = getGroupID(a); for (int i : groups[gid]) { knowledge[i] = 1; } // not sure if this is needed and might be slow tbh // fusedInto[gid] = -1; // groups[gid].clear(); } // return ID of A's group int getGroupID(int a) { int gid = getRealGroupID(membership[a]); membership[a] = gid; return gid; } // get group ID of g after fuses int getRealGroupID(int g) { if (fusedInto[g] == g) { return g; } else { int realGroup = getRealGroupID(fusedInto[g]); fusedInto[g] = realGroup; return realGroup; } } int main() { int n, q; scanf("%d %d\n", &n, &q); char c; int a, b; for (int i = 0; i < q; ++i) { // cout << "PRINT ALL GROUPS" << endl; // for (int k = 0; k < nextFreeSetID; ++k) { // if (fusedInto[k] == k) { // cout << "GROUP " << k << endl; // for (int j : groups[k]) { // cout << j << " "; // } // cout << endl; // } // } // cout << "PRINTED ALL GROUPS" << endl; scanf("%c", &c); if (c == '?') { scanf("%d\n", &a); if (knowledge[a] < 2) { printf("%d", knowledge[a]); } else { printf("\?"); } } else if (c == '+') { scanf("%d %d\n", &a, &b); if (a == b) { if (knowledge[a] == 0) { knowledge[a] = 1; } else /* knowledge[a] == 2 */ { knowAll(a); } } if (knowledge[a] < knowledge[b]) { // eliminate symmetrical cases int temp = a; a = b; b = temp; } if (knowledge[a] == 0) /* and knowledge[b] == 0 */ { newGroup(a, b); } else if (knowledge[a] == 1) { if (knowledge[b] == 0) { knowledge[b] = 1; } else { // THIS SHOULD NEVER HAPPEN! } } else /* knowledge[a] == 2 */ { if (knowledge[b] == 0) { groupAdd(a, b); } else if (knowledge[b] == 1) { knowAll(a); } else { // both A and B unknown. joinGroups(a, b); } } } else if (c == '-') { scanf("%d\n", &a); groupRemove(a); } } printf("\n"); return 0; } |