#include <cstdio> #include <string> using namespace std; constexpr int N = 300000; int n, q; char q_type; int q_val1, q_val2; int group[N]; int group_type[N]; int parent[N]; int next_group; void init() { for (int i = 0; i < N; i++) parent[i] = i; } int find(const int a) { if (parent[a] != a) { parent[a] = find(parent[a]); return parent[a]; } return a; } void union_sets(const int a, const int b) { const int a_parent = find(a); const int b_parent = find(b); if (a_parent == b_parent) return; parent[a_parent] = b_parent; } int main() { init(); group_type[0] = -1; next_group = 1; scanf("%d%d", &n, &q); for (int i = 0; i < q; i++) { scanf(" %c", &q_type); if (q_type == '?') { scanf("%d", &q_val1); if (group[q_val1] == 0) printf("0"); else if (group_type[find(group[q_val1])]) printf("1"); else printf("?"); } else if (q_type == '-') { scanf("%d", &q_val1); group[q_val1] = 0; } else { scanf("%d%d", &q_val1, &q_val2); if (q_val1 == q_val2) { if (group[q_val1] == 0) { group[q_val1] = next_group; group[q_val2] = next_group; group_type[next_group++] = 1; } else { group_type[find(group[q_val1])] = 1; } } else if (group[q_val1] == 0 && group[q_val2] == 0) { group[q_val1] = next_group; group[q_val2] = next_group++; } else if (group[q_val1] == 0) group[q_val1] = group[q_val2]; else if (group[q_val2] == 0) group[q_val2] = group[q_val1]; else if (find(group[q_val1]) == find(group[q_val2])) { group_type[find(group[q_val1])] = 1; } else { union_sets(find(group[q_val1]), find(group[q_val2])); } } } }
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 | #include <cstdio> #include <string> using namespace std; constexpr int N = 300000; int n, q; char q_type; int q_val1, q_val2; int group[N]; int group_type[N]; int parent[N]; int next_group; void init() { for (int i = 0; i < N; i++) parent[i] = i; } int find(const int a) { if (parent[a] != a) { parent[a] = find(parent[a]); return parent[a]; } return a; } void union_sets(const int a, const int b) { const int a_parent = find(a); const int b_parent = find(b); if (a_parent == b_parent) return; parent[a_parent] = b_parent; } int main() { init(); group_type[0] = -1; next_group = 1; scanf("%d%d", &n, &q); for (int i = 0; i < q; i++) { scanf(" %c", &q_type); if (q_type == '?') { scanf("%d", &q_val1); if (group[q_val1] == 0) printf("0"); else if (group_type[find(group[q_val1])]) printf("1"); else printf("?"); } else if (q_type == '-') { scanf("%d", &q_val1); group[q_val1] = 0; } else { scanf("%d%d", &q_val1, &q_val2); if (q_val1 == q_val2) { if (group[q_val1] == 0) { group[q_val1] = next_group; group[q_val2] = next_group; group_type[next_group++] = 1; } else { group_type[find(group[q_val1])] = 1; } } else if (group[q_val1] == 0 && group[q_val2] == 0) { group[q_val1] = next_group; group[q_val2] = next_group++; } else if (group[q_val1] == 0) group[q_val1] = group[q_val2]; else if (group[q_val2] == 0) group[q_val2] = group[q_val1]; else if (find(group[q_val1]) == find(group[q_val2])) { group_type[find(group[q_val1])] = 1; } else { union_sets(find(group[q_val1]), find(group[q_val2])); } } } } |