이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "islands.h"
#include <variant>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
variant<bool, vector<int>> find_journey(
int N, int M, vector<int> U, vector<int> V) {
if (N == 2)
{
vector<int>a;
vector<int>b;
for (int i = 0; i < M; i++)
if (U[i] == 0 && V[i] == 1)
a.push_back(i);
else
b.push_back(i);
if (a.size() >= 2 && b.size() >= 1)
return vector<int>({a[0], b[0], a[1], a[0], b[0], a[1]});
return false;
}
{
int x = -1;
int y = -1;
int z = -1;
int t = -1;
for (int i = 0; i < M; i++)
if (U[i] == 0 && V[i] == 1)
x = i;
else if (U[i] == 1 && V[i] == 0)
y = i;
else if (U[i] == 0 && V[i] == 2)
z = i;
else if (U[i] == 2 && V[i] == 0)
t = i;
if (min({x, y, z, t}) >= 0)
return vector<int>({x, y, z, t, y, x, t, z});
}
{
vector<pair<int, int>>adj[N];
for (int i = 0; i + 1 < M; i += 2)
if (V[i + 1] == U[i] && V[i] == U[i + 1])
{
adj[U[i]].push_back({V[i], i});
adj[V[i]].push_back({U[i], i + 1});
}
vector<bool> vis(N, false);
vector<int>c;
vector<int>xx;
function<void(int)>dfs = [&](int i)->void
{
if (vis[i])
return;
if (!c.empty())
return;
vis[i] = true;
vector<int>V;
for (pair<int, int> jj : adj[i])
{
int j = jj.first;
if (vis[j] == false)
V.push_back(jj.second);
}
if (V.size() >= 2)
{
vector<int>a = xx;
c = xx;
c.push_back(V[0]);
c.push_back(V[0] ^ 1);
c.push_back(V[1]);
c.push_back(V[1] ^ 1);
c.push_back(V[0] ^ 1);
c.push_back(V[0]);
c.push_back(V[1] ^ 1);
c.push_back(V[1]);
reverse(a.begin(), a.end());
for (int i : a)
c.push_back(i);
}
else
for (pair<int, int> jj : adj[i])
{
int j = jj.first;
if (vis[j] == false)
{
xx.push_back(jj.second);
dfs(j);
xx.pop_back();
if (!c.empty())
return;
}
}
};
dfs(0);
if (!c.empty())
return c;
}
{
vector<pair<int, int>>adj[N];
for (int i = 0; i + 1 < M; i += 2)
if (U[i + 1] == U[i] && V[i] == V[i + 1])
adj[U[i]].push_back({V[i], i});
vector<bool> vis(N, false);
vector<bool> st(N, false);
vector<int>c;
vector<int>xx;
vector<int> D(N);
function<void(int, int)>dfs = [&](int i, int d)->void
{
if (vis[i])
return;
if (!c.empty())
return;
D[i] = d;
vis[i] = true;
for (pair<int, int> jj : adj[i])
{
int j = jj.first;
if (st[j] == true)
{
xx.push_back(jj.second);
vector<int>a, b;
for (int i = 0; i < D[j]; i++)
a.push_back(xx[i]);
for (int i = D[j]; i < (int)xx.size(); i++)
b.push_back(xx[i]);
for (int i : a)
c.push_back(i);
for (int i : b)
c.push_back(i);
for (int i : b)
c.push_back(i ^ 1);
reverse(b.begin(), b.end());
reverse(a.begin(), a.end());
for (int i : b)
c.push_back(i);
for (int i : b)
c.push_back(i ^ 1);
for (int i : a)
c.push_back(i);
return;
}
}
for (pair<int, int> jj : adj[i])
{
int j = jj.first;
if (vis[j] == false)
{
xx.push_back(jj.second);
st[j] = true;
dfs(j, d + 1);
st[j] = false;
xx.pop_back();
if (!c.empty())
return;
}
}
};
st[0] = true;
dfs(0, 0);
if (!c.empty())
return c;
}
{
vector<pair<int, int>>adj[N];
for (int i = 0; i < M; i++)
adj[U[i]].push_back({V[i], i});
vector<bool> vis(N, false);
vector<bool> st(M, false);
vector<bool> stV(N, false);
vector<int> cid(N, -1);
vector<set<pair<int, int>, greater<pair<int, int>>>> aaa(N);
vector<int>c;
vector<int>xx;
vector<int> D(N);
vector<int> E(N);
function<void(int, int)>dfs = [&](int i, int d)->void
{
if (vis[i])
return;
if (!c.empty())
return;
D[i] = d;
vis[i] = true;
for (pair<int, int> jj : adj[i])
{
int j = jj.first;
if (vis[j] == false)
{
xx.push_back(jj.second);
st[jj.second] = true;
stV[jj.first] = true;
E[i] = jj.second;
dfs(j, d + 1);
for (pair<int, int> cc : aaa[j])
{
int c = cc.second;
if (U[c] != j)
{
if (cid[i] == -1)
cid[i] = c;
else if (D[U[cid[i]]] < D[U[c]])
cid[i] = c;
aaa[i].insert(cc);
}
}
st[jj.second] = false;
stV[jj.first] = false;
xx.pop_back();
if (!c.empty())
return;
}
else
{
if (stV[j])
{
int e = E[j];
if (cid[i] == -1)
cid[i] = e;
else if (D[U[cid[i]]] < D[j])
cid[i] = e;
aaa[i].insert({D[j], e});
}
if (cid[j] != -1)
{
if (!st[cid[j]])
{
c = {1, 2, 0};
return;
}
}
}
}
};
stV[0] = true;
dfs(0, 0);
stV[0] = false;
if (!c.empty())
return c;
}
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... |