# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
746774 | math_rabbit_1028 | Thousands Islands (IOI22_islands) | C++17 | 281 ms | 24996 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;
bool ans = false;
int n, m;
vector< pair<int, int> > adj[101010], rev[101010];
int out[101010], in[101010], remain[202020];
set< pair<int, int> > outset;
int ch[202020];
void DFS(int v) {
int cnt = 0;
for (int i = 0; i < adj[v].size(); i++) {
if (remain[adj[v][i].second] == 0) continue;
if (ch[adj[v][i].first] == 0) {
cnt++;
}
}
if (cnt >= 2) {
ans = true;
return;
}
for (int i = 0; i < adj[v].size(); i++) {
if (remain[adj[v][i].second] == 0) continue;
if (ch[adj[v][i].first] == 0) {
ch[adj[v][i].first] = 1;
DFS(adj[v][i].first);
}
}
}
std::variant<bool, std::vector<int>> find_journey(
int N, int M, std::vector<int> U, std::vector<int> V) {
n = N; m = M;
for (int i = 0; i < m; i++) {
adj[U[i]].push_back({V[i], i});
rev[V[i]].push_back({U[i], i});
out[U[i]]++;
in[V[i]]++;
remain[i] = 1;
}
for (int i = 0; i < n; i++) outset.insert({out[i], i});
while (!outset.empty() && outset.begin()->first == 0) {
int v = outset.begin()->second;
outset.erase(outset.begin());
for (int i = 0; i < rev[v].size(); i++) {
if (remain[rev[v][i].second] == 0) continue;
remain[rev[v][i].second] = 0;
outset.erase(outset.find(make_pair(out[rev[v][i].first], rev[v][i].first)));
out[rev[v][i].first]--;
outset.insert(make_pair(out[rev[v][i].first], rev[v][i].first));
}
}
if (out[0] <= 1) {
for (int i = 0; i < rev[0].size(); i++) {
if (remain[rev[0][i].second] == 0) continue;
remain[rev[0][i].second] = 0;
outset.erase(outset.find(make_pair(out[rev[0][i].first], rev[0][i].first)));
out[rev[0][i].first]--;
outset.insert(make_pair(out[rev[0][i].first], rev[0][i].first));
}
}
while (!outset.empty() && outset.begin()->first == 0) {
int v = outset.begin()->second;
outset.erase(outset.begin());
for (int i = 0; i < rev[v].size(); i++) {
if (remain[rev[v][i].second] == 0) continue;
remain[rev[v][i].second] = 0;
outset.erase(outset.find(make_pair(out[rev[v][i].first], rev[v][i].first)));
out[rev[v][i].first]--;
outset.insert(make_pair(out[rev[v][i].first], rev[v][i].first));
}
}
//for (int i = 0; i < m; i++) cout << remain[i] << "\n";
//cout << "\n";
ch[0] = 1;
DFS(0);
return ans;
}
Compilation message (stderr)
# | 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... |