Submission #830469

#TimeUsernameProblemLanguageResultExecution timeMemory
830469caganyanmazThousands Islands (IOI22_islands)C++17
1.75 / 100
33 ms10324 KiB
#include <bits/stdc++.h>
#define mp(x...) array<int, 2>({x})
#define pb push_back
#include "islands.h"
using namespace std;

#ifdef DEBUGGING
#include "../debug.h"
#else
#define debug(x...) void(42)
#endif

int n, m;
vector<int> u, v;
variant<bool, vector<int>> subtask1()
{
	if (n <= 2)
		return false;
	array<array<int, 3>, 3> a;
	for (int i = 0; i < m; i++)
		if (u[i] <= 2 && v[i] <= 2)
			a[u[i]][v[i]] = i;	
	debug(a);
	return vector<int>({a[0][1], a[1][2], a[2][0], a[0][2], a[2][1], a[1][0], a[2][0], a[1][2], a[0][1], a[1][0], a[2][1], a[0][2]});
}

constexpr static int MXN = 1e5;

bitset<MXN> visited;
vector<array<int, 2>> res;
vector<array<int, 2>> g[MXN];

bool dfs(int node, int le)
{
	res.pb(mp(node, le));
	visited[node] = true;
	for (auto [c, e] : g[node])
	{
		if ((e/2) == (le/2))
			continue;
		if (visited[c])
		{
			res.pb({c, e});
			return true;
		}
		else if (dfs(c, e))
		{
			return true;
		}
	}
	res.pop_back();
	return false;
}

static inline int get_reverse(int i)
{
	return (i / 2) * 2 + 1 - (i&1);
}

variant<bool, vector<int>> subtask2()
{
	for (int i = 0; i < m; i++)
	{
		debug(i, u[i], v[i]);
		g[u[i]].pb(mp(v[i], i));
	}
	if(dfs(0, -1))
	{
		vector<int> out;
		debug(res);
		int lsn = res.back()[0];
		// Go to the loop
		int i;
		for (i = 0; res[i][0] != lsn; i++) 
			if (i != 0)
				out.pb(res[i][1]);
		debug(out);
		// Cycle in loop
		for (int j = i+1; j < res.size(); j++)
			out.pb(res[j][1]);
		debug(out);
		// Cycle back in loop
		for (int j = res.size()-1; j > i; j--)
			out.pb(get_reverse(res[j][1]));
		debug(out);
		// Cycle in loop again
		for (int j = i+1; j < res.size(); j++)
			out.pb(res[j][1]);
		debug(out);
		// Cycle back again
		for (int j = res.size()-1; j > i; j--)
			out.pb(get_reverse(res[j][1]));
		debug(out);
		// Come back
		for (int j = i; j >= 1; j--)
			out.pb(res[j][1]);
		debug(out);
		return out;
	}
	return false;
}

variant<bool, vector<int>> find_journey(int N, int M, vector<int> U, vector<int> V) 
{
	n = N;
	m = M;
	u = U;
	v = V;
	return subtask2();
	assert(false);
}

Compilation message (stderr)

islands.cpp: In function 'std::variant<bool, std::vector<int, std::allocator<int> > > subtask2()':
islands.cpp:79:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |   for (int j = i+1; j < res.size(); j++)
      |                     ~~^~~~~~~~~~~~
islands.cpp:87:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |   for (int j = i+1; j < res.size(); j++)
      |                     ~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...