#include <bits/stdc++.h> using namespace std; int find(int a, vector<int>& p){ if (a == p[a]) return a; return p[a] = find(p[a], p); } void unio(int a, int b, vector<int>& p, vector<int>& rank){ int x = find(a,p); int y = find(b,p); if (rank[y] > rank[x]) swap(x,y); if (rank[y] == rank[x]) rank[x]++; p[y] = x; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; vector<vector<int>> kol(2, vector<int>(n)); vector<vector<int>>pos(2*n+1, vector<int>(2)); for (int i = 0; i < 2; i++){ for (int j = 0; j < n; j++){ cin >> kol[i][j]; pos[kol[i][j]][0]= i; pos[kol[i][j]][1] = j; } } vector<int> aha(2*n+1); vector<int> dx = {1,0,0,-1}; vector<int> dy = {0,1,-1,0}; int nk; for (int i = 1; i <= 2*n; i++){ vector<int> p(2*n+1); p[i] = i; vector<int> rank(2*n+1); int lspojnych = 1; aha[lspojnych]++; for (int j = i+1; j <= 2*n; j++){ p[j] = j; lspojnych++; for (int a = 0; a < 4; a++){ int col = (pos[j][0]+dx[a]+2)%2; int cob = (pos[j][1]+dy[a]+n)%n; nk = kol[col][cob]; //cout << nk << "loo \n"; if (nk >= i && nk < j && find(nk, p) != find(j, p)){ //cout << nk << " ehe " << j << "\n"; unio(nk,j,p,rank); lspojnych--; } } // cout << i << " " << j << " " << lspojnych << "\n"; aha[lspojnych]++; } } for (int i = 1; i <= k; i++) cout << aha[i] << " "; 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 | #include <bits/stdc++.h> using namespace std; int find(int a, vector<int>& p){ if (a == p[a]) return a; return p[a] = find(p[a], p); } void unio(int a, int b, vector<int>& p, vector<int>& rank){ int x = find(a,p); int y = find(b,p); if (rank[y] > rank[x]) swap(x,y); if (rank[y] == rank[x]) rank[x]++; p[y] = x; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; vector<vector<int>> kol(2, vector<int>(n)); vector<vector<int>>pos(2*n+1, vector<int>(2)); for (int i = 0; i < 2; i++){ for (int j = 0; j < n; j++){ cin >> kol[i][j]; pos[kol[i][j]][0]= i; pos[kol[i][j]][1] = j; } } vector<int> aha(2*n+1); vector<int> dx = {1,0,0,-1}; vector<int> dy = {0,1,-1,0}; int nk; for (int i = 1; i <= 2*n; i++){ vector<int> p(2*n+1); p[i] = i; vector<int> rank(2*n+1); int lspojnych = 1; aha[lspojnych]++; for (int j = i+1; j <= 2*n; j++){ p[j] = j; lspojnych++; for (int a = 0; a < 4; a++){ int col = (pos[j][0]+dx[a]+2)%2; int cob = (pos[j][1]+dy[a]+n)%n; nk = kol[col][cob]; //cout << nk << "loo \n"; if (nk >= i && nk < j && find(nk, p) != find(j, p)){ //cout << nk << " ehe " << j << "\n"; unio(nk,j,p,rank); lspojnych--; } } // cout << i << " " << j << " " << lspojnych << "\n"; aha[lspojnych]++; } } for (int i = 1; i <= k; i++) cout << aha[i] << " "; return 0; } |