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>
using namespace std;
const int MAX = 1e5 + 5;
typedef pair<int, int> ii;
int N, mark[MAX];
vector<vector<vector<int>>> g;
vector<ii> ng[MAX];
vector<int> path;
bool dfs(int u) {
if (mark[u] == 2)
return false;
if (mark[u] == 1) {
path.push_back(u);
return true;
}
mark[u] = 1;
for (auto &[v, id]: ng[u])
if (dfs(v)) {
path.push_back(u);
return true;
}
mark[u] = 2;
return false;
}
bool dfs1(int u) {
if ((!u && ng[u].size() >= 2) || ng[u].size() >= 3) {
path.push_back(u);
return true;
}
if (mark[u])
return false;
mark[u] = true;
for (auto &[v, id]: ng[u])
if (dfs1(v)) {
path.push_back(u);
return true;
}
return false;
}
variant<bool, vector<int>> find_journey(int _N, int M, vector<int> U, vector<int> V) {
N = _N;
if (N == 2) {
vector<int> g[2];
for (int i = 0; i < M; i++)
g[U[i]].push_back(i);
if (g[0].size() < 2 || g[1].empty())
return false;
return vector<int>({g[0][0], g[1][0], g[0][1], g[0][0], g[1][0], g[0][1]});
}
g.resize(N, vector<vector<int>>(N));
for (int i = 0; i < M; i++)
g[U[i]][V[i]].push_back(i);
bool isClique = true;
for (int i = 0; i < N && isClique; i++)
for (int j = 0; j < N && isClique; j++)
if (i != j && g[i][j].empty())
isClique = false;
if (isClique)
return vector<int>({g[0][1][0], g[1][2][0], g[2][0][0], g[0][2][0], g[2][1][0], g[1][0][0],
g[2][0][0], g[1][2][0], g[0][1][0], g[1][0][0], g[2][1][0], g[0][2][0]});
bool isDoubled = M % 2 == 0;
for (int i = 1; i < M && isDoubled; i += 2)
if (U[i - 1] != U[i] || V[i - 1] != V[i])
isDoubled = false;
if (isDoubled) {
for (int i = 1; i < M; i += 2)
ng[U[i]].emplace_back(V[i], i);
if (dfs(0)) {
reverse(path.begin(), path.end());
int pivot = -1;
for (int i = 0; i < path.size(); i++)
if (path[i] == path.back()) {
pivot = i;
break;
}
vector<int> rs;
for (int i = 0; i < pivot; i++)
rs.push_back(g[path[i]][path[i + 1]][0]);
for (int i = pivot; i < path.size() - 1; i++)
rs.push_back(g[path[i]][path[i + 1]][0]);
for (int i = pivot; i < path.size() - 1; i++)
rs.push_back(g[path[i]][path[i + 1]][1]);
for (int i = path.size() - 2; i >= pivot; i--)
rs.push_back(g[path[i]][path[i + 1]][0]);
for (int i = path.size() - 2; i >= pivot; i--)
rs.push_back(g[path[i]][path[i + 1]][1]);
for (int i = pivot - 1; i >= 0; i--)
rs.push_back(g[path[i]][path[i + 1]][0]);
return rs;
}
return false;
}
for (int i = 0; i < M; i++)
ng[U[i]].emplace_back(V[i], i);
if (dfs1(0)) {
vector<int> rs;
for (int i = 0; i < path.size() - 1; i++)
rs.push_back(g[path[i]][path[i + 1]][0]);
int p = path.size() == 1 ? -1 : path[path.size() - 2];
vector<int> sample;
for (auto &[v, id]: ng[path.back()])
if (v != p) {
sample.push_back(id);
if (sample.size() == 2)
break;
}
for (int id: sample) {
rs.push_back(id);
rs.push_back(id % 2 ? id - 1 : id + 1);
}
for (int id: sample) {
rs.push_back(id % 2 ? id - 1 : id + 1);
rs.push_back(id);
}
for (int i = (int) path.size() - 2; i >= 0; i--)
rs.push_back(g[path[i]][path[i + 1]][0]);
return rs;
}
return false;
}
//int main() {
// freopen("../_input", "r", stdin);
// int N, M;
// assert(2 == scanf("%d %d", &N, &M));
//
// vector<int> U(M), V(M);
// for (int i = 0; i < M; ++i) {
// assert(2 == scanf("%d %d", &U[i], &V[i]));
// }
//
// variant<bool, vector<int>> result = find_journey(N, M, U, V);
// if (result.index() == 0) {
// printf("0\n");
// if (get<bool>(result)) {
// printf("1\n");
// } else {
// printf("0\n");
// }
// } else {
// printf("1\n");
// vector<int> &canoes = get<vector<int>>(result);
// printf("%d\n", static_cast<int>(canoes.size()));
// for (int i = 0; i < static_cast<int>(canoes.size()); ++i) {
// if (i > 0) {
// printf(" ");
// }
// printf("%d", canoes[i]);
// }
// printf("\n");
// }
// return 0;
//}
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:79:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for (int i = 0; i < path.size(); i++)
| ~~^~~~~~~~~~~~~
islands.cpp:87:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for (int i = pivot; i < path.size() - 1; i++)
| ~~^~~~~~~~~~~~~~~~~
islands.cpp:89:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for (int i = pivot; i < path.size() - 1; i++)
| ~~^~~~~~~~~~~~~~~~~
islands.cpp:105:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | for (int i = 0; i < path.size() - 1; 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... |