-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1751.cpp
53 lines (49 loc) · 915 Bytes
/
1751.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
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, t[N], m, cmp[N], clen[N], r[N], dis[N];
vector<int> rg[N];
bool isroot[N];
void dfs(int u, int root) {
if (isroot[u]) root = u;
r[u] = root;
cmp[u] = m;
for (int v: rg[u]) {
if (!cmp[v]) {
dis[v] = dis[u] + 1;
dfs(v, root);
}
}
}
void process(int s) {
++m;
int x = t[s], y = t[t[s]];
while (x != y) {
x = t[x];
y = t[t[y]];
}
x = s;
while (x != y) {
x = t[x];
y = t[y];
}
do {
isroot[y] = true;
++clen[m];
y = t[y];
} while (y != x);
dfs(x, -1);
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> t[i];
rg[t[i]].emplace_back(i);
}
for (int i = 1; i <= n; ++i) {
if (!cmp[i]) process(i);
cout << dis[i] - dis[r[i]] + clen[cmp[i]] << " \n"[i == n];
}
return 0;
}