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 "islands.h"
#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int>>> adj;
vector<int> visited;
bool ok;
vector<int> find_cycle(int s, set<int>&path)
{
bool skip = false;
vector<int> cycle;
int i = s;
do
{
for (auto node : adj[i])
{
if (!path.count(node.first)) continue;
cycle.push_back(node.second);
i = node.first;
break;
}
}
while (i != s);
const int sz = cycle.size();
for (int i = 0; i < sz; i++)
cycle.push_back(cycle[i] ^ 1);
for (int i = 2 * sz - 1 ; i >= 0; i--)
cycle.push_back(cycle[i] ^ 1);
return cycle;
}
void DFS(int index, set<int>&path, vector<int>&v)
{
visited[index] = 1;
path.insert(index);
for (auto node : adj[index])
{
if (path.count(node.first))
{
ok = true;
vector<int> cycle = find_cycle(node.first, path);
const int sz = cycle.size();
for (int i = 0; i < sz / 4 - 1; i++)
v.pop_back();
v.insert(v.end(), cycle.begin(), cycle.end());
for (int i = v.size() - sz - 1; i >= 0; i--)
v.push_back(v[i]);
return;
}
if (visited[node.first]) continue;
v.push_back(node.second);
DFS(node.first, path, v);
if (ok) return;
v.pop_back();
}
if (ok) return;
path.erase(index);
}
variant<bool, vector<int>> find_journey(int N, int M, vector<int> U, vector<int> V)
{
adj = vector<vector<pair<int, int>>>(N);
set<pair<int, int>> s;
for (int i = 0; i < M; i += 2)
{
if (s.count({U[i], V[i]})) continue;
adj[U[i]].push_back({V[i], i});
s.insert({U[i], V[i]});
}
ok = false;
visited = vector<int>(N);
set<int> path;
vector<int> res;
DFS(0, path, res);
if (ok)
return res;
return false;
}
Compilation message (stderr)
islands.cpp: In function 'std::vector<int> find_cycle(int, std::set<int>&)':
islands.cpp:11:8: warning: unused variable 'skip' [-Wunused-variable]
11 | bool skip = false;
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |