# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
630932 | garam1732 | Thousands Islands (IOI22_islands) | C++17 | 0 ms | 0 KiB |
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 <iostream>
#include <variant>
#include <vector>
#include <algorithm>
#include <map>
#define ff first
#define ss second
using namespace std;
typedef pair<int, int> pi;
map<pi, int> m;
vector<pi> adj[100100];
vector<pi> v;
vector<int> ans;
bool visited[100100], chk[100100];
int x = -1;
void dfs(int here) {
//cout << here << endl;
for(pi there : adj[here]) {
if(visited[there.ff]) {
v.push_back(there);
x = there.ff;
return;
}
if(chk[there.ff]) return;
visited[there.ff] = true;
chk[there.ff] = true;
v.push_back(there);
dfs(there.ff);
if(x != -1) return;
visited[there.ff] = false;
v.pop_back();
}
}
vector<int> em;
vector<int> find_journey(
//std::variant<bool, std::vector<int>> find_journey(
int N, int M, std::vector<int> U, std::vector<int> V) {
for(int i = 0; i < M; i+=2) {
if(m.find(pi(U[i], V[i])) == m.end()) {
adj[U[i]].push_back(pi(V[i], i));
}
//adj[V[i]].push_back(pi(U[i], i));
m[pi(U[i], V[i])] = 1;
}
visited[0] = true;
dfs(0);
if(x == -1) return em;
int i;
for(i = 0; i < v.size(); i++) if(v[i].ff == x) break;
if(i == (int)v.size()-1) i = -1;
for(int j = 0; j <= i; j++) ans.push_back(v[j].ss);
for(int j = i+1; j < v.size(); j++) ans.push_back(v[j].ss);
for(int j = i+1; j < v.size(); j++) ans.push_back(v[j].ss^1);
for(int j = (int)v.size()-1; j > i; j--) ans.push_back(v[j].ss);
for(int j = (int)v.size()-1; j > i; j--) ans.push_back(v[j].ss^1);
for(int j = i; j >= 0; j--) ans.push_back(v[j].ss);
return ans;
// std::vector<int> v, w;
// for(int i = 0; i < M; i++) {
// if(U[i] == 0) v.push_back(i);
// else w.push_back(i);
// }
//
// if(v.size() < 2 || w.size() < 1) {
// return em;
// //return false;
// }
// std::vector<int> ans;
// ans.push_back(v[0]);
// ans.push_back(w[0]);
// ans.push_back(v[1]);
// ans.push_back(v[0]);
// ans.push_back(w[0]);
// ans.push_back(v[1]);
// return ans;
}