#include<bits/stdc++.h> using namespace std; struct FU { vector<int> rep; vector<bool> elements; int comp; FU(int n): rep(n), comp(0), elements(n, false) { iota(rep.begin(), rep.end(), 0); } int find(int x) { return (rep[x] == x ? x : rep[x] = find(rep[x])); } void insert(int x) { elements[x] = true; comp++; } void merge(int a, int b) { a = find(a), b = find(b); if(elements[a] && elements[b] && a != b) { rep[a] = b; comp--; } } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n,k; cin >> n >> k; vector<vector<int>> t(2, vector<int>(n)); for(auto& i : t) for(int& j : i) cin >> j, j--; vector<pair<int,int>> pos(2*n); for(int i=0;i<2;i++) for(int j=0;j<n;j++) pos[t[i][j]] = {i,j}; vector<int> res(k, 0); for(int i=0;i<2*n;i++) { FU f(2*n); for(int j=i;j<2*n;j++) { f.insert(j); auto [x,y] = pos[j]; f.merge(j, t[1-x][y]); f.merge(j, t[x][(y+n-1)%n]); f.merge(j, t[x][(y+1)%n]); if(f.comp <= k) res[f.comp-1]++; } } for(int i : res) cout << i << " "; cout << "\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 | #include<bits/stdc++.h> using namespace std; struct FU { vector<int> rep; vector<bool> elements; int comp; FU(int n): rep(n), comp(0), elements(n, false) { iota(rep.begin(), rep.end(), 0); } int find(int x) { return (rep[x] == x ? x : rep[x] = find(rep[x])); } void insert(int x) { elements[x] = true; comp++; } void merge(int a, int b) { a = find(a), b = find(b); if(elements[a] && elements[b] && a != b) { rep[a] = b; comp--; } } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n,k; cin >> n >> k; vector<vector<int>> t(2, vector<int>(n)); for(auto& i : t) for(int& j : i) cin >> j, j--; vector<pair<int,int>> pos(2*n); for(int i=0;i<2;i++) for(int j=0;j<n;j++) pos[t[i][j]] = {i,j}; vector<int> res(k, 0); for(int i=0;i<2*n;i++) { FU f(2*n); for(int j=i;j<2*n;j++) { f.insert(j); auto [x,y] = pos[j]; f.merge(j, t[1-x][y]); f.merge(j, t[x][(y+n-1)%n]); f.merge(j, t[x][(y+1)%n]); if(f.comp <= k) res[f.comp-1]++; } } for(int i : res) cout << i << " "; cout << "\n"; return 0; } |