#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define crep(i,x,n) for(int i=x;i<n;i++)
#define drep(i,n) for(int i=n-1;i>=0;i--)
#define vec(...) vector<__VA_ARGS__>
#define _3qplfh5 ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
using pii=pair<int,int>;
using vi=vector<int>;
using vll=vector<long long>;
int main(){
_3qplfh5;
int n;
ll k;
cin>>n>>k;
vec(vi) a(n,vi(n));
rep(i,n){
rep(j,n){
cin>>a[i][j];
a[i][j]--;
}
}
k--;
vec(vi) usd(n,vi(n));
vec(pii) way;
int c=0,nec=1;
way.pb({c,nec});
usd[c][nec]=1;
vec(pii) incyc;
while(k>0){
int onec=nec;
// nec , a[nec][c]
int nxt=a[nec][c];
// printf("%d %d\n", nec,nxt);
if(usd[nec][nxt]){
pii p={nec,nxt};
while(sz(way)){
incyc.pb(way.back());
if(way.back()==p){
break;
}
way.pop_back();
}
break;
}
way.pb({nec,nxt});
usd[nec][nxt]=1;
nec=a[nec][c];
c=onec;
k--;
}
if(k==0){
printf("%d\n", c+1);
exit(0);
}
reverse(all(incyc));
k--;
k%=sz(incyc);
printf("%d\n", incyc[k].fi+1);
//
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
3 ms |
748 KB |
Output is correct |
3 |
Correct |
10 ms |
2016 KB |
Output is correct |
4 |
Correct |
12 ms |
2592 KB |
Output is correct |
5 |
Correct |
20 ms |
3172 KB |
Output is correct |
6 |
Correct |
15 ms |
3160 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
3 ms |
716 KB |
Output is correct |
10 |
Correct |
12 ms |
1892 KB |
Output is correct |
11 |
Correct |
14 ms |
2208 KB |
Output is correct |
12 |
Correct |
14 ms |
2252 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |