#include <bits/stdc++.h>
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define findx(V,x) (int(find(allv(V),(x))-(V).begin()))
#define erasex(V,x) (V).erase(find(allv(V),(x)))
using namespace std;
typedef long long ll;
const int MAXN = 3005;
vector<int> G[MAXN];
vector<int> CV[MAXN];
int Cn;
int D[MAXN];
bitset<MAXN> inTree, chk;
int A[MAXN];
ll Ans[MAXN];
ll K;
int N;
void dfs(vector<int> &cnt, int i, int j, int L, int CL, ll K) {
cnt[(j+K-L)%CL]++;
for(int v : G[i])
dfs(cnt, v, j, L+1, CL, K);
}
void dfsTree(vector<int> &V, int i, int L, ll K) {
chk[i] = true;
V.eb(i); L++;
Ans[V[(L - K%L)%L]]++;
for(int v : G[i])
dfsTree(V, v, L, K);
V.pop_back();
}
void solve_naive(int R, ll K) {
for(int i = N; i; i--) G[i].clear();
for(int i = N; i; i--)
if(i != R) G[A[i]].eb(i);
chk.reset();
{
vector<int> V;
dfsTree(V, R, 0, K);
}
for(int i = N; i; i--) if(!chk[i]) {
vector<int> V;
int j = i;
while(!chk[j]) {
chk[j] = true;
V.eb(j);
j = A[j];
}
int x = findx(V, j);
if(sz(V) <= x) continue;
CV[Cn++] = vector<int>(V.begin()+x, V.end());
}
for(int i = Cn; i--;) {
int L = sz(CV[i]);
vector<int> cnt(L, 0);
for(int j = L; j--;)
erasex(G[CV[i][j]], CV[i][(L+j-1)%L]);
for(int j = L; j--;)
dfs(cnt, CV[i][j], j, 0, L, K-1);
for(int j = L; j--;)
Ans[CV[i][j]] += cnt[j];
}
for(int i = Cn; i--;) CV[i].clear();
Cn = 0;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> K;
for(int i = 1; i <= N; i++) cin >> A[i];
vector<int> V;
int L;
{
int i = 1;
while(!chk[i]) {
chk[i] = true;
V.eb(i);
i = A[i];
}
L = sz(V) - findx(V, i);
}
const int W = V[sz(V) - L + (K - (sz(V) - L)) % L];
Ans[W] += ll(N - sz(V)) * N;
for(int i = sz(V); i--;)
solve_naive(V[i], K - i);
for(int i = 1; i <= N; i++)
cout << Ans[i] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
13 ms |
480 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
15 ms |
596 KB |
Output is correct |
18 |
Correct |
336 ms |
828 KB |
Output is correct |
19 |
Correct |
194 ms |
704 KB |
Output is correct |
20 |
Correct |
169 ms |
800 KB |
Output is correct |
21 |
Correct |
341 ms |
828 KB |
Output is correct |
22 |
Correct |
170 ms |
732 KB |
Output is correct |
23 |
Correct |
317 ms |
848 KB |
Output is correct |
24 |
Correct |
143 ms |
748 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
347 ms |
804 KB |
Output is correct |
27 |
Correct |
199 ms |
668 KB |
Output is correct |
28 |
Correct |
168 ms |
656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
4 ms |
1248 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
13 ms |
480 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
15 ms |
596 KB |
Output is correct |
18 |
Correct |
336 ms |
828 KB |
Output is correct |
19 |
Correct |
194 ms |
704 KB |
Output is correct |
20 |
Correct |
169 ms |
800 KB |
Output is correct |
21 |
Correct |
341 ms |
828 KB |
Output is correct |
22 |
Correct |
170 ms |
732 KB |
Output is correct |
23 |
Correct |
317 ms |
848 KB |
Output is correct |
24 |
Correct |
143 ms |
748 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
347 ms |
804 KB |
Output is correct |
27 |
Correct |
199 ms |
668 KB |
Output is correct |
28 |
Correct |
168 ms |
656 KB |
Output is correct |
29 |
Runtime error |
4 ms |
1248 KB |
Execution killed with signal 11 |
30 |
Halted |
0 ms |
0 KB |
- |