Submission #731454

# Submission time Handle Problem Language Result Execution time Memory
731454 2023-04-27T12:54:40 Z eneskav Političari (COCI20_politicari) C++17
70 / 70
17 ms 4204 KB
#include <iostream>
#include <vector>
#include <cstring>
#define int long long
using namespace std;
#define fastio                        \
	ios_base::sync_with_stdio(false); \
	cin.tie(NULL);                    \
	cout.tie(NULL);
signed main()
{
	fastio;
  
	int n, k;
	cin >> n >> k;
	int a[n + 1][n + 1];
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= n; ++j)
			cin >> a[i][j];
	if (k <= 2)
	{
		cout << k;
		return 0;
	}
	int vis[n + 1][n + 1];
	memset(vis, 0, sizeof(vis));
	int p = 0, c = 1;
	vector<int> v;
	v.push_back(1);
	a[c][p] = 2;
	int cycle = 0;
	for (int i = 1; i < k * 3; ++i)
	{
		int t = c;
		c = a[c][p];
		p = t;
		if (vis[c][p])
		{
			cycle = vis[c][p];
			v.pop_back();
			break;
		}
		vis[c][p] = i;
		v.push_back(c);
	}
	// print v
	/* for (auto i : v)
		cout << i << " ";
	cout << endl; */
	--cycle;
	// cout << "cycle " << cycle << endl;
	// cout << "cycle length " << v.size() - cycle << endl;
	if (k < v.size())
	{
		cout << v[k - 1];
		return 0;
	}
	// cout << v[((k - cycle) - 1) % (v.size() - cycle)];
	//  the code above is wrong, it should be like this
	cout << v[(k - cycle - 1) % (v.size() - cycle) + cycle];
	return 0;
}

Compilation message

politicari.cpp: In function 'int main()':
politicari.cpp:53:8: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |  if (k < v.size())
      |      ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 4 ms 980 KB Output is correct
3 Correct 11 ms 2772 KB Output is correct
4 Correct 14 ms 3412 KB Output is correct
5 Correct 17 ms 4200 KB Output is correct
6 Correct 17 ms 4180 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 4 ms 1108 KB Output is correct
10 Correct 12 ms 3412 KB Output is correct
11 Correct 16 ms 4204 KB Output is correct
12 Correct 16 ms 4200 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 2 ms 596 KB Output is correct