#include <bits/stdc++.h>
#define sz(x) ((int)x.size())
#define pii pair<int,int>
#define ft first
#define sd second
#define all(x) x.begin(),x.end()
#define PB push_back
#define MP make_pair
using namespace std;
typedef long long ll;
const int N = 510;
vector<pii> vc, cyc;
ll k;
int n, nt[N][N], mrk[N][N], tim[N][N], tt = 0;
void CYCLE(pii lst){
cyc.clear();
vc.PB(MP(-1, -1));
do {
vc.pop_back();
cyc.PB(vc.back());
} while (vc.back() != lst);
reverse(all(cyc));
k -= ll(tim[lst.ft][lst.sd]);
k %= sz(cyc);
cout << cyc[k].ft + 1;
exit(0);
}
void dfs(int x, int y){
tt++;
tim[x][y] = tt;
if (ll(tt) == k){
cout << x + 1;
exit(0);
}
vc.PB(MP(x, y));
mrk[x][y] = 1;
int nw = nt[x][y];
if (mrk[nw][x] == 0)
dfs(nw, x); else
if (mrk[nw][x] == 1)
CYCLE(MP(nw, x));
mrk[x][y] = 2;
vc.pop_back();
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("in.txt","r",stdin);
cin >> n >> k;
if (k == 1){
cout << 1;
return 0;
}
k--;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++){
cin >> nt[i][j];
nt[i][j]--;
}
dfs(1, 0);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
8 ms |
1920 KB |
Output is correct |
3 |
Correct |
17 ms |
2944 KB |
Output is correct |
4 |
Correct |
28 ms |
3456 KB |
Output is correct |
5 |
Correct |
24 ms |
3712 KB |
Output is correct |
6 |
Correct |
23 ms |
3456 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
6 ms |
1152 KB |
Output is correct |
9 |
Correct |
9 ms |
1920 KB |
Output is correct |
10 |
Correct |
19 ms |
3200 KB |
Output is correct |
11 |
Correct |
23 ms |
3968 KB |
Output is correct |
12 |
Correct |
24 ms |
3832 KB |
Output is correct |
13 |
Correct |
5 ms |
640 KB |
Output is correct |
14 |
Correct |
6 ms |
1152 KB |
Output is correct |