#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int M = 3010;
int E[M], n;
vector<int> R[M];
ll k;
ll ans[M];
int ck[M], in_cycle[M];
vector<vector<int>> cycle;
void find_cycle(int x){
vector<int> V;
while(!ck[x]) {
ck[x] = 1;
V.push_back(x);
x = E[x];
}
int p = find(V.begin(),V.end(),x) - V.begin();
if(p>=V.size()) return;
cycle.push_back({});
for(int i = p;i<V.size();i++) {
in_cycle[V[i]]=1;
cycle.back().push_back(V[i]);
}
}
void dfs(int x,ll dist,int dep, vector<int> &L){ //
ck[x] = 1;
L.push_back(x);
//fi[v] 가 나에게 온다면?
ans[L[(L.size() - (dist%L.size()))%L.size()]]++;
for(auto i : R[x]) {
dfs(i,dist,dep+1,L);
}
L.pop_back();
}
void dfs2(int x,int s,int dep,ll dist,int ver){
ans[cycle[ver][(s-dep+dist)%cycle[ver].size()]]++;
for(auto i : R[x]) if(!in_cycle[i]) {
dfs2(i,s,dep+1,dist,ver);
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
for(int i = 1;i<=n;i++){
cin >> E[i];
}
vector<int> first_cycle;
int p = 1;
while(!ck[p]){
ck[p] = 1;
first_cycle.push_back(p);
p = E[p];
}
int p1 = find(first_cycle.begin(),first_cycle.end(),p) - first_cycle.begin();
int w = first_cycle[(k-p1)%(first_cycle.size()-p1)+p1];
ans[w] += ((ll)n-first_cycle.size())*n;
for(int v = 0;v<first_cycle.size();v++) {
// first[v]에서 가는 간선은 이제 없다.
for(int i = 1;i<=n;i++) ck[i] = 0;
for(int i = 1;i<=n;i++) in_cycle[i] = 0;
for(int i = 1;i<=n;i++) R[i].clear();
cycle.clear();
for(int i = 1;i<=n;i++) if(i^first_cycle[v]){
R[E[i]].push_back(i);
}
vector<int> L;
dfs(first_cycle[v],k-v,0,L);
// 내 트리로 가는게 아닌 경우
for(int i = 1;i<=n;i++) if(!ck[i])
find_cycle(i);
ll dist = k-v-1;
for(int i = 0;i<cycle.size();i++)
for(int j = 0;j<cycle[i].size();j++)
dfs2(cycle[i][j],j,0,dist,i);
}
for(int i = 1;i<=n;i++) cout << ans[i] << '\n';
}
Compilation message
space_pirate.cpp: In function 'void find_cycle(int)':
space_pirate.cpp:19:6: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | if(p>=V.size()) return;
| ~^~~~~~~~~~
space_pirate.cpp:21:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for(int i = p;i<V.size();i++) {
| ~^~~~~~~~~
space_pirate.cpp: In function 'int main()':
space_pirate.cpp:59:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int v = 0;v<first_cycle.size();v++) {
| ~^~~~~~~~~~~~~~~~~~~
space_pirate.cpp:76:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for(int i = 0;i<cycle.size();i++)
| ~^~~~~~~~~~~~~
space_pirate.cpp:77:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for(int j = 0;j<cycle[i].size();j++)
| ~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
15 ms |
524 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
18 ms |
516 KB |
Output is correct |
18 |
Correct |
468 ms |
756 KB |
Output is correct |
19 |
Correct |
219 ms |
716 KB |
Output is correct |
20 |
Correct |
238 ms |
820 KB |
Output is correct |
21 |
Correct |
444 ms |
728 KB |
Output is correct |
22 |
Correct |
204 ms |
660 KB |
Output is correct |
23 |
Correct |
422 ms |
748 KB |
Output is correct |
24 |
Correct |
185 ms |
652 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
467 ms |
756 KB |
Output is correct |
27 |
Correct |
222 ms |
564 KB |
Output is correct |
28 |
Correct |
200 ms |
692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
596 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
15 ms |
524 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
18 ms |
516 KB |
Output is correct |
18 |
Correct |
468 ms |
756 KB |
Output is correct |
19 |
Correct |
219 ms |
716 KB |
Output is correct |
20 |
Correct |
238 ms |
820 KB |
Output is correct |
21 |
Correct |
444 ms |
728 KB |
Output is correct |
22 |
Correct |
204 ms |
660 KB |
Output is correct |
23 |
Correct |
422 ms |
748 KB |
Output is correct |
24 |
Correct |
185 ms |
652 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
467 ms |
756 KB |
Output is correct |
27 |
Correct |
222 ms |
564 KB |
Output is correct |
28 |
Correct |
200 ms |
692 KB |
Output is correct |
29 |
Runtime error |
1 ms |
596 KB |
Execution killed with signal 11 |
30 |
Halted |
0 ms |
0 KB |
- |