This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100000
#define L 60 /* L = ceil(log2(10^18)) */
int pp[N], pp_[L + 1][N]; long long cnt[N], cnt_[L + 1][N];
void mark(int *pp, int n, long long k) {
static int tt[N];
int i, t;
memset(tt, 0, n * sizeof *tt);
i = 0, t = 0;
while (!tt[i])
tt[i] = t++, i = pp[i];
k = (k - t) % (t - tt[i]);
while (k--)
i = pp[i];
cnt[i]++;
}
int *ej[N], eo[N];
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;
}
int tt[N], tt_[N];
void dfs(int i, int t) {
int o;
if (tt_[i] != -1)
return;
tt_[i] = t;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
dfs(j, t);
}
}
void update(int i, long long k, int x) {
int l;
for (l = L; l >= 0; l--)
if (k >= 1LL << l)
cnt_[l][i] += x, k -= 1LL << l, i = pp_[l][i];
}
int main() {
static int ii[N + 1];
int n, h, i, j, t, l;
long long k, 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);
}
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, dfs(i, t);
ii[t] = i;
for (i = 0; i < n; i++)
if (tt_[i] == -1)
tt_[i] = t;
j = ii[t], k_ = (k - t) % (t - tt[ii[t]]);
while (k_--)
j = pp[j];
for (i = 0; i < n; i++)
if (tt[i] == -1)
cnt[j] += n;
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]];
for (i = 0; i < n; i++) {
update(i, k, 1), update(i, k - tt_[i], -1);
for (h = tt_[i]; h < t; h++)
pp[ii[h]] = i, mark(pp, n, k), pp[ii[h]] = ii[h + 1];
}
for (l = L; l > 0; l--)
for (i = 0; i < n; i++)
cnt_[l - 1][i] += cnt_[l][i], cnt_[l - 1][pp_[l - 1][i]] += cnt_[l][i];
for (i = 0; i < n; i++)
cnt[i] += cnt_[0][i];
for (i = 0; i < n; i++)
printf("%lld\n", cnt[i]);
return 0;
}
Compilation message (stderr)
space_pirate.c: In function 'append':
space_pirate.c:29:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
29 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
space_pirate.c: In function 'main':
space_pirate.c:62:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | scanf("%d%lld", &n, &k);
| ^~~~~~~~~~~~~~~~~~~~~~~
space_pirate.c:66:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | scanf("%d", &pp[i]), pp[i]--;
| ^~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |