# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
753854 | jakobrs | Stranded Far From Home (BOI22_island) | 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 <iostream>
#include <vector>
#include <queue>
using i64 = int64_t;
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
i64 n, m;
std::cin >> n >> m;
std::vector<i64> a(n);
for (i64 i = 0; i < n; i++)
std::cin >> a[i];
std::vector<std::vector<i64>> adj(n);
for (i64 i = 0; i < m; i++) {
i64 u, v;
std::cin >> u >> v;
u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
std::string result;
result.reserve(n);
std::vector<i64> visited(n);
for (i64 i = 0; i < n; i++) {
struct Node {
i64 size;
i64 idx;
bool operator<(const Node &rhs) const {
return std::tie(rhs.size, idx) < std::tie(size, rhs.idx);
}
};
std::priority_queue<Node> q;
std::fill(visited.begin(), visited.end(), false);
visited[i] = true;
i64 mass = a[i];
q.push(Node { .size = 0, .idx = i });
while (!q.empty()) {
Node node = q.top();
if (node.size > mass) break;
mass += node.size;
q.pop();
for (i64 j : adj[i]) {
if (!visited[j]) {
visited[j] = true;
q.push(Node { .size = a[j], .idx = j });
}
}
}
result.push_back(q.empty() ? '1' : '0');
}
std::cout << result;
}