# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
628549 | qwerasdfzxcl | 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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
set<pair<int, int>> adj[100100], INV[100100];
vector<int> deg0, ansf, ansb;
int n, s, on[100100];
void _erase(int x){
if (!on[x]) return;
on[x] = 0;
for (auto &[v, i]:INV[x]){
adj[v].erase(adj[v].find(make_pair(x, i)));
if (adj[v].empty()) deg0.push_back(v);
}
for (auto &[v, i]:adj[x]) INV[v].erase(INV[v].find(make_pair(x, i)));
}
std::variant<bool, std::vector<int>> find_journey(
int N, int M, std::vector<int> U, std::vector<int> V) {
n = N, s = 1;
fill(on+1, on+n+1, 1);
for (int i=0;i<M;i++){
++U[i], ++V[i];
adj[U[i]].emplace(V[i], i+1);
INV[V[i]].emplace(U[i], i+1);
}
for (int i=1;i<=n;i++) if (adj[i].empty()) deg0.push_back(i);
while(true){
while(!deg0.empty()){
int x = deg0.back(); deg0.pop_back();
_erase(x);
}
if (!on[s]) return 0;
if (adj[s].size()>1) break;
ansf.push_back(adj[s].begin()->second);
ansb.push_back(adj[s].begin()->second);
int nxt = adj[s].begin()->first;
_erase(s);
s = nxt;
}
return 1;
}