Submission #776826

#TimeUsernameProblemLanguageResultExecution timeMemory
776826SanguineChameleonThousands Islands (IOI22_islands)C++17
57.10 / 100
104 ms19956 KiB
#include "islands.h" #include <bits/stdc++.h> using namespace std; const int maxN = 1e5 + 20; vector<pair<int, int>> adj1[maxN]; vector<pair<int, int>> adj2[maxN]; pair<int, int> nxt[maxN]; pair<int, int> prv[maxN]; int deg[maxN]; bool rem[maxN]; variant<bool, vector<int>> find_journey(int N, int M, vector<int> U, vector<int> V) { for (int i = 0; i < M; i++) { adj1[U[i]].emplace_back(V[i], i); adj2[V[i]].emplace_back(U[i], i); deg[U[i]]++; } deque<int> Q; for (int i = 0; i < N; i++) { if (deg[i] == 0) { rem[i] = true; Q.push_back(i); } } vector<int> res; int start = 0; pair<int, int> nxt1, nxt2; while (true) { while (!Q.empty()) { int u = Q.front(); Q.pop_front(); for (auto e: adj2[u]) { int v = e.first; if (rem[v]) { continue; } deg[v]--; if (deg[v] == 0) { rem[v] = true; Q.push_back(v); } } } if (rem[start]) { return false; } nxt1 = {-1, -1}; nxt2 = {-1, -1}; for (auto e: adj1[start]) { if (!rem[e.first]) { if (nxt1.first == -1) { nxt1 = e; } else if (nxt2.first == -1) { nxt2 = e; } } } if (nxt1.first == -1) { return false; } if (nxt2.first == -1) { rem[start] = true; Q.push_back(start); start = nxt1.first; res.push_back(nxt1.second); } else { break; } } for (int i = 0; i < N; i++) { if (rem[i] || i == start) { continue; } for (auto e: adj1[i]) { if (!rem[e.first]) { nxt[i] = e; break; } } } bool has_start = false; bool done = false; for (int iter = -1; iter < 4; iter++) { for (int i = 0; i < N; i++) { prv[i] = {-1, -1}; } int u = -1; int extra = -1; if (iter == 1 && has_start) { extra = nxt2.second; u = nxt2.first; prv[nxt2.first] = {nxt2.first, -1}; } else { if (iter != 2 || !has_start) { nxt[start] = (iter == -1 ? nxt2 : ((iter & 1) ? nxt2 : nxt1)); } u = start; prv[start] = {start, -1}; } while (true) { int v = nxt[u].first; if (prv[v].first != -1) { if (iter == -1) { if (v == start) { swap(nxt1, nxt2); } break; } nxt[v] = {u, nxt[u].second}; vector<int> cycle = {nxt[u].second}; while (u != v) { if (iter == 1) { done |= (u == start); } cycle.push_back(prv[u].second); nxt[u] = prv[u]; u = prv[u].first; } if (iter == 1) { done |= (u == start); } vector<int> suf; while (u != ((iter == 1 && has_start) ? nxt2.first : start)) { suf.push_back(prv[u].second); u = prv[u].first; } if (iter == 1 && has_start) { suf.push_back(extra); } res.insert(res.end(), suf.rbegin(), suf.rend()); res.insert(res.end(), cycle.rbegin(), cycle.rend()); res.insert(res.end(), suf.begin(), suf.end()); if (iter == 0) { has_start = (v == start); } break; } else { prv[v] = {u, nxt[u].second}; u = v; } } if (iter == 1 && has_start && done) { break; } } return res; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...