# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1050452 | Zicrus | Keys (IOI21_keys) | C++17 | 3091 ms | 28540 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;
typedef long long ll;
ll n;
vector<vector<pair<ll, ll>>> edges;
vector<int> r;
vector<ll> lnk;
vector<ll> sz;
ll find(ll a) {
if (a != lnk[a]) lnk[a] = find(lnk[a]);
return lnk[a];
}
bool unite(ll a, ll b) {
a = find(a); b = find(b);
if (a == b) return false;
if (sz[a] > sz[b]) swap(a, b);
lnk[a] = b;
sz[b] += sz[a];
return true;
}
ll solve(ll start) {
lnk = vector<ll>(n);
sz = vector<ll>(n, 1);
for (int i = 0; i < n; i++) lnk[i] = i;
vector<vector<ll>> adj(n);
vector<bool> vst(n);
vector<bool> hasKey(n);
queue<ll> q;
q.push(start);
while (!q.empty()) {
ll cur = q.front(); q.pop();
if (vst[cur]) continue;
vst[cur] = true;
if (!hasKey[r[cur]]) {
for (auto &e : edges[r[cur]]) {
if (!unite(e.first, e.second)) continue;
if (vst[e.first] || vst[e.second]) {
q.push(e.first);
q.push(e.second);
}
adj[e.first].push_back(e.second);
adj[e.second].push_back(e.first);
}
}
for (auto &e : adj[cur]) {
q.push(e);
}
}
return sz[find(start)];
}
vector<int> find_reachable(vector<int> r1, vector<int> u, vector<int> v, vector<int> c) {
r = r1;
n = r.size();
edges = vector<vector<pair<ll, ll>>>(n);
for (int i = 0; i < c.size(); i++) {
edges[c[i]].push_back({u[i], v[i]});
}
vector<int> res(n);
int mn = 1ll << 30ll;
for (int i = 0; i < n; i++) {
res[i] = solve(i);
mn = min(mn, res[i]);
}
for (auto &e : res) {
e = e == mn;
}
return res;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |