#include <bits/stdc++.h> #define dbg(x) " [" << #x << ": " << (x) << "] " using namespace std; template<typename A, typename B> ostream& operator<<(ostream& out, const pair<A,B>& p) { return out << "(" << p.first << ", " << p.second << ")"; } template<typename T> ostream& operator<<(ostream& out, const vector<T>& c) { out << "{"; for(auto it = c.begin(); it != c.end(); it++) { if(it != c.begin()) out << ", "; out << *it; } return out << "}"; } vector<int> p, r; int cnt; int repr(int v) { if(p[v] == v) return v; return p[v] = repr(p[v]); } void un(int a, int b) { a = repr(a); b = repr(b); if(a == b) return; if(r[a] > r[b]) swap(a, b); p[a] = b; if(r[a] == r[b]) r[b]++; cnt--; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n,k; cin >> n >> k; vector<long long> ans(k + 1); p.resize(2 * n); vector<vector<int>> v(n, vector<int>(2)); for(int i = 0; i < 2; i++) { for(int j = 0; j < n; j++) { cin >> v[j][i]; v[j][i]--; } } vector<vector<int>> g(2 * n); for(int i = 0; i < n; i++) { g[v[i][0]].push_back(v[i][1]); g[v[i][1]].push_back(v[i][0]); for(int j = 0; j < 2; j++) { g[v[i][j]].push_back(v[(i + 1) % n][j]); g[v[(i + 1) % n][j]].push_back(v[i][j]); } } for(int i = 0; i < 2 * n; i++) { cnt = 0; r.assign(2 * n, 1); iota(p.begin(), p.end(), 0); for(int j = i; j < 2 * n; j++) { cnt++; for(int to : g[j]) { if(i <= to && to < j) { un(to, j); } } if(cnt <= k) ans[cnt]++; } } for(int i = 1; i < k + 1; i++) { if(i > 1) cout << " "; cout << ans[i]; } cout << endl; 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 | #include <bits/stdc++.h> #define dbg(x) " [" << #x << ": " << (x) << "] " using namespace std; template<typename A, typename B> ostream& operator<<(ostream& out, const pair<A,B>& p) { return out << "(" << p.first << ", " << p.second << ")"; } template<typename T> ostream& operator<<(ostream& out, const vector<T>& c) { out << "{"; for(auto it = c.begin(); it != c.end(); it++) { if(it != c.begin()) out << ", "; out << *it; } return out << "}"; } vector<int> p, r; int cnt; int repr(int v) { if(p[v] == v) return v; return p[v] = repr(p[v]); } void un(int a, int b) { a = repr(a); b = repr(b); if(a == b) return; if(r[a] > r[b]) swap(a, b); p[a] = b; if(r[a] == r[b]) r[b]++; cnt--; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n,k; cin >> n >> k; vector<long long> ans(k + 1); p.resize(2 * n); vector<vector<int>> v(n, vector<int>(2)); for(int i = 0; i < 2; i++) { for(int j = 0; j < n; j++) { cin >> v[j][i]; v[j][i]--; } } vector<vector<int>> g(2 * n); for(int i = 0; i < n; i++) { g[v[i][0]].push_back(v[i][1]); g[v[i][1]].push_back(v[i][0]); for(int j = 0; j < 2; j++) { g[v[i][j]].push_back(v[(i + 1) % n][j]); g[v[(i + 1) % n][j]].push_back(v[i][j]); } } for(int i = 0; i < 2 * n; i++) { cnt = 0; r.assign(2 * n, 1); iota(p.begin(), p.end(), 0); for(int j = i; j < 2 * n; j++) { cnt++; for(int to : g[j]) { if(i <= to && to < j) { un(to, j); } } if(cnt <= k) ans[cnt]++; } } for(int i = 1; i < k + 1; i++) { if(i > 1) cout << " "; cout << ans[i]; } cout << endl; return 0; } |