#include <bits/stdc++.h>
#include "islands.h"
using namespace std;
#define pb push_back
const int MAXN=1000+5;
bool vis[MAXN],vis2[MAXN];
vector<int> adj[MAXN][MAXN];
vector<int> path;
bool dfs(int node,int par){
if(vis[node])return 0;
vis[node]=1;
for(int j=0;j<MAXN;j++){
if(j==par)continue;
if(adj[node][j].size()>1 && adj[j][node].size()>0){
path.pb(adj[node][j][0]);
path.pb(adj[j][node][0]);
path.pb(adj[node][j][1]);
path.pb(adj[node][j][0]);
path.pb(adj[j][node][0]);
path.pb(adj[node][j][1]);
return 1;
}
}
for(int j=0;j<MAXN;j++){
if(j==par)continue;
if(adj[node][j].size()>0){
path.pb(adj[node][j][0]);
bool flag=dfs(j,node);
if(flag){
path.pb(adj[node][j][0]);
return 1;
}
else path.pop_back();
}
}
return 0;
}
vector<int> cycle;
int dfs2(int node,int par){
vis2[node]=1;
for(int j=0;j<MAXN;j++){
if(j==par || adj[node][j].size()==0)continue;
if(vis2[j]){
cycle.pb(node);
return j;
}
int x=dfs2(j,node);
if(x==-2){
path.pb(adj[node][j][0]);
return -2;
}
if(x!=-1){
cycle.pb(node);
if(x==node)return -2;
return x;
}
}
vis2[node]=0;
return -1;
}
variant<bool, vector<int>> find_journey(int N, int M, vector<int> U, vector<int> V) {
for(int i=0;i<M;i++){
if(adj[U[i]][V[i]].size()<2)adj[U[i]][V[i]].pb(i);
}
bool flag=dfs(0,-1);
if(!flag){
for(int i=1;i<=N;i++)vis[i]=0;
dfs2(0,-1);
if(cycle.empty())return false;
else{
reverse(path.begin(),path.end());
reverse(cycle.begin(),cycle.end());
/* for(auto i:path)cout<<i<<' ';
cout<<'s'<<endl;
/* for(auto i:cycle)cout<<i<<' ';
cout<<endl<<endl;*/
vector<int> final=path,rev[2];
for(int t=0;t<2;t++) {
for (int id = 0; id < cycle.size() ; id++) {
int i = cycle[id];
int i2 = cycle[(id + 1)%cycle.size()];
rev[t].pb(adj[i][i2][t]);
final.pb(adj[i][i2][t]);
}
}
reverse(rev[0].begin(),rev[0].end());
reverse(rev[1].begin(),rev[1].end());
for(int t=0;t<2;t++){
for(auto i:rev[t])final.pb(i);
}
for(int i=path.size()-1;i>=0;i--)final.pb(path[i]);
return final;
}
}
return path;
}
# | 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... |