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;
variant<bool, vector<int>> find_journey(int N,int M,vector<int> U,vector<int> V){
vector<vector<pair<int,int>>> adj(N);
for(int i=0;i<M;i+=2) {
adj[U[i]].emplace_back(V[i],i);
}
vector<int> visited(N);
stack<int> path;
function<bool(int)> dfs = [&](int x) {
if(visited[x]==2)return false;
if(visited[x]==1){
return true;
}
visited[x]=1;
for(auto&[v,idx]:adj[x]) {
path.emplace(idx);
if(dfs(v))return true;
path.pop();
}
visited[x]=2;
return false;
};
if(!dfs(0))return false;
vector<int> path_cycle,path_direct;
int ver = V[path.top()];
bool phase = false;
while(!path.empty()) {
int curr = path.top();path.pop();
if(!phase)path_cycle.emplace_back(curr);
else path_direct.emplace_back(curr);
if(U[curr]==ver)phase=true;
}
reverse(path_cycle.begin(),path_cycle.end());
reverse(path_direct.begin(),path_direct.end());
vector<int> ans = path_direct;
for(int&i:path_cycle)ans.emplace_back(i);
for(int&i:path_cycle)ans.emplace_back(i+1);
reverse(path_cycle.begin(), path_cycle.end());
for(int&i:path_cycle)ans.emplace_back(i);
for(int&i:path_cycle)ans.emplace_back(i+1);
reverse(path_direct.begin(),path_direct.end());
for(int&i:path_direct)ans.emplace_back(i);
return ans;
}
# | 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... |