#include <bits/stdc++.h>
#include <variant>
using namespace std;
#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x << ' '; print(x); cerr << endl;
#else
#define dbg(x)
#endif
void print(int x) {cerr << x;}
void print(long long x) {cerr << x;}
void print(char x) {cerr << x;}
void print(string x) {cerr << x;}
void print(double x) {cerr << x;}
template <class T> void print(vector <T> x);
template <class T> void print(set <T> x);
template <class T> void print(multiset <T> x);
template <class T, class V> void print(pair <T, V> x);
template <class T, class V> void print(map <T, V> x);
template <class T> void print(vector <T> x) {cerr << "[ "; for(auto i : x) {print(i); cerr << ' ';} cerr << "]";}
template <class T> void print(set <T> x) {cerr << "[ "; for(auto i : x) {print(i); cerr << ' ';} cerr << "]";}
template <class T> void print(multiset <T> x) {cerr << "[ "; for(auto i : x) {print(i); cerr << ' ';} cerr << "]";}
template <class T, class V> void print(pair <T, V> x) {cerr << "{"; print(x.first); cerr << ' '; print(x.second); cerr << "}";}
template <class T, class V> void print(map <T, V> x) {cerr << "[ "; for(auto i : x) {print(i); cerr << ' ';} cerr << "]";}
#define ll long long
#define pb push_back
#define ppb pop_back
#define PII pair <int, int>
#define PLL pair <ll, ll>
#define all(v) (v).begin(), (v).end()
#define OK cerr << "OK\n";
#define MP make_pair
const int N0 = 1005, M0 = 2e5 + 5;
int n, m, kayak[N0][N0], revkayak[M0];
vector <int> u, v;
vector <int> G[N0];
vector <int> color, cycle, road;
int par[N0];
void dfs(int node, int parent){
par[node] = parent;
color[node] = 1;
for(int u : G[node]){
if(u == parent) continue;
if(color[u] == 0){
dfs(u, node);
if(!cycle.empty()) return;
continue;
}
if(color[u] == 1){
int curr = node;
while(curr != par[u]){
cycle.pb(curr);
curr = par[curr];
}
while(curr != -1){
road.pb(curr);
curr = par[curr];
}
return;
}
}
}
variant <bool, vector <int>> find_journey(int N, int M, vector<int> U, vector<int> V){
if(N == 2) return false;
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
kayak[i][j] = -1;
}
}
n = N, m = M, u = U, v = V;
for(int i = 0; i < M; i++){
G[u[i]].pb(v[i]);
kayak[u[i]][v[i]] = i;
}
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
if(kayak[i][j] == -1) continue;
revkayak[kayak[i][j]] = kayak[j][i];
}
}
color.assign(N, 0);
dfs(0, -1);
if(cycle.empty()) return false;
vector <int> res;
reverse(all(road));
for(int i = 0; i < int(road.size() - 1); i++){
res.pb(kayak[road[i]][road[i + 1]]);
}
reverse(all(cycle));
for(int i = 1; i < cycle.size(); i++){
res.pb(kayak[cycle[i - 1]][cycle[i]]);
}
res.pb(kayak[cycle.back()][cycle.front()]);
res.pb(kayak[cycle.front()][cycle.back()]);
for(int i = cycle.size() - 1; i >= 1; i--){
res.pb(kayak[cycle[i]][cycle[i - 1]]);
}
int sz = res.size();
for(int i = sz - cycle.size(); i < sz; i++){
res.pb(revkayak[res[i]]);
}
for(int i = road.size(); i < road.size() + cycle.size(); i++){
res.pb(revkayak[res[i]]);
}
for(int i = int(road.size() - 1); i >= 1; i--){
res.pb(kayak[road[i - 1]][road[i]]);
}
return res;
}
# | 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... |