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>
#include <variant>
#include <vector>
using namespace std;
int n, m, can = -1;
vector<int> u, v, vis;
vector<pair<int, int>> adj[1001];
void dfs(int node, vector<int> &ans)
{
vis[node] = 1;
if(node == 0)
{
if(adj[node].size() >= 2)
{
can = 1;
vector<int> cur(4);
for(int i=0;i<2;i++)
{
int bNum = adj[node][i].second;
cur[2 * i] = bNum;
cur[2 * i + 1] = (bNum % 2 == 1 ? bNum - 1:bNum + 1);
ans.push_back(cur[2 * i]);
ans.push_back(cur[2 * i + 1]);
}
ans.push_back(cur[1]);
ans.push_back(cur[0]);
ans.push_back(cur[3]);
ans.push_back(cur[2]);
return;
}
}
else
{
if(adj[node].size() >= 3)
{
can = 1;
int i = 0, lboat1 = ans.back(), lboat2 = (lboat1 % 2 == 1 ? lboat1 - 1:lboat1 + 1), sz = ans.size();;
vector<int> cur(4);
for(auto k:adj[node])
{
if(i == 2)
break;
if(k.second == lboat2)
continue;
int bNum = k.second;
cur[2 * i] = bNum;
cur[2 * i + 1] = (bNum % 2 == 1 ? bNum - 1:bNum + 1);
ans.push_back(cur[2 * i]);
ans.push_back(cur[2 * i + 1]);
i++;
}
ans.push_back(cur[1]);
ans.push_back(cur[0]);
ans.push_back(cur[3]);
ans.push_back(cur[2]);
for(int i=sz-1;i>=0;i--)
ans.push_back(ans[i]);
return;
}
}
for(auto a:adj[node])
{
if(vis[a.first])
continue;
ans.push_back(a.second);
dfs(a.first, ans);
if(can == 1)
return;
ans.pop_back();
}
}
std::variant<bool, std::vector<int>> find_journey(int N, int M, std::vector<int> U, std::vector<int> V) {
n = N, m = M;
u = U, v = V;
vector<int> ans;
if(n <= 2)
{
if(m <= 2)
return false;
int z = 0;
vector<int> cur(3);
for(int i=0;i<m;i++)
{
if(u[i] == 0)
{
if(z < 2)
cur[z] = i;
z++;
}
else
cur[2] = i;
}
if(z < 2 || z == m)
return false;
ans.resize(3);
ans[0] = cur[0];
ans[1] = cur[2];
ans[2] = cur[1];
for(int i=0;i<3;i++)
ans.push_back(ans[i]);
return ans;
}
vis.resize(n, 0);
for(int i=0;i<m;i+=2)
{
adj[u[i]].push_back({v[i], i});
adj[v[i]].push_back({u[i], i + 1});
}
dfs(0, ans);
if(can)
return ans;
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... |