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;
}
vector<int> endTrick;
bool dfs3(int cur, int root, bool first = true) {
if (cur == root && !first) return true;
for (auto &e : adj[cur]) {
if (lnk[e.second] != lnk[cur]) continue;
if (vst[e.first]) continue;
vst[e.first] = true;
if (dfs3(e.second, root, false)) {
endTrick.push_back(e.first);
return true;
}
}
return false;
}
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;
vector<int> revPath;
vector<int> dist(n, 1 << 30);
q.push(0);
dist[0] = 0;
ll comp = 0;
while (!q.empty()) {
ll cur = q.front(); q.pop();
if (vst[cur]) continue;
vst[cur] = true;
if (sz[lnk[cur]] >= 2) {
comp = cur;
while (cur != 0) {
for (auto &e : revAdj[cur]) {
if (dist[e.second] < dist[cur]) {
revPath.push_back(e.first);
cur = e.second;
break;
}
}
}
//
vst = vector<bool>(m);
dfs3(comp, comp);
reverse(endTrick.begin(), endTrick.end());
vector<int> res;
for (int i = revPath.size()-1; i >= 0; i--) {
res.push_back(revPath[i]);
}
for (int i = 0; i < endTrick.size(); i++) {
res.push_back(endTrick[i]);
}
for (int i = 0; i < revPath.size(); i++) {
res.push_back(revPath[i]);
}
for (int i = 0; i < revPath.size(); i++) {
res.push_back(revPath[i]^1);
}
for (int i = endTrick.size()-1; i >= 0; i--) {
res.push_back(endTrick[i]);
}
for (int i = 0; i < revPath.size(); i++) {
res.push_back(revPath[i]^1);
}
return res;
//
break;
}
for (auto &e : adj[cur]) {
if (vst[e.second]) continue;
dist[e.second] = min(dist[e.second], dist[cur]+1);
q.push(e.second);
}
}
return false;
}
#ifdef TEST
#include "grader.cpp"
#endif
Compilation message (stderr)
islands.cpp: In function 'std::variant<bool, std::vector<int, std::allocator<int> > > find_journey(int, int, std::vector<int>, std::vector<int>)':
islands.cpp:132:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
132 | for (int i = 0; i < endTrick.size(); i++) {
| ~~^~~~~~~~~~~~~~~~~
islands.cpp:135:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
135 | for (int i = 0; i < revPath.size(); i++) {
| ~~^~~~~~~~~~~~~~~~
islands.cpp:139:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
139 | for (int i = 0; i < revPath.size(); i++) {
| ~~^~~~~~~~~~~~~~~~
islands.cpp:145:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
145 | for (int i = 0; i < revPath.size(); i++) {
| ~~^~~~~~~~~~~~~~~~
# | 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... |