# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
751911 | kenansalloum | Thousands Islands (IOI22_islands) | 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.
#include <vector>
#include <unordered_set>
using namespace std;
bool dfs(int curr, int prev, vector<vector<int>>& adjList, vector<int>& journey, unordered_set<int>& usedCanoes) {
journey.push_back(curr);
if (curr == 0 && journey.size() > 1) {
return true;
}
for (int next : adjList[curr]) {
if (next != prev && !usedCanoes.count(next)) {
usedCanoes.insert(next);
if (dfs(next, curr, adjList, journey, usedCanoes)) {
return true;
}
usedCanoes.erase(next);
}
}
journey.pop_back();
return false;
}
vector<int> find_journey(int N, int M, vector<int>& U, vector<int>& V) {
vector<vector<int>> adjList(N);
for (int i = 0; i < M; i++) {
adjList[U[i]].push_back(i);
adjList[V[i]].push_back(i);
}
vector<int> journey;
unordered_set<int> usedCanoes;
dfs(0, -1, adjList, journey, usedCanoes);
if (journey.size() <= 2000000) {
return journey;
} else {
return {};
}
}