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;
variant<bool, vector<int>> find_journey(int N, int M, vector<int> U, vector<int> V) {
int s = 0;
vector<bool> e(N, true);
vector<int>ans0;
{
vector<int> C(N, 0);
vector<int>b[N];
vector<int>ff[N];
for (int i = 0; i < M; i++)
{
C[U[i]]++;
b[V[i]].push_back(U[i]);
ff[U[i]].push_back(i);
}
stack<int>Q;
for (int i = 0; i < N; i++)
if (C[i] == 0)
Q.push(i);
while (e[s])
{
if (C[s] == 1)
{
int s_ = -1;
for (int ee : ff[s])
{
if (e[V[ee]])
{
ans0.push_back(ee);
s_ = V[ee];
}
}
for (int t : b[s])
{
if (e[t])
{
C[t]--;
if (C[t] == 0)
Q.push(t);
}
}
e[s] = false;
s = s_;
continue;
}
if (Q.empty())
break;
int a = Q.top();
Q.pop();
if (!e[a])
continue;
e[a] = false;
for (int t : b[a])
{
if (e[t])
{
C[t]--;
if (C[t] == 0)
Q.push(t);
}
}
}
if (!e[s])
return false;
}
vector<int>adj[N];
for (int i = 0; i < M; i++)
if (e[U[i]] && e[V[i]])
adj[U[i]].push_back(i);
auto c = [&](int s)->pair<vector<int>, vector<int>>
{
vector<int> D(N, -1);
D[s] = 0;
int t = V[adj[s][0]];
vector<int>v = {adj[s][0]};
int val = 0;
while (D[t] == -1)
{
val++;
D[t] = val;
v.push_back(adj[t][0]);
t = V[adj[t][0]];
}
vector<int>a, b;
for (int i = 0; i < D[t]; i++)
a.push_back(v[i]);
for (int i = D[t]; i < (int)v.size(); i++)
b.push_back(v[i]);
return {a, b};
};
pair<vector<int>, vector<int>>c0 = c(s);
adj[s][0] = adj[s].back();
pair<vector<int>, vector<int>>c1 = c(s);
vector<int> ret;
for (int i : ans0)
ret.push_back(i);
bool disjoint = true;
vector<int> cnt(M, 0);
for (auto x : {c0.first, c0.second, c1.first, c1.second})
for (int i : x)
{
cnt[i]++;
if (cnt[i] == 2)
disjoint = false;
}
set<int>S1, S2;
for (int i : c0.second)
S1.insert(i);
for (int i : c1.second)
S2.insert(i);
bool gal1 = S1 == S2; if (disjoint) {
for (int i : c0.first)
ret.push_back(i);
for (int i : c0.second)
ret.push_back(i);
reverse(c0.first.begin(), c0.first.end());
for (int i : c0.first)
ret.push_back(i);
for (int i : c1.first)
ret.push_back(i);
for (int i : c1.second)
ret.push_back(i);
reverse(c1.first.begin(), c1.first.end());
for (int i : c1.first)
ret.push_back(i);
reverse(c0.second.begin(), c0.second.end());
reverse(c1.second.begin(), c1.second.end());
for (int i : c0.first)
ret.push_back(i);
for (int i : c0.second)
ret.push_back(i);
reverse(c0.first.begin(), c0.first.end());
for (int i : c0.first)
ret.push_back(i);
for (int i : c1.first)
ret.push_back(i);
for (int i : c1.second)
ret.push_back(i);
reverse(c1.first.begin(), c1.first.end());
for (int i : c1.first)
ret.push_back(i);
}
else if (gal1)
{
for (int i : c0.first)
ret.push_back(i);
for (int i : c0.second)
ret.push_back(i);
reverse(c0.first.begin(), c0.first.end());
for (int i : c0.first)
ret.push_back(i);
for (int i : c1.first)
ret.push_back(i);
for (int i : c1.second)
ret.push_back(i);
reverse(c1.first.begin(), c1.first.end());
for (int i : c1.first)
ret.push_back(i);
}
else
{
vector<int>a = c0.second;
vector<int>b = c1.second;
vector<int>c;
while (a.size() > 0 && a.back() == b.back())
{
c.push_back(a.back());
a.pop_back();
b.pop_back();
}
reverse(c.begin(), c.end());
for (int i : a)
ret.push_back(i);
for (int i : c)
ret.push_back(i);
for (int i : b)
ret.push_back(i);
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
reverse(c.begin(), c.end());
for (int i : a)
ret.push_back(i);
for (int i : c)
ret.push_back(i);
for (int i : b)
ret.push_back(i);
}
reverse(ans0.begin(), ans0.end());
for (int i : ans0)
ret.push_back(i);
return ret;
}
# | 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... |