Submission #776770

#TimeUsernameProblemLanguageResultExecution timeMemory
776770SanguineChameleonThousands Islands (IOI22_islands)C++17
35 / 100
99 ms20052 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; } } } for (int iter = 0; iter < 4; iter++) { nxt[start] = (iter & 1) ? nxt2 : nxt1; for (int i = 0; i < N; i++) { prv[i] = {-1, -1}; } int u = start; prv[start] = {start, -1}; while (true) { int v = nxt[u].first; if (prv[v].first != -1) { nxt[v] = {u, nxt[u].second}; vector<int> cycle = {nxt[u].second}; while (u != v) { cycle.push_back(prv[u].second); nxt[u] = prv[u]; u = prv[u].first; } vector<int> suf; while (u != start) { suf.push_back(prv[u].second); u = prv[u].first; } res.insert(res.end(), suf.rbegin(), suf.rend()); res.insert(res.end(), cycle.rbegin(), cycle.rend()); res.insert(res.end(), suf.begin(), suf.end()); break; } else { prv[v] = {u, nxt[u].second}; u = v; } } } 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...