#include <iostream> #define MAX_K 10 using namespace std; class unionFind { int *parent; int size; int set_count; public: unionFind(int size) : size(size), set_count(size) { parent = new int[size]; // for (int i = 0; i < size; ++i) // { // parent[i] = i; // } } void clear() { for (int i = 0; i < size; ++i) { parent[i] = i; } set_count = size; } int find(int x) { while (parent[x] != x) { parent[x] = parent[parent[x]]; x = parent[x]; } return x; } void unionSets(int x, int y) { x = find(x); y = find(y); if (x == y) return; parent[y] = x; --set_count; } int setCount() { return set_count; } ~unionFind() { delete[] parent; } }; struct neighbours { int left; int right; int updown; int operator[](int i) { switch (i) { case 0: return left; case 1: return right; case 2: return updown; } return -1; } }; int main() { int n, k; cin >> n >> k; int *up = new int[n], *down = new int[n]; int v[MAX_K] = {2 * n, 0}; neighbours *ns = new neighbours[2 * n]; // read data for (int i = 0; i < n; ++i) { cin >> up[i]; --up[i]; } for (int i = 0; i < n; ++i) { cin >> down[i]; --down[i]; } // prepare neighbours for (int i = 0; i < n - 1; ++i) { ns[up[i]].right = up[i + 1]; ns[down[i]].right = down[i + 1]; } ns[up[n - 1]].right = up[0]; ns[down[n - 1]].right = down[0]; for (int i = 1; i < n; ++i) { ns[up[i]].left = up[i - 1]; ns[down[i]].left = down[i - 1]; } ns[up[0]].left = up[n - 1]; ns[down[0]].left = down[n - 1]; for (int i = 0; i < n; ++i) { ns[up[i]].updown = down[i]; ns[down[i]].updown = up[i]; } // init unionFind unionFind uf(2 * n); for (int i = 0; i < 2 * n; ++i) { uf.clear(); //++v[0]; (zrobione przy deklaracji) for (int j = i + 1; j < 2 * n; ++j) { for (int p = 0; p < 3; ++p) { if (ns[j][p] < j && ns[j][p] >= i) // sasiad jest w przedziale { uf.unionSets(ns[j][p], j); } } int actualSetCount = uf.setCount() - 2 * n + j - i + 1; if (actualSetCount <= k) { ++v[actualSetCount - 1]; } } } for (int i = 0; i < k; ++i) { cout << v[i] << " "; } cout << endl; delete[] up; delete[] down; 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 | #include <iostream> #define MAX_K 10 using namespace std; class unionFind { int *parent; int size; int set_count; public: unionFind(int size) : size(size), set_count(size) { parent = new int[size]; // for (int i = 0; i < size; ++i) // { // parent[i] = i; // } } void clear() { for (int i = 0; i < size; ++i) { parent[i] = i; } set_count = size; } int find(int x) { while (parent[x] != x) { parent[x] = parent[parent[x]]; x = parent[x]; } return x; } void unionSets(int x, int y) { x = find(x); y = find(y); if (x == y) return; parent[y] = x; --set_count; } int setCount() { return set_count; } ~unionFind() { delete[] parent; } }; struct neighbours { int left; int right; int updown; int operator[](int i) { switch (i) { case 0: return left; case 1: return right; case 2: return updown; } return -1; } }; int main() { int n, k; cin >> n >> k; int *up = new int[n], *down = new int[n]; int v[MAX_K] = {2 * n, 0}; neighbours *ns = new neighbours[2 * n]; // read data for (int i = 0; i < n; ++i) { cin >> up[i]; --up[i]; } for (int i = 0; i < n; ++i) { cin >> down[i]; --down[i]; } // prepare neighbours for (int i = 0; i < n - 1; ++i) { ns[up[i]].right = up[i + 1]; ns[down[i]].right = down[i + 1]; } ns[up[n - 1]].right = up[0]; ns[down[n - 1]].right = down[0]; for (int i = 1; i < n; ++i) { ns[up[i]].left = up[i - 1]; ns[down[i]].left = down[i - 1]; } ns[up[0]].left = up[n - 1]; ns[down[0]].left = down[n - 1]; for (int i = 0; i < n; ++i) { ns[up[i]].updown = down[i]; ns[down[i]].updown = up[i]; } // init unionFind unionFind uf(2 * n); for (int i = 0; i < 2 * n; ++i) { uf.clear(); //++v[0]; (zrobione przy deklaracji) for (int j = i + 1; j < 2 * n; ++j) { for (int p = 0; p < 3; ++p) { if (ns[j][p] < j && ns[j][p] >= i) // sasiad jest w przedziale { uf.unionSets(ns[j][p], j); } } int actualSetCount = uf.setCount() - 2 * n + j - i + 1; if (actualSetCount <= k) { ++v[actualSetCount - 1]; } } } for (int i = 0; i < k; ++i) { cout << v[i] << " "; } cout << endl; delete[] up; delete[] down; return 0; } |