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 <variant>
#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int,int>>> adj;
vector<int> path;
vector<bool> used;
int loop = -1;
void dfs(int v) {
used[v] = true;
for (auto u: adj[v]) {
if (!used[u.first]) {
path.push_back(u.second);
dfs(u.first);
} else {
path.push_back(u.second);
loop = u.first;
break;
}
if (loop == -1) {
path.pop_back();
}
}
}
variant<bool,vector<int>> find_journey( int n, int m, vector<int> u, vector<int> v) {
adj.resize(n); used.resize(n);
for (int i=0;i<m;i++) {
adj[u[i]].push_back({v[i],i});
}
dfs(0);
if (loop == -1) {
return false;
}
vector<int> route,cycle;
bool inroute = true;
for (int i=0;i<route.size();i++) {
if (u[i] == loop) {
inroute = false;
}
if (inroute) {
route.push_back(path[i]);
} else {
cycle.push_back(path[i]);
}
}
int lenp = route.size();
vector<int> p1=route,b1=route,p2(lenp),b2,c1=cycle,d1=cycle;
reverse(b1.begin(),b1.end()); reverse(d1.begin(),d1.end());
for (int i=0;i<lenp;i++) {
p2[i] = p1[i]^1;
}
b2 = p2;
reverse(b2.begin(),b2.end());
vector<int> ans;
ans.insert(ans.end(),p1.begin(),p1.end());
ans.insert(ans.end(),c1.begin(),c1.end());
ans.insert(ans.end(),b1.begin(),b1.end());
ans.insert(ans.end(),p2.begin(),p2.end());
ans.insert(ans.end(),d1.begin(),d1.end());
ans.insert(ans.end(),b2.begin(),b2.end());
return ans;
}
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:38:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for (int i=0;i<route.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... |