-
Notifications
You must be signed in to change notification settings - Fork 0
/
6584.cpp
103 lines (98 loc) · 2.25 KB
/
6584.cpp
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
#include <cstdio>
#include <cassert>
#include <functional>
#include <algorithm>
#include <utility>
#include <vector>
using namespace std;
const int maxn = 500000 + 5;
int n, k, fa[maxn], ans[maxn], dep[maxn];
vector<int> edge[maxn];
int pre[maxn];
int find(int x) {
return x == pre[x] ? x : pre[x] = find(pre[x]);
}
bool join(int x, int y) {
x = find(x); y = find(y);
if (x == y) return false;
pre[x] = y;
return true;
}
int a[maxn];
void update(int i, int x) {
for (; i < maxn; i += i & -i) a[i] += x;
}
int query(int i) {
if (i > n) i = n;
if (i <= 0) return 0;
int r = 0;
for (; i; i -= i & -i) r += a[i];
return r;
}
int query(int l, int r) {
if (l > r) return 0;
return query(r) - query(l - 1);
}
vector<int> ri;
bool vis[maxn], ring[maxn];
void getring(int u) {
vis[u] = true;
for (int v: edge[u]) {
if (vis[v]) {
if (!ri.empty()) continue;
int x = u;
while (x != v) {
ring[x] = true;
ri.push_back(x);
x = fa[x];
}
ring[v] = true;
ri.push_back(v);
continue;
}
dep[v] = dep[u] + 1;
getring(v);
}
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) pre[i] = i;
vector<int> rts;
for (int i = 1; i <= n; i++) {
scanf("%d", fa + i);
edge[fa[i]].push_back(i);
if (!join(fa[i], i)) {
rts.push_back(i);
}
}
for (int rt: rts) {
dep[rt] = 1; ri.clear();
getring(rt);
for (int x: ri) {
ans[x] -= query(1, k - ((int)ri.size() - dep[x]));
}
function<void(int)> dfs = [&](int u) {
if (ring[u]) {
ans[u] -= query(max(dep[u], 1 + k - ((int)ri.size() - dep[u])), dep[u] + k);
} else {
ans[u] -= query(dep[u], dep[u] + k);
}
update(dep[u], 1);
for (int v: edge[u]) {
if (v == rt) continue;
dfs(v);
}
if (ring[u]) {
ans[u] += query(max(dep[u], 1 + k - ((int)ri.size() - dep[u])), dep[u] + k);
} else {
ans[u] += query(dep[u], dep[u] + k);
}
};
dfs(rt);
for (int x: ri) {
ans[x] += query(1, k - ((int)ri.size() - dep[x]));
}
}
for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
return 0;
}