#include <bits/stdc++.h>
using namespace std;
const int N = 500 + 5;
long long n, k, graph[N][N], dist[N][N];
struct Item{
long long a, b;
};
Item cur = {1, 2};
bool flag;
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin >> n >> k;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
cin >> graph[i][j];
}
}
if(k == 1) cout << 1;
else{
dist[1][2] = 2, k -= 2;
while(k > 0){
k--;
if(flag) cur = {cur.b, graph[cur.b][cur.a]};
else{
if(dist[cur.b][graph[cur.b][cur.a]] == 0){
dist[cur.b][graph[cur.b][cur.a]] = dist[cur.a][cur.b] + 1;
cur = {cur.b, graph[cur.b][cur.a]};
}
else{
k %= dist[cur.a][cur.b] - dist[cur.b][graph[cur.b][cur.a]] + 1;
cur = {cur.b, graph[cur.b][cur.a]};
flag = true;
}
}
}
}
cout << cur.b;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Correct |
4 ms |
2148 KB |
Output is correct |
3 |
Correct |
10 ms |
3284 KB |
Output is correct |
4 |
Correct |
13 ms |
3668 KB |
Output is correct |
5 |
Correct |
18 ms |
4124 KB |
Output is correct |
6 |
Correct |
16 ms |
3788 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
1232 KB |
Output is correct |
9 |
Correct |
5 ms |
2260 KB |
Output is correct |
10 |
Correct |
15 ms |
3540 KB |
Output is correct |
11 |
Correct |
16 ms |
4300 KB |
Output is correct |
12 |
Correct |
16 ms |
4264 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
1236 KB |
Output is correct |