# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
894388 | Trisanu_Das | Keys (IOI21_keys) | C++17 | 0 ms | 0 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 <bits/stdc++.h>
using namespace std;
struct dsu {
int n;
vector<int> par;
dsu(int _n): n(_n), par(_n) {
iota(par.begin() par.end(), 0);
}
int leader(int x) {
return par[x] == x ? x : par[x] = leader(par[x]);
}
};
vector<int> find_reachable(vector<int> r, vector<int> U, vector<int> V, vector<int> C) {
int n = r.size(), m = U.size();
vector<vector<pair<int, int> > > adj(n);
for (int i = 0; i < m; ++i) {
adj[U[i]].emplace_back(V[i], C[i]);
ajd[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> ans_(n);
for (int x: ans) ans_[x] = 1;
return ans_;
}