# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
732255 | vjudge1 | Političari (COCI20_politicari) | C++17 | 0 ms | 0 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.
// author: MisterReaper (Ahmet Alp Orakci)
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sz(x) (int) x.size()
#ifndef ONLINE_JUDGE
#include "debug.h"
#define OPEN freopen(".in", "r", stdin); freopen(".out", "w", stdout);
#define TIME cerr << "\n" << fixed << setprecision(2) << 1000.0 * clock() / CLOCKS_PER_SEC << " milliseconds ";
#else
#define debug(...) void(23)
#define OPEN void(0000)
#define TIME void(232323233)
#endif
void solve()
{
int n, k; cin >> n >> k;
int arr[n +1][n +1], vis[n +1][n +1];
memset(vis, -1, sizeof(vis));
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
cin >> arr[j][i];
}
}
vector <int> v;
v.emplace_back(1);
v.emplace_back(2);
while(true)
{
if(vis[v[sz(v) - 2]][v.back()] != -1) break;
vis[v[sz(v) - 2]][v.back()] = sz(v) -2;
v.emplace_back(arr[v[sz(v) - 2]][v.back()]);
}
debug(v);
if(k < sz(v))
{
cout << v[k -1];
return;
}
k -= vis[v[sz(v) -2]][v.back()];
vector <int> ans;
for(int i = vis[v[sz(v) -2]][v.back()]; i < sz(v) -2; i++) ans.emplace_back(v[i]);
debug(ans);
k %= sz(ans);
cout << ans[(k -1 + sz(ans)) % sz(ans)];
return;
}
int32_t main()
{
OPEN;
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t = 1; //cin >> t;
while(t--)
{
solve();
}
TIME;
return 0;
}