이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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);
queue<ll> q;
q.push(0);
while (!q.empty()) {
ll cur = q.front(); q.pop();
if (vst[cur]) continue;
vst[cur] = true;
if (sz[lnk[cur]] >= 2) return true;
for (auto &e : adj[cur]) {
q.push(e.second);
}
}
return false;
}
#ifdef TEST
#include "grader.cpp"
#endif
# | 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... |