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>
using namespace std;
#define MAX 201010
int deg[MAX];
vector<int> adj[MAX];
vector<int> radj[MAX];
int chk[MAX];
int vis[MAX];
vector<int> U, V;
int N, M;
variant<bool, vector<int>> find_journey(int N, int M, vector<int> U, vector<int> V) {
int i;
::N = N;
::M = M;
::U = U;
::V = V;
for (i = 0; i < M; i++) deg[U[i]]++, adj[U[i]].push_back(i), radj[V[i]].push_back(U[i]);
queue<int> q;
for (i = 0; i < N; i++) if (!deg[i]) q.push(i);
int s = 0;
vector<int> st;
while (1) {
while (q.size()) {
int t = q.front();
chk[t] = 1;
q.pop();
for (auto v : radj[t]) if (!chk[v]) {
deg[v]--;
if (deg[v] == 0) q.push(v);
}
}
if (deg[s] <= 1) {
if (deg[s] == 0) return false;
for (auto n : adj[s]) {
if (!chk[V[n]]) {
for (auto v : radj[s]) if (!chk[v]) {
deg[v]--;
if (deg[v] == 0) q.push(v);
}
chk[s] = 1;
s = V[n];
st.push_back(n);
break;
}
}
if (deg[s] == 0) return false;
continue;
}
else break;
}
vector<int> ans = st;
int a, b;
a = b = -1;
for (auto n : adj[s]) if (!chk[V[n]]) b = a, a = n;
vector<int> p1, p2, cyc1, cyc2;
int v;
p1.push_back(a);
v = V[a];
vis[v] = 1;
while (1) {
int nn, x;
for (auto n : adj[v]) {
x = V[n];
if (chk[x]) continue;
if (x == s) continue;
nn = n;
break;
}
if (vis[x]) {
while (p1.size() && V[p1.back()] != x) cyc1.push_back(p1.back()), p1.pop_back();
reverse(cyc1.begin(), cyc1.end());
cyc1.push_back(nn);
break;
}
p1.push_back(nn);
vis[x] = 1;
v = x;
}
memset(vis, 0, sizeof(vis));
p2.push_back(b);
v = V[b];
vis[v] = 1;
while (1) {
int nn, x;
for (auto n : adj[v]) {
x = V[n];
if (chk[x]) continue;
if (x == s) continue;
nn = n;
break;
}
if (vis[x]) {
while (p2.size() && V[p2.back()] != x) cyc2.push_back(p2.back()), p2.pop_back();
reverse(cyc2.begin(), cyc2.end());
cyc2.push_back(nn);
break;
}
p2.push_back(nn);
vis[x] = 1;
v = x;
}
memset(vis, 0, sizeof(vis));
for (auto v : cyc1) vis[v] = 1;
int chk = 0;
for (auto v : cyc2) if (vis[v]) chk = 1;
for (auto v : p1) ans.push_back(v);
for (auto v : cyc1) ans.push_back(v);
reverse(p1.begin(), p1.end());
for (auto v : p1) ans.push_back(v);
for (auto v : p2) ans.push_back(v);
if (chk) reverse(cyc2.begin(), cyc2.end());
for (auto v : cyc2) ans.push_back(v);
if (chk) reverse(cyc2.begin(), cyc2.end());
reverse(p2.begin(), p2.end());
for (auto v : p2) ans.push_back(v);
for (auto v : p1) ans.push_back(v);
if (!chk) reverse(cyc1.begin(), cyc1.end());
for (auto v : cyc1) ans.push_back(v);
if (!chk) reverse(cyc1.begin(), cyc1.end());
reverse(p1.begin(), p1.end());
for (auto v : p1) ans.push_back(v);
for (auto v : p2) ans.push_back(v);
for (auto v : cyc2) ans.push_back(v);
reverse(p2.begin(), p2.end());
for (auto v : p2) ans.push_back(v);
return ans;
}
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:101:21: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
101 | while (p2.size() && V[p2.back()] != x) cyc2.push_back(p2.back()), p2.pop_back();
| ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
islands.cpp:76:21: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
76 | while (p1.size() && V[p1.back()] != x) cyc1.push_back(p1.back()), p1.pop_back();
| ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
# | 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... |