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>
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
using i32 = int;
#define int long long
#define len(x) (int)(x.size())
template<typename T>
using vec = vector<T>;
vector<i32> find_reachable(vector<i32> r, vector<i32> u1, vector<i32> v1, vector<i32> c) {
int n = len(r);
vec<vec<pair<int, int>>> g(n);
for (int i = 0; i < len(u1); i++) {
g[u1[i]].emplace_back(v1[i], c[i]);
g[v1[i]].emplace_back(u1[i], c[i]);
}
vec<int> key_set;
vec<vec<int>> destinations(n);
vec<bool> keys(n, false);
auto clean = [&]() {
for (auto x: key_set) {
keys[x] = false;
destinations[x].clear();
}
key_set.clear();
};
vec<i32> ans(n, -1);
i32 cur_min = 1e9;
auto solve = [&](int start) -> int {
clean();
queue<int> q;
auto add_key = [&](int x)->int {
if (keys[x]) return 0;
keys[x] = true;
key_set.push_back(x);
for (auto t: destinations[x]) {
if (ans[t] > cur_min) {
return -1;
}
q.push(t);
}
return 0;
};
vec<bool> ok(n, false);
q.push(start);
int cnt = 0;
add_key(r[start]);
while (!q.empty()) {
auto v = q.front();
q.pop();
if (ok[v]) continue;
ok[v] = true;
cnt++;
if(add_key(r[v]) == -1) {
return 1e9;
}
for (auto [u, tc]: g[v]) {
if (ok[u]) continue;
if (keys[tc]) {
if (ans[u] > cur_min) {
return 1e9;
}
q.push(u);
} else {
destinations[tc].push_back(u);
key_set.push_back(tc);
}
}
if(cnt > cur_min) {
return 1e9;
}
}
return cnt;
};
vec<int> perm(n);
iota(perm.begin(), perm.end(), 0);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
shuffle(perm.begin(), perm.end(), rng);
for (auto i : perm){
ans[i] = solve(i);
cur_min = min(cur_min, ans[i]);
}
int min_val = *min_element(ans.begin(), ans.end());
for (int i = 0; i < n; i++) {
ans[i] = ans[i] == min_val;
}
return ans;
}
Compilation message (stderr)
keys.cpp:5: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
5 | #pragma GCC optimization("unroll-loops")
|
# | 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... |