# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
282591 | AMO5 | Političari (COCI20_politicari) | C++17 | 20 ms | 2560 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define eb emplace_back
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define MOD 1000000007
typedef long long ll;
typedef pair <int, int> ii;
typedef pair <ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;
const ll INF=63;
const int mxn=505;
bool DEBUG=0;
int n,a[mxn][mxn],vis[mxn][mxn];
ll k;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
//freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
cin>>n>>k;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
cin>>a[i][j];
}
}
if(k==1){
cout<<1<<"\n";
return 0;
}
if(k==2){
cout<<2<<"\n";
return 0;
}
int ptr=3,now=2,prev=1;
vi list;
while(!vis[now][prev]){
//cerr<<ptr<<" "<<now<<" "<<prev<<"\n";
vis[now][prev]=ptr;
int pr = now;
now=a[now][prev];
prev = pr;
if(ptr==k){
cout<<now<<"\n";
return 0;
}
ptr++;
list.eb(now);
}
k-=vis[now][prev];
ll siz = 1LL*(ptr-vis[now][prev]);
k=k%siz;
cout<<list[k+vis[now][prev]-1-2];
return 0;
}
// READ & UNDERSTAND
// ll, int overflow, array bounds, memset(0)
// special cases (n=1?), n+1 (1-index)
// do smth instead of nothing & stay organized
// WRITE STUFF DOWN
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |