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 <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); --i)
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
using namespace std;
using ll = long long;
template<class T, class U> bool chmin(T& a, const U& b) { return a > b ? a = b, true : false; }
template<class T, class U> bool chmax(T& a, const U& b) { return a < b ? a = b, true : false; }
struct edge {
int to, idx;
};
using Graph = vector<vector<edge>>;
class StronglyConnectedComponents {
Graph G, RG;
vector<bool> seen;
vector<int> ord;
vector<int> cmp;
int cnt;
void dfs1(int v) {
if (seen[v]) return;
seen[v] = true;
for (auto e : G[v]) dfs1(e.to);
ord.push_back(v);
}
void dfs2(int v) {
if (cmp[v] != -1) return;
cmp[v] = cnt;
for (auto e : RG[v]) dfs2(e.to);
}
public:
StronglyConnectedComponents(Graph G_, Graph RG_) : G(G_), RG(RG_) {
seen.assign(G.size(), false);
rep (i, G.size()) dfs1(i);
reverse(all(ord));
cmp.assign(G.size(), -1);
cnt = 0;
for (int i : ord) {
if (cmp[i] == -1) {
dfs2(i);
++cnt;
}
}
}
vector<vector<int>> get() const {
vector<vector<int>> res(cnt);
rep (i, G.size()) res[cmp[i]].push_back(i);
return res;
}
};
std::variant<bool, std::vector<int>> find_journey(int N, int M, std::vector<int> U, std::vector<int> V) {
Graph G(N), RG(N);
rep (i, M) {
G[U[i]].push_back({V[i], i});
RG[V[i]].push_back({U[i], i});
}
vector<int> cnt(N);
{
vector<bool> seen(N);
auto dfs = [&](auto&& self, int v) -> void {
if (seen[v]) return;
seen[v] = true;
if (cnt[v] < 2) {
++cnt[v];
for (auto e : G[v]) self(self, e.to);
}
seen[v] = false;
};
dfs(dfs, 0);
}
{
auto dfs = [&](auto&& self, int v) -> void {
for (auto e : G[v]) {
if (cnt[e.to] < 2) {
cnt[e.to] = 2;
self(self, e.to);
}
}
};
rep (i, N) {
if (cnt[i] >= 2) dfs(dfs, i);
}
}
auto g = StronglyConnectedComponents(G, RG).get();
rep (i, g.size()) {
if (g[i].size() <= 1) continue;
for (int v : g[i]) {
if (cnt[v] >= 2) return true;
}
}
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... |