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) {
vis[n] = true;
for (auto [ne, nu]: g[n]) {
if (nu == p) continue;
if (vis[ne]) {
re.push_back(nu);
return ne;
}
int res = dfs(ne, nu, g, vis, re);
if (re.empty()) continue;
re.push_back(nu);
if (res == n) {
re.push_back(-1);
return -1;
}
return res;
}
return -1;
}
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], i});
g[V[2*i]].push_back({U[2*i], i});
}
vector<int> intr;
vector<bool> vis(N, false);
dfs(0, -1, g, vis, intr);
if (intr.empty()) return false;
vector<int> res;
int lim = 0;
for (int i = intr.size() - 1; intr[i] != -1; --i) {
res.push_back(2*intr[i]);
lim = i - 1;
}
if (U[0] == U[1]) {
for (int i = lim - 1; i >= 0; --i) {
res.push_back(2*intr[i]);
}
for (int i = lim - 1; i >= 0; --i) {
res.push_back(2*intr[i]);
}
for (int i = 0; i < lim; ++i) {
res.push_back(2*intr[i] + 1);
}
for (int i = 0; i < lim; ++i) {
res.push_back(2*intr[i] + 1);
}
} else {
}
for (int i = lim + 1; i < intr.size(); ++i) {
res.push_back(2*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:75:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | 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... |