#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100000
#define L 60 /* L = ceil(log2(10^18)) */
int *ej[N], eo[N], pp[N], pp_[L + 1][N], ii[N + 1], tt[N], tt_[N], n; long long cc[N], cc_[L + 1][N], k;
void append(int i, int j) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ej[i][o] = j;
}
void dfs1(int i, int t) {
int o;
if (tt_[i] != -1)
return;
tt_[i] = t;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
dfs1(j, t);
}
}
void update(int i, long long k, int x) {
int l;
for (l = L; l >= 0; l--)
if (k >= 1LL << l)
cc_[l][i] += x, k -= 1LL << l, i = pp_[l][i];
}
int qu[N * 2];
void dfs2(int p, int i, int h_, int cnt) {
int o, h;
qu[cnt++] = i;
for (h = cnt - 1; h >= h_; h--)
if (tt[qu[h]] != -1)
cc[qu[cnt - 1 - (k - tt[qu[h]] - 1) % (cnt - h)]]++;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p)
dfs2(i, j, h_, cnt);
}
}
int main() {
int h, i, i_, j, t, l, cnt;
long long k_;
scanf("%d%lld", &n, &k);
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
for (i = 0; i < n; i++) {
scanf("%d", &pp[i]), pp[i]--;
append(pp[i], i);
}
for (i = 0; i < n; i++)
pp_[0][i] = pp[i];
for (l = 1; l <= L; l++)
for (i = 0; i < n; i++)
pp_[l][i] = pp_[l - 1][pp_[l - 1][i]];
memset(tt, -1, n * sizeof *tt), memset(tt_, -1, n * sizeof *tt_);
for (i = 0, t = 0; tt[i] == -1; i = pp[i], t++)
ii[tt[i] = t] = i, dfs1(i, t);
ii[t] = i_ = i;
for (i = 0; i < n; i++)
if (tt_[i] == -1)
tt_[i] = t;
j = i_, k_ = (k - t) % (t - tt[i_]);
while (k_--)
j = pp[j];
for (i = 0; i < n; i++)
if (tt[i] == -1)
cc[j] += n;
for (i = 0; i < n; i++)
update(i, k, 1), update(i, k - tt_[i], -1);
cnt = 0;
for (h = t - 1; h > tt[i_]; h--)
qu[cnt++] = ii[h];
for (h = t; h > tt[i_]; h--)
dfs2(ii[h - 1], ii[h], cnt - (t - tt[i_]) + 1, cnt), qu[cnt++] = ii[h];
for (l = L; l > 0; l--)
for (i = 0; i < n; i++)
cc_[l - 1][i] += cc_[l][i], cc_[l - 1][pp_[l - 1][i]] += cc_[l][i];
for (i = 0; i < n; i++)
cc[i] += cc_[0][i];
for (i = 0; i < n; i++)
printf("%lld\n", cc[i]);
return 0;
}
Compilation message
space_pirate.c: In function 'append':
space_pirate.c:13:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
13 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
space_pirate.c: In function 'main':
space_pirate.c:60:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | scanf("%d%lld", &n, &k);
| ^~~~~~~~~~~~~~~~~~~~~~~
space_pirate.c:64:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | scanf("%d", &pp[i]), pp[i]--;
| ^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
992 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
992 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
6 ms |
3156 KB |
Output is correct |
16 |
Correct |
4 ms |
3156 KB |
Output is correct |
17 |
Correct |
10 ms |
3188 KB |
Output is correct |
18 |
Correct |
167 ms |
3332 KB |
Output is correct |
19 |
Correct |
49 ms |
3356 KB |
Output is correct |
20 |
Correct |
88 ms |
3360 KB |
Output is correct |
21 |
Correct |
95 ms |
3472 KB |
Output is correct |
22 |
Correct |
34 ms |
3352 KB |
Output is correct |
23 |
Correct |
91 ms |
3436 KB |
Output is correct |
24 |
Correct |
24 ms |
3292 KB |
Output is correct |
25 |
Correct |
4 ms |
3156 KB |
Output is correct |
26 |
Correct |
170 ms |
3448 KB |
Output is correct |
27 |
Correct |
38 ms |
3232 KB |
Output is correct |
28 |
Correct |
61 ms |
3244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
436 ms |
77724 KB |
Output is correct |
2 |
Execution timed out |
2095 ms |
56220 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
992 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
6 ms |
3156 KB |
Output is correct |
16 |
Correct |
4 ms |
3156 KB |
Output is correct |
17 |
Correct |
10 ms |
3188 KB |
Output is correct |
18 |
Correct |
167 ms |
3332 KB |
Output is correct |
19 |
Correct |
49 ms |
3356 KB |
Output is correct |
20 |
Correct |
88 ms |
3360 KB |
Output is correct |
21 |
Correct |
95 ms |
3472 KB |
Output is correct |
22 |
Correct |
34 ms |
3352 KB |
Output is correct |
23 |
Correct |
91 ms |
3436 KB |
Output is correct |
24 |
Correct |
24 ms |
3292 KB |
Output is correct |
25 |
Correct |
4 ms |
3156 KB |
Output is correct |
26 |
Correct |
170 ms |
3448 KB |
Output is correct |
27 |
Correct |
38 ms |
3232 KB |
Output is correct |
28 |
Correct |
61 ms |
3244 KB |
Output is correct |
29 |
Correct |
436 ms |
77724 KB |
Output is correct |
30 |
Execution timed out |
2095 ms |
56220 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |