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 ll = int;
std::variant<bool, std::vector<int>> find_journey(
int n, int m, std::vector<int> u, std::vector<int> v){
if (n == 2){
vector<ll> a,b;
for(ll i = 0;i<m;i++){
if (u[i]==0) a.push_back(i);
else b.push_back(i);
}
if (a.size()<2||b.size()<1) return false;
return vector<ll>({a[0],b[0],a[1],a[0],b[0],a[1]});
}
if(m==n*(n-1)){
vector<ll> a(6);
for(ll i = 0;i<m;i++){
if (u[i]==0&&v[i]==1) a[0] = i;
if (u[i]==1&&v[i]==2) a[1] = i;
if (u[i]==2&&v[i]==0) a[2] = i;
if (u[i]==0&&v[i]==2) a[3] = i;
if (u[i]==2&&v[i]==1) a[4] = i;
if (u[i]==1&&v[i]==0) a[5] = i;
}
return vector<ll>({a[0],a[1],a[2],a[3],a[4],a[5],a[2],a[1],a[0],a[5],a[4],a[3]});
}
if(m%2==0){
vector<vector<ll>> con(n);
for(ll i = 0;i<m;i++){
con[u[i]].push_back(i);
}
vector<ll> inv(m);
for(ll i = 0;i<m;i++) inv[i] = i^1;
vector<ll> ans;
if(u[0]!=u[1]){
vector<ll> st;
ll cur = 0;
vector<ll> uu(m,0);
while(con[cur].size()-(cur!=0)==1){
for(ll j : con[cur]) if(!uu[j]){
uu[j] = 1;
uu[inv[j]] = 1;
cur = v[j];
st.push_back(j);
break;
}
}
vector<ll> oks;
for(ll j : con[cur]) if(!uu[j]&&oks.size()<2){
oks.push_back(j);
}
if(oks.size()<2) return false;
for(ll i : st) ans.push_back(i);
ans.push_back(oks[0]);
ans.push_back(inv[oks[0]]);
ans.push_back(oks[1]);
ans.push_back(inv[oks[1]]);
ans.push_back(inv[oks[0]]);
ans.push_back(oks[0]);
ans.push_back(inv[oks[1]]);
ans.push_back(oks[1]);
reverse(st.begin(),st.end());
for(ll i : st) ans.push_back(i);
}else{
//map<pair<ll,ll>,ll> mp;
//for(ll i = 0;i<m;i++){
// mp[make_pair(u[i],v[i])] = i;
//}
//for(ll i = 0;i<m;i++){
// inv[i] = mp[make_pair(v[i],u[i])];
//}
deque<ll> c(n,0),st,ins(m,0),uu(n,0);
ll fr = 0;
function<bool(ll,ll)> dfs;
dfs = [&](ll i, ll p){
if (uu[i]) return false;
c[i] = 1;
for(ll j : con[i]) if(inv[j]!=p){
st.push_back(j);
ins[j] = 1;
if (c[v[j]]){
fr = v[j];
return true;
}
if(dfs(v[j],j)) return true;
ins[j] = 0;
st.pop_back();
}
c[i] = 0;
uu[i] = 1;
return false;
};
bool ok = dfs(0,-1);
if(!ok) return false;
vector<ll> beg;
while(u[st.front()]!=fr){
beg.push_back(st.front());
st.pop_front();
}
for(ll i : beg) ans.push_back(i);
for(ll i : st) ans.push_back(i);
for(ll i : st) ans.push_back(inv[i]);
reverse(st.begin(),st.end());
for(ll i : st) ans.push_back(i);
for(ll i : st) ans.push_back(inv[i]);
reverse(beg.begin(),beg.end());
for(ll i : beg) ans.push_back(i);
}
return ans;
}
if (n == 4){
return std::vector<int>({0, 1, 2, 4, 0, 3, 2, 1, 4, 3});
}
return false;
}
# | 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... |