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 <variant>
#include <vector>
#include "bits/stdc++.h"
using namespace std;
using g_t = vector<vector<pair<int,int>>>;
int dfs(int n, int p, g_t& g, vector<bool>& vis, vector<int>& re, vector<int>& oRe) {
vis[n] = true;
vector<int> doTh;
for (auto [ne, nu]: g[n]) {
if (nu/2 == p/2) continue;
if (vis[ne]) {
re.push_back(nu);
return ne;
}
doTh.push_back(nu);
int res = dfs(ne, nu, g, vis, re, oRe);
if (re.empty() && oRe.empty()) continue;
if (!re.empty()) {
re.push_back(nu);
if (res == n) {
re.push_back(-1);
return -2;
}
} else {
oRe.push_back(nu);
}
return res;
}
if (doTh.size() > 1) {
doTh.resize(2);
oRe = doTh;
}
return -2;
}
std::variant<bool, std::vector<int>> find_journey(
int N, int M, std::vector<int> U, std::vector<int> V) {
if (N == 2) {
vector<int> s0, s1;
for (int i = 0; i < M; ++i) {
if (U[i] == 0) s0.push_back(i);
else s1.push_back(i);
}
if (s0.size() >= 2 && s1.size() >= 1) {
return vector<int>{s0[0], s1[0], s0[1], s0[0], s1[0], s0[1]};
} else return false;
}
g_t g(N);
for (int i = 0; i < M / 2; ++i) {
g[U[2*i]].push_back({V[2*i], 2*i});
g[V[2*i]].push_back({U[2*i], 2*i + 1});
}
vector<int> intr, oIntr;
vector<bool> vis(N, false);
dfs(0, -2, g, vis, intr, oIntr);
if (intr.empty() && oIntr.empty()) return false;
vector<int> res;
int lim = intr.size() - 1;
for (int i = intr.size() - 1; intr[i] != -1; --i) {
res.push_back(intr[i]);
lim = i - 1;
}
for (int i = lim - 1; i >= 0; --i) {
res.push_back(intr[i]);
}
for (int i = 0; i < lim; ++i) {
res.push_back(intr[i] - (2*(intr[i]%2) - 1));
}
for (int i = 0; i < lim; ++i) {
res.push_back(intr[i]);
}
for (int i = lim - 1; i >= 0; --i) {
res.push_back(intr[i] - (2*(intr[i]%2) - 1));
}
for (int i = lim + 1; i < intr.size(); ++i) {
res.push_back(intr[i]);
}
return res;
}
Compilation message (stderr)
islands.cpp: In function 'std::variant<bool, std::vector<int, std::allocator<int> > > find_journey(int, int, std::vector<int>, std::vector<int>)':
islands.cpp:81:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for (int i = lim + 1; i < intr.size(); ++i) {
| ~~^~~~~~~~~~~~~
# | 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... |