Submission #894390

#TimeUsernameProblemLanguageResultExecution timeMemory
894390Trisanu_DasKeys (IOI21_keys)C++17
100 / 100
579 ms60104 KiB
#include <bits/stdc++.h> using namespace std; struct dsu { int n; vector<int> p; dsu(int _n): n(_n), p(_n) { iota(begin(p), end(p), 0); } int leader(int x) { return p[x] == x ? x : p[x] = leader(p[x]); } }; vector<int> find_reachable(vector<int> r, vector<int> U, vector<int> V, vector<int> C) { const auto n = int(r.size()), m = int(U.size()); vector<vector<pair<int, int>>> e(n); for (int i = 0; i < m; ++i) { e[U[i]].emplace_back(V[i], C[i]); e[V[i]].emplace_back(U[i], C[i]); } dsu d(n); auto num = numeric_limits<size_t>::max(); vector<int> ans, solved(n); while (true) { vector nodes(n, vector<int>()); vector<int> vis(n), changed, appear(n), res; auto BFS = [&] (int S) { for (int x: res) appear[r[x]] = 0; for (int x: changed) nodes[x].clear(); res.clear(), changed.clear(); queue<int> Q; Q.push(S); while (!empty(Q)) { const auto u = Q.front(); Q.pop(); if (S != d.leader(u)) { vis[d.leader(u)] = 1; d.p[S] = d.leader(u); return; } if (vis[u]) continue; vis[u] = 1, res.push_back(u); if (!appear[r[u]]) { appear[r[u]] = 1; for (int v: nodes[r[u]]) Q.push(v); } for (auto [v, c]: e[u]) if (appear[c]) Q.push(v); else nodes[c].push_back(v), changed.push_back(c); } solved[S] = 1; if (size(res) < num) num = size(ans = res); else if (size(res) == num) for (int x: res) ans.push_back(x); }; bool hav = 0; for (int i = 0; i < n; ++i) if (d.leader(i) == i && !vis[i] && !solved[i]) hav = 1, BFS(i); if (!hav) break; } vector<int> ret(n); for (int x: ans) ret[x] = 1; return ret; }
#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...