Submission #654478

#TimeUsernameProblemLanguageResultExecution timeMemory
654478prvocisloStranded Far From Home (BOI22_island)C++17
100 / 100
288 ms30632 KiB
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <string> #include <vector> typedef long long ll; typedef long double ld; using namespace std; const int maxn = 2e5 + 5; struct comp { ll sum = 0; priority_queue<pair<int, int> > s; }; vector<comp> c(maxn); vector<int> a(maxn), p(maxn); int root(int u) { return u == p[u] ? u : p[u] = root(p[u]); } int merge(int u, int v) { u = root(u), v = root(v); if (u == v) return u; if (c[u].s.size() < c[v].s.size()) swap(u, v); p[v] = u; c[u].sum += c[v].sum; while (!c[v].s.empty()) c[u].s.push(c[v].s.top()), c[v].s.pop(); return u; } bool cmp(int i, int j) { return a[i] < a[j]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<pair<int, int> > w; for (int i = 0; i < n; i++) { cin >> a[i]; w.push_back({ a[i], i }); c[i].sum = a[i]; p[i] = i; } vector<vector<int> > g(n); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--, v--; if (make_pair( a[u], u ) > make_pair( a[v], v )) swap(u, v); c[u].s.push({-a[v], -v}); g[v].push_back(u); } vector<int> de(n, -1); sort(w.begin(), w.end()); for (pair<int, int> p : w) { int u = p.second, cm = u; for (int v : g[u]) cm = merge(cm, v); while (!c[cm].s.empty() && -c[cm].s.top().second == u) c[cm].s.pop(); if (!c[cm].s.empty() && a[-c[cm].s.top().second] <= c[cm].sum) de[u] = -c[cm].s.top().second; } reverse(w.begin(), w.end()); vector<bool> ans(n, false); for (pair<int, int> p : w) { int u = p.second; if (p == w[0]) ans[u] = true; else if (de[u] != -1) ans[u] = ans[de[u]]; } for (int i = 0; i < n; i++) cout << "01"[ans[i]]; cout << "\n"; return 0; }
#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...