#include <bits/stdc++.h> using namespace std; using LL = long long; #define FOR(i, l, r) for (auto i = (l); i <= (r); ++i) #define REP(i, n) FOR (i, 0, (n)-1) #define ssize(x) int(x.size()) template<class A, class B> auto& operator<<(ostream& o, pair<A, B> p) { return o << "(" << p.first << ", " << p.second << ")"; } template<class T> auto operator<<(ostream& o, T x) -> decltype(x.end(), o) { o << "{"; int i = 0; for (auto e : x) o << (", ") + 2 * !i++ << e; return o << "}"; } #ifdef DEBUG #define debug(x...) \ cerr << "[" #x "]: ", [](auto... $) { ((cerr << $ << "; "), ...); }(x), \ cerr << '\n' #else #define debug(...) \ { \ } #endif #define INRANGE(v, from, to) (v >= from && v <= to) // Global variables int nverts, k; vector<vector<int>> adj; void input(); int main() { cin.tie(0)->sync_with_stdio(0); input(); vector<int> score(k); // Check each range FOR (from, 0, nverts - 1) FOR (to, from, nverts - 1) { // Calculate number of connected components using DFS vector<bool> visited(nverts); int cc = 0; FOR (start, 0, nverts - 1) if (!visited[start] && INRANGE(start, from, to)) { if (++cc > k) break; visited[start] = true; deque<int> st({start}); while (!st.empty()) { int v = st.back(); st.pop_back(); for (auto& neigh : adj[v]) if (!visited[neigh] && INRANGE(neigh, from, to)) { visited[neigh] = true; st.push_back(neigh); } } } if (cc <= k) score[cc - 1]++; } // Output for (auto &s : score) cout << s << " "; cout << "\n"; return 0; } void input() { // Input int n; cin >> n >> k; vector<int> first(n), second(n); for (auto& x : first) cin >> x; for (auto& x : second) cin >> x; // Make graph out of our canvas nverts = n << 1; adj.resize(nverts); auto add_edge = [](int a, int b) { adj[a-1].push_back(b-1); adj[b-1].push_back(a-1); }; REP (i, n) { add_edge(first[i], second[i]); if (i + 1 < n) { add_edge(first[i], first[i + 1]); add_edge(second[i], second[i + 1]); } if (i > 0) { add_edge(first[i - 1], first[i]); add_edge(second[i - 1], second[i]); } } add_edge(first.front(), first.back()); add_edge(second.front(), second.back()); }
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 | #include <bits/stdc++.h> using namespace std; using LL = long long; #define FOR(i, l, r) for (auto i = (l); i <= (r); ++i) #define REP(i, n) FOR (i, 0, (n)-1) #define ssize(x) int(x.size()) template<class A, class B> auto& operator<<(ostream& o, pair<A, B> p) { return o << "(" << p.first << ", " << p.second << ")"; } template<class T> auto operator<<(ostream& o, T x) -> decltype(x.end(), o) { o << "{"; int i = 0; for (auto e : x) o << (", ") + 2 * !i++ << e; return o << "}"; } #ifdef DEBUG #define debug(x...) \ cerr << "[" #x "]: ", [](auto... $) { ((cerr << $ << "; "), ...); }(x), \ cerr << '\n' #else #define debug(...) \ { \ } #endif #define INRANGE(v, from, to) (v >= from && v <= to) // Global variables int nverts, k; vector<vector<int>> adj; void input(); int main() { cin.tie(0)->sync_with_stdio(0); input(); vector<int> score(k); // Check each range FOR (from, 0, nverts - 1) FOR (to, from, nverts - 1) { // Calculate number of connected components using DFS vector<bool> visited(nverts); int cc = 0; FOR (start, 0, nverts - 1) if (!visited[start] && INRANGE(start, from, to)) { if (++cc > k) break; visited[start] = true; deque<int> st({start}); while (!st.empty()) { int v = st.back(); st.pop_back(); for (auto& neigh : adj[v]) if (!visited[neigh] && INRANGE(neigh, from, to)) { visited[neigh] = true; st.push_back(neigh); } } } if (cc <= k) score[cc - 1]++; } // Output for (auto &s : score) cout << s << " "; cout << "\n"; return 0; } void input() { // Input int n; cin >> n >> k; vector<int> first(n), second(n); for (auto& x : first) cin >> x; for (auto& x : second) cin >> x; // Make graph out of our canvas nverts = n << 1; adj.resize(nverts); auto add_edge = [](int a, int b) { adj[a-1].push_back(b-1); adj[b-1].push_back(a-1); }; REP (i, n) { add_edge(first[i], second[i]); if (i + 1 < n) { add_edge(first[i], first[i + 1]); add_edge(second[i], second[i + 1]); } if (i > 0) { add_edge(first[i - 1], first[i]); add_edge(second[i - 1], second[i]); } } add_edge(first.front(), first.back()); add_edge(second.front(), second.back()); } |