#include <bits/stdc++.h>
using namespace std;
#define FOR(i, j, k, l) for(int i = (j); i < (k); i += (l))
#define FORD(i, j, k, l) for(int i = (j); i >= (k); i -= (l))
#define REP(i, n) FOR(i, 0, n, 1)
#define REPD(i, n) FORD(i, n, 0, 1)
#define pb push_back
const int INF = (int)1e9;
typedef long long ll;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n; ll k;
cin >> n >> k;
vector<int> g(n);
REP(i, n){
cin >> g[i];
g[i]--;
}
vector<vector<int>> dist(n, vector<int>(n, -1));
vector<vector<int>> revdist(n, vector<int>(n, -1)); // who is at dist x?
vector<int> maxdist(n);
function<void(int, int, int)> dfs = [&](int v, int d, int s){
dist[s][v] = d;
if(dist[s][g[v]] == -1){
dfs(g[v], d + 1, s);
}
};
REP(i, n){
dfs(i, 0, i);
}
REP(i, n){
REP(j, n){
if(dist[i][j] != -1)
revdist[i][dist[i][j]] = j;
}
}
REP(i, n){
maxdist[i] = *max_element(dist[i].begin(), dist[i].end());
}
auto go = [&](int v, ll k){ // in default graph;;
if(maxdist[v] == 0)
return v;
if(maxdist[v] >= k){
return revdist[v][k];
}
ll kk = k - maxdist[v];
v = revdist[v][maxdist[v]];
if(maxdist[v] == 0)
return v;
int u = revdist[v][1];
ll cycle_size = 1 + dist[u][v];
kk %= cycle_size;
if(kk == 0)
return v;
else
return revdist[u][kk - 1];
};
vector<int> ans(n, 0);
int def = go(0, k);
REP(a, n){
REP(b, n){
if(dist[0][a] == -1){ // neviem sa dostat do zaciatku novej hrany
ans[def]++;
}
else if(dist[b][a] == -1){ // nevytvorim cyklus
ans[go(b, k - dist[0][a] - 1)]++;
}
else if(a == b){ // rovnake
ans[a]++;
}
else{ // vytvorim cyklus; pojdem do a
ll kk = k - dist[0][a];
ll cycle_size = 1 + dist[b][a];
kk %= cycle_size;
if(kk == 0)
ans[a]++;
else
ans[go(b, kk - 1)]++;
}
//cout << a << " " << b << " " << ans[0] << "\n";
}
}
REP(i, n){
cout << ans[i] << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
117 ms |
70972 KB |
Output is correct |
16 |
Correct |
87 ms |
70980 KB |
Output is correct |
17 |
Correct |
141 ms |
71024 KB |
Output is correct |
18 |
Correct |
1855 ms |
71124 KB |
Output is correct |
19 |
Correct |
1063 ms |
71064 KB |
Output is correct |
20 |
Correct |
895 ms |
71244 KB |
Output is correct |
21 |
Correct |
1427 ms |
71236 KB |
Output is correct |
22 |
Correct |
646 ms |
71112 KB |
Output is correct |
23 |
Correct |
1084 ms |
71184 KB |
Output is correct |
24 |
Correct |
404 ms |
70980 KB |
Output is correct |
25 |
Correct |
92 ms |
71048 KB |
Output is correct |
26 |
Correct |
1607 ms |
71160 KB |
Output is correct |
27 |
Correct |
554 ms |
71056 KB |
Output is correct |
28 |
Correct |
719 ms |
71108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
256 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
117 ms |
70972 KB |
Output is correct |
16 |
Correct |
87 ms |
70980 KB |
Output is correct |
17 |
Correct |
141 ms |
71024 KB |
Output is correct |
18 |
Correct |
1855 ms |
71124 KB |
Output is correct |
19 |
Correct |
1063 ms |
71064 KB |
Output is correct |
20 |
Correct |
895 ms |
71244 KB |
Output is correct |
21 |
Correct |
1427 ms |
71236 KB |
Output is correct |
22 |
Correct |
646 ms |
71112 KB |
Output is correct |
23 |
Correct |
1084 ms |
71184 KB |
Output is correct |
24 |
Correct |
404 ms |
70980 KB |
Output is correct |
25 |
Correct |
92 ms |
71048 KB |
Output is correct |
26 |
Correct |
1607 ms |
71160 KB |
Output is correct |
27 |
Correct |
554 ms |
71056 KB |
Output is correct |
28 |
Correct |
719 ms |
71108 KB |
Output is correct |
29 |
Runtime error |
256 ms |
524292 KB |
Execution killed with signal 9 |
30 |
Halted |
0 ms |
0 KB |
- |