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 <bits/stdc++.h>
using namespace std;
int reverse_edge(int ind){
if(ind % 2 == 0) return ind+1;
else return ind-1;
}
std::variant<bool, std::vector<int>> find_journey(int n, int m, vector<int> u, vector<int> v){
vector<pair<int,int>> adj[n];
for(int i=0; i<m; i+=2){
adj[u[i]].push_back(make_pair(v[i],i));
adj[v[i]].push_back(make_pair(u[i],i+1));
}
//find cycle
vector<int> prev(n,-1), st(1,0);
vector<bool> vis(n, false);
bool found = false;
while(!st.empty()){
int v = st[st.size()-1];
st.pop_back();
vis[v] = true;
for(pair<int,int> p : adj[v]){
int u = p.first;
if(u==0 && prev[v]/2 != p.second/2){
found = true;
prev[0] = p.second;
break;
}
if(vis[u]) continue;
prev[u] = p.second;
st.push_back(u);
}
if(found) break;
}
if(!found) return false;
vector<int> cyc;
int x = 0;
do{
cyc.push_back(prev[x]);
x = u[prev[x]];
}while(x != 0);
reverse(cyc.begin(), cyc.end());
//now print the journey
vector<int> c;
for(int e : cyc){
c.push_back(e);
}
for(int e : cyc){
c.push_back(reverse_edge(e));
}
reverse(cyc.begin(), cyc.end());
for(int e : cyc){
c.push_back(e);
}
for(int e : cyc){
c.push_back(reverse_edge(e));
}
return c;
}
# | 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... |