#include <iostream>
using namespace std;
int tab[2][100007];
int kraw[200007][3];
int rep[200007];
long long wyn[11];
int ile;
int find(int x){
if(rep[x]==x){
return x;
}
rep[x]=find(rep[x]);
return rep[x];
}
void uni(int a, int b){
// cout << "union "<<a<<" "<<b<<"\n";
int fa = find(a);
int fb = find(b);
if (fa != fb) {
ile--;
// cout << "ile--\n";
rep[fa] = fb;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < n; j++) {
cin >> tab[i][j];
}
}
for (int i = 0; i < n; i++) {
// na prawo
kraw[tab[0][i]][0] = tab[0][(i + 1) % n];
kraw[tab[1][i]][0] = tab[1][(i + 1) % n];
// na lewo
kraw[tab[0][(i + 1) % n]][1] = tab[0][i];
kraw[tab[1][(i + 1) % n]][1] = tab[1][i];
// pionowo
kraw[tab[0][i]][2] = tab[1][i];
kraw[tab[1][i]][2] = tab[0][i];
}
// for (int i = 1; i <= 2 * n; i++) {
// cout << i << ": ";
// for (int j = 0; j < 3; j++) {
// cout << kraw[i][j] << " ";
// }
// cout << "\n";
// }
for (int pocz = 1; pocz <= 2 * n; pocz++) {
for (int i = 1; i <= 2 * n; i++) {
rep[i] = i;
}
ile = 0;
for (int kon = pocz; kon <= 2 * n; kon++) {
ile++;
// cout << "before union (" << pocz << ", " << kon << ") : " << ile << "\n";
for (int i = 0; i < 3; i++) {
if (kraw[kon][i] <= kon && kraw[kon][i] >= pocz)
uni(kon, kraw[kon][i]);
}
// cout << "(" << pocz << ", " << kon << ") : " << ile << "\n";
if (ile <= k) wyn[ile]++;
}
}
for (int i = 1; i <= k; i++) {
cout << wyn[i] << " ";
}
}
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 | #include <iostream> using namespace std; int tab[2][100007]; int kraw[200007][3]; int rep[200007]; long long wyn[11]; int ile; int find(int x){ if(rep[x]==x){ return x; } rep[x]=find(rep[x]); return rep[x]; } void uni(int a, int b){ // cout << "union "<<a<<" "<<b<<"\n"; int fa = find(a); int fb = find(b); if (fa != fb) { ile--; // cout << "ile--\n"; rep[fa] = fb; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; for (int i = 0; i < 2; i++) { for (int j = 0; j < n; j++) { cin >> tab[i][j]; } } for (int i = 0; i < n; i++) { // na prawo kraw[tab[0][i]][0] = tab[0][(i + 1) % n]; kraw[tab[1][i]][0] = tab[1][(i + 1) % n]; // na lewo kraw[tab[0][(i + 1) % n]][1] = tab[0][i]; kraw[tab[1][(i + 1) % n]][1] = tab[1][i]; // pionowo kraw[tab[0][i]][2] = tab[1][i]; kraw[tab[1][i]][2] = tab[0][i]; } // for (int i = 1; i <= 2 * n; i++) { // cout << i << ": "; // for (int j = 0; j < 3; j++) { // cout << kraw[i][j] << " "; // } // cout << "\n"; // } for (int pocz = 1; pocz <= 2 * n; pocz++) { for (int i = 1; i <= 2 * n; i++) { rep[i] = i; } ile = 0; for (int kon = pocz; kon <= 2 * n; kon++) { ile++; // cout << "before union (" << pocz << ", " << kon << ") : " << ile << "\n"; for (int i = 0; i < 3; i++) { if (kraw[kon][i] <= kon && kraw[kon][i] >= pocz) uni(kon, kraw[kon][i]); } // cout << "(" << pocz << ", " << kon << ") : " << ile << "\n"; if (ile <= k) wyn[ile]++; } } for (int i = 1; i <= k; i++) { cout << wyn[i] << " "; } } |
English