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<vector<int>> adj_rev;
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;
return;
}
if (loop == -1) {
path.pop_back();
} else {
return;
}
}
}
vector<bool> used1,used2;
vector<int> order;
void dfs1(int v) {
used1[v] = true;
for (auto u : adj[v])
if (!used1[u.first])
dfs1(u.first);
order.push_back(v);
}
bool dfs2(int v) {
used2[v] = true;
for (auto u : adj_rev[v])
if (used1[u] and !used2[u])
return true;
return false;
}
variant<bool,vector<int>> find_journey( int n, int m, vector<int> u, vector<int> v) {
adj.resize(n); used.resize(n); used1.resize(n); used2.resize(n); adj_rev.resize(n);
for (int i=0;i<m;i+=2) {
adj[u[i]].push_back({v[i],i});
adj_rev[v[i]].push_back(u[i]);
}
bool lp = false;
dfs1(0);
reverse(order.begin(), order.end());
for (auto u: order) {
if (dfs2(u)) {
lp = true;
cout << u << " ";
break;
}
}
if (!lp) {
return false;
}
dfs(0);
vector<int> route,cycle;
bool inroute = true;
for (int i=0;i<path.size();i++) {
if (u[path[i]] == loop) {
inroute = false;
}
if (inroute) {
route.push_back(path[i]);
} else {
cycle.push_back(path[i]);
}
}
int lenc = cycle.size();
vector<int> p1=route,b1=route,c1=cycle,d1=cycle,c2(lenc),d2;
reverse(b1.begin(),b1.end()); reverse(d1.begin(),d1.end());
for (int i=0;i<lenc;i++) {
c2[i] = c1[i]^1;
}
d2 = c2;
reverse(d2.begin(),d2.end());
vector<int> ans;
ans.insert(ans.end(),p1.begin(),p1.end());
ans.insert(ans.end(),c1.begin(),c1.end());
ans.insert(ans.end(),c2.begin(),c2.end());
ans.insert(ans.end(),d1.begin(),d1.end());
ans.insert(ans.end(),d2.begin(),d2.end());
ans.insert(ans.end(),b1.begin(),b1.end());
/*for (auto u: p1) cout << u << " "; cout << "\n";
for (auto u: c1) cout << u << " "; cout << "\n";
for (auto u: c2) cout << u << " "; cout << "\n";
for (auto u: d1) cout << u << " "; cout << "\n";
for (auto u: d2) cout << u << " "; cout << "\n";
for (auto u: b1) cout << u << " "; cout << "\n";*/
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:70:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for (int i=0;i<path.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... |