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;
using i32 = int;
using i64 = long long;
template <typename T>
using V = vector<T>;
template <typename T>
using VV = V<V<T>>;
template <typename T>
using VVV = V<VV<T>>;
template <typename T>
bool chmin(T &x, const T &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <typename T>
bool chmax(T &x, const T &y) {
if (x < y) {
x = y;
return true;
}
return false;
}
#define OVERRIDE4(a, b, c, d, ...) d
#define REP2(i, n) for (i32 i = 0; i < (i32)(n); ++i)
#define REP3(i, l, r) for (i32 i = (i32)(l); i < (i32)(r); ++i)
#define REP(...) OVERRIDE4(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define PER2(i, n) for (i32 i = (i32)(n)-1; i >= 0; --i)
#define PER3(i, l, r) for (i32 i = (i32)(r)-1; i >= (i32)(l); --i)
#define PER(...) OVERRIDE4(__VA_ARGS__, PER3, PER2)(__VA_ARGS__)
#define ALL(x) begin(x), end(x)
#define LEN(x) (i32) size(x)
void dbg(i32 x) { cerr << x; }
void dbg(i64 x) { cerr << x; }
template <typename T, typename U>
void dbg(pair<T, U> p) {
cerr << '(';
dbg(p.first);
cerr << ", ";
dbg(p.second);
cerr << ')';
}
template <typename T>
void dbg(V<T> arr) {
cerr << '[';
REP(i, LEN(arr)) {
if (i) {
cerr << ", ";
}
dbg(arr[i]);
}
cerr << ']';
}
void debug() { cerr << '\n'; }
template <typename Head, typename... Tail>
void debug(Head head, Tail... tail) {
dbg(head);
cerr << ", ";
debug(tail...);
}
#ifdef DEBUGF
#define DBG(...) \
do { \
cerr << #__VA_ARGS__ << " : "; \
debug(__VA_ARGS__); \
} while (false)
#else
#define DBG(...) (void)0
#endif
#include <variant>
#include "islands.h"
VV<i32> scc(const VV<pair<i32, i32>> &g) {
i32 n = LEN(g);
VV<i32> ret;
V<i32> alive(n, 1), in(n, -1), ord(n, -1), stc;
auto dfs = [&](auto dfs, i32 v, i32 &t) -> void {
in[v] = ord[v] = t++;
stc.push_back(v);
for (auto [u, e] : g[v]) {
if (!alive[u]) {
continue;
}
if (in[u] == -1) {
dfs(dfs, u, t);
chmin(ord[v], ord[u]);
} else {
chmin(ord[v], in[u]);
}
}
if (in[v] == ord[v]) {
V<i32> comp;
while (true) {
comp.push_back(stc.back());
stc.pop_back();
if (comp.back() == v) {
break;
}
}
ret.push_back(comp);
}
alive[v] = 0;
};
i32 t = 0;
REP(i, n) {
if (in[i] == -1) {
dfs(dfs, i, t);
}
}
return ret;
}
variant<bool, V<i32>> find_journey(i32 n, i32 m, V<i32> u, V<i32> v) {
VV<pair<i32, i32>> g(n);
REP(i, m) {
g[u[i]].emplace_back(v[i], i);
}
if (n == 2) {
bool ok = (LEN(g[0]) >= 2 && LEN(g[1]) >= 1);
if (!ok) {
return false;
}
i32 e01a = g[0][0].second;
i32 e01b = g[0][1].second;
i32 e10 = g[1][0].second;
V<i32> walk({e01a, e10, e01b, e01a, e10, e01b});
return walk;
}
map<pair<i32, i32>, i32> edgeid;
REP(i, m) {
edgeid[pair<i32, i32>(u[i], v[i])] = i;
}
{
V<i32> revc;
REP(i, 1, n) {
if (edgeid.count(pair<i32, i32>(0, i)) && edgeid.count(pair<i32, i32>(i, 0))) {
revc.push_back(i);
}
}
if (LEN(revc) >= 2) {
i32 u = revc[0];
i32 v = revc[1];
i32 e01 = edgeid[pair<i32, i32>(0, u)];
i32 e10 = edgeid[pair<i32, i32>(u, 0)];
i32 e02 = edgeid[pair<i32, i32>(0, v)];
i32 e20 = edgeid[pair<i32, i32>(v, 0)];
V<i32> walk({e01, e10, e02, e20, e10, e01, e20, e02});
return walk;
}
}
VV<i32> comps = scc(g);
DBG(comps);
V<i32> ccn(n);
REP(i, LEN(comps)) {
for (i32 v : comps[i]) {
ccn[v] = i;
}
}
V<i32> is_ok(n, 0), reach_cycle(n, 0);
for (const V<i32> &comp : comps) {
i32 reach_edge = 0;
for (i32 v : comp) {
for (auto [u, e] : g[v]) {
if (reach_cycle[u]) {
++reach_edge;
}
}
}
if (LEN(comp) >= 2 || reach_edge) {
for (i32 v : comp) {
reach_cycle[v] = 1;
}
}
if (reach_edge >= 2) {
for (i32 v : comp) {
is_ok[v] = 1;
}
continue;
}
bool outer = false;
for (i32 v : comp) {
for (auto [u, e] : g[v]) {
if (is_ok[u]) {
outer = true;
}
}
}
if (outer) {
for (i32 v : comp) {
is_ok[v] = 1;
}
continue;
}
bool deg3 = false;
for (i32 v : comp) {
i32 cnt = 0;
for (auto [u, e] : g[v]) {
if (ccn[u] == ccn[v]) {
++cnt;
}
}
if (cnt >= 3) {
deg3 = true;
}
if (cnt >= 2) {
is_ok[v] = 1;
}
}
if (deg3) {
for (i32 v : comp) {
is_ok[v] = 1;
}
}
}
return (bool)is_ok[0];
}
# | 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... |