# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1074192 | Zicrus | Thousands Islands (IOI22_islands) | C++17 | 34 ms | 8652 KiB |
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>
#include "islands.h"
using namespace std;
typedef long long ll;
int n, m;
vector<vector<pair<int, int>>> adj, revAdj; // edgeId, node
vector<bool> vst, edgeUsed;
stack<int> stk;
vector<int> lnk, sz, acc;
vector<int> numPaths;
void dfs1(int cur) {
vst[cur] = true;
for (auto &e : adj[cur]) {
if (vst[e.second]) continue;
dfs1(e.second);
}
stk.push(cur);
}
int dfs2(int cur, int rep) {
vst[cur] = true;
lnk[cur] = rep;
sz[rep]++;
int last = cur;
for (auto &e : revAdj[cur]) {
if (vst[e.second]) continue;
edgeUsed[e.first] = true;
last = dfs2(e.second, rep);
}
if (cur == rep && cur != last) {
for (auto &e : adj[cur]) {
if (e.second == last) {
edgeUsed[e.first] = true;
break;
}
}
}
return last;
}
int dfsPaths(int cur) {
vst[cur] = true;
int res = 0;
for (auto &e : revAdj[cur]) {
if (edgeUsed[e.first]) continue;
if (vst[e.second]) {
res += numPaths[e.second];
}
else {
res += dfsPaths(e.second);
}
}
return numPaths[cur] = res;
}
variant<bool, vector<int>> find_journey(int N, int M, vector<int> U, vector<int> V) {
n = N; m = M;
lnk = vector<int>(n);
sz = vector<int>(n);
acc = vector<int>(n);
adj = vector<vector<pair<int, int>>>(n);
revAdj = vector<vector<pair<int, int>>>(n);
for (int i = 0; i < n; i++) lnk[i] = i;
for (int i = 0; i < m; i++) {
adj[U[i]].push_back({i, V[i]});
revAdj[V[i]].push_back({i, U[i]});
}
edgeUsed = vector<bool>(m);
vst = vector<bool>(n);
for (int i = 0; i < n; i++) {
if (vst[i]) continue;
dfs1(i);
}
vst = vector<bool>(n);
while (!stk.empty()) {
ll cur = stk.top(); stk.pop();
if (vst[cur]) continue;
dfs2(cur, cur);
}
vst = vector<bool>(n);
numPaths = vector<int>(n);
vst[0] = true;
numPaths[0] = 1;
for (int i = 0; i < n; i++) {
if (vst[i]) continue;
dfsPaths(i);
}
vector<int> compPaths(n);
vector<int> rep(n);
int a = -1, b = -1;
for (int i = 0; i < n; i++) {
if (sz[lnk[i]] <= 1) continue;
compPaths[lnk[i]] += numPaths[i];
if (compPaths[lnk[i]] >= 2) {
a = rep[lnk[i]];
b = i;
break;
}
rep[lnk[i]] = i;
}
return a != -1;
}
#ifdef TEST
#include "grader.cpp"
#endif
Compilation message (stderr)
# | 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... |