#include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
#include <queue>
#include <bitset>
#include <utility>
#include <stack>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define MP make_pair
#define FOR(v,p,k) for(int v=(p);v<=(k);++v)
#define FORD(v,p,k) for(int v=(p);v>=(k);--v)
#define REP(i,n) for(int i=0;i<(n);++i)
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define PB push_back
#define ST first
#define ND second
#define SIZE(x) (int)x.size()
#define ALL(c) c.begin(),c.end()
#define ODD(x) ((x)%2)
#define EVEN(x) (!(ODD(x)))
// copied from: https://www-users.mat.umk.pl/~stencel/acm/algorytmika_praktyczna.pdf
// Struktura danych do reprezentacji zbiorów rozłącznych
struct FAU {
int *p, *w;
int n;
// Konstruktor tworzący reprezentację n jednoelementowych zbiorów rozłącznych
FAU(int n_) : p(new int[n_]), w(new int[n_]), n(n_) {
reset();
}
// Destruktor zwalniający wykorzystywaną pamięć
~FAU() {
delete[]p;
delete[]w;
}
void reset() {
REP(x, n) p[x] = w[x] = -1;
}
// Funkcja zwraca numer reprezentanta zbioru, do którego należy element x
int Find(int x) {
return (p[x] < 0) ? x : p[x] = Find(p[x]);
}
// Funkcja łączy zbiory zawierające elementy x oraz y
void Union(int x, int y) {
if ((x = Find(x)) == (y = Find(y))) return;
if (w[x] > w[y]) p[y] = x;
else p[x] = y;
if (w[x] == w[y]) w[y]++;
}
};
int n, k;
int main() {
scanf("%d %d\n", &n, &k);
int N=2*n;
vector<VI> graph(N, VI(3));
vector<VI> t(2, VI(n));
REP(i, 2) REP(j, n) scanf("%d", &t[i][j]);
REP(i, 2) REP(j, n) t[i][j]--;
REP(i, 2) REP(j, n) {
int num = t[i][j];
graph[num][0] = t[i][(n+j-1)%n];
graph[num][1] = t[i][(n+j+1)%n];
graph[num][2] = t[1-i][j];
}
FAU fau(N);
vector<LL> res(k+1);
REP(i, N) {
fau.reset();
int cnt=1;
res[cnt]++;
FOR(j, i+1, N-1) {
cnt++;
FOREACH(it, graph[j]) {
int next = *it;
if ((next >= i) && (next <= j) && (fau.Find(next) != fau.Find(j))) {
cnt--;
fau.Union(next, j);
}
}
if (cnt <= k) res[cnt]++;
}
}
FOR(i, 1, k) printf("%lld%c", res[i], (i<k) ? ' ' : '\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 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 | #include <cstdio> #include <cstring> #include <cmath> #include <cassert> #include <iostream> #include <algorithm> #include <iterator> #include <string> #include <vector> #include <queue> #include <bitset> #include <utility> #include <stack> using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef vector<int> VI; #define MP make_pair #define FOR(v,p,k) for(int v=(p);v<=(k);++v) #define FORD(v,p,k) for(int v=(p);v>=(k);--v) #define REP(i,n) for(int i=0;i<(n);++i) #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define PB push_back #define ST first #define ND second #define SIZE(x) (int)x.size() #define ALL(c) c.begin(),c.end() #define ODD(x) ((x)%2) #define EVEN(x) (!(ODD(x))) // copied from: https://www-users.mat.umk.pl/~stencel/acm/algorytmika_praktyczna.pdf // Struktura danych do reprezentacji zbiorów rozłącznych struct FAU { int *p, *w; int n; // Konstruktor tworzący reprezentację n jednoelementowych zbiorów rozłącznych FAU(int n_) : p(new int[n_]), w(new int[n_]), n(n_) { reset(); } // Destruktor zwalniający wykorzystywaną pamięć ~FAU() { delete[]p; delete[]w; } void reset() { REP(x, n) p[x] = w[x] = -1; } // Funkcja zwraca numer reprezentanta zbioru, do którego należy element x int Find(int x) { return (p[x] < 0) ? x : p[x] = Find(p[x]); } // Funkcja łączy zbiory zawierające elementy x oraz y void Union(int x, int y) { if ((x = Find(x)) == (y = Find(y))) return; if (w[x] > w[y]) p[y] = x; else p[x] = y; if (w[x] == w[y]) w[y]++; } }; int n, k; int main() { scanf("%d %d\n", &n, &k); int N=2*n; vector<VI> graph(N, VI(3)); vector<VI> t(2, VI(n)); REP(i, 2) REP(j, n) scanf("%d", &t[i][j]); REP(i, 2) REP(j, n) t[i][j]--; REP(i, 2) REP(j, n) { int num = t[i][j]; graph[num][0] = t[i][(n+j-1)%n]; graph[num][1] = t[i][(n+j+1)%n]; graph[num][2] = t[1-i][j]; } FAU fau(N); vector<LL> res(k+1); REP(i, N) { fau.reset(); int cnt=1; res[cnt]++; FOR(j, i+1, N-1) { cnt++; FOREACH(it, graph[j]) { int next = *it; if ((next >= i) && (next <= j) && (fau.Find(next) != fau.Find(j))) { cnt--; fau.Union(next, j); } } if (cnt <= k) res[cnt]++; } } FOR(i, 1, k) printf("%lld%c", res[i], (i<k) ? ' ' : '\n'); return 0; } |
English