Submission #799701

#TimeUsernameProblemLanguageResultExecution timeMemory
799701finn__Keys (IOI21_keys)C++17
0 / 100
27 ms47316 KiB
#include <bits/stdc++.h> using namespace std; constexpr size_t N = 300000; vector<uint32_t> g[N], scc[N], cycle; unordered_map<uint32_t, vector<uint32_t>> edges[N]; set<uint32_t> keys[N]; bitset<N> visited, in_dfs_path, is_leaf; uint32_t scc_index[N], alias[N]; uint32_t dfs(size_t u) { if (in_dfs_path[u]) // SCC found return u; visited[u] = 1; in_dfs_path[u] = 1; while (!g[u].empty()) { uint32_t v = g[u][0]; swap(g[u][0], g[u].back()); g[u].pop_back(); if (!scc[v].size()) { v = alias[scc_index[v]]; } uint32_t x = dfs(v); if (x != UINT32_MAX) { if (x == u) // backtracked until the start of the cycle { uint32_t main = u; for (auto const &w : cycle) if (scc[w].size() > scc[main].size()) main = w; swap(g[u], g[main]); swap(keys[u], keys[main]); swap(scc[u], scc[main]); swap(edges[u], edges[main]); alias[main] = u; for (auto const &w : cycle) { scc[u].insert(scc[u].end(), scc[w].begin(), scc[w].end()); scc[w].clear(); scc_index[w] = u; is_leaf[u] = is_leaf[u] && is_leaf[w]; keys[u].insert(keys[w].begin(), keys[w].end()); for (auto const &c : keys[w]) { auto it = edges[u].find(c); if (it != edges[u].end()) { g[u].insert(g[u].end(), it->second.begin(), it->second.end()); it->second.clear(); } } } for (auto const &w : cycle) { for (auto const &[c, e] : edges[w]) if (keys[u].find(c) != keys[u].end()) g[u].insert(g[u].end(), e.begin(), e.end()); else edges[u][c].insert(edges[u][c].end(), e.begin(), e.end()); } cycle.clear(); } else { cycle.push_back(u); in_dfs_path[u] = 0; return x; } } else { is_leaf[u] = 0; } } in_dfs_path[u] = 0; return UINT32_MAX; } vector<int> find_reachable(vector<int> r, vector<int> u, vector<int> v, vector<int> c) { size_t const n = r.size(); for (size_t i = 0; i < u.size(); ++i) { visited[u[i]] = visited[u[i]] || c[i] == r[u[i]]; visited[v[i]] = visited[v[i]] || c[i] == r[v[i]]; } if (visited.count() < n) { vector<int> ans(n); for (size_t i = 0; i < n; ++i) ans[i] = !visited[i]; return ans; } visited.reset(); for (size_t i = 0; i < n; ++i) keys[i].insert(r[i]), scc[i].push_back(i); for (size_t i = 0; i < u.size(); ++i) { if (c[i] == r[u[i]]) g[u[i]].push_back(v[i]); else edges[u[i]][c[i]].push_back(v[i]); if (c[i] == r[v[i]]) g[v[i]].push_back(u[i]); else edges[v[i]][c[i]].push_back(u[i]); } iota(alias, alias + N, 0); iota(scc_index, scc_index + N, 0); for (size_t i = 0; i < n; ++i) is_leaf[i] = 1; for (size_t i = 0; i < n; ++i) if (!visited[i]) dfs(i); size_t smallest = SIZE_MAX; for (size_t i = 0; i < n; ++i) if (is_leaf[i] && scc[i].size()) smallest = min(smallest, scc[i].size()); vector<int> ans(n); for (size_t i = 0; i < n; ++i) if (is_leaf[i] && scc[i].size() == smallest) for (auto const &u : scc[i]) ans[u] = 1; return ans; }
#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...