Submission #723113

#TimeUsernameProblemLanguageResultExecution timeMemory
723113dxz05Stranded Far From Home (BOI22_island)C++17
100 / 100
516 ms38992 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define all(x) (x).begin(), (x).end() #define MP make_pair const int N = 200500; mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); int n; int s[N]; bool can[N]; struct dsu{ vector<int> p; vector<long long> sum; vector<set<int>> children; dsu(){ p.resize(n); iota(all(p), 0); sum.resize(n); for (int i = 0; i < n; i++) sum[i] = s[i]; children.resize(n); for (int i = 0; i < n; i++){ children[i].insert(i); } } int leader(int x){ return (x == p[x] ? x : p[x] = leader(p[x])); } bool unite(int x, int y){ int px = leader(x); int py = leader(y); if (px == py) return false; if (sum[px] < s[y]){ for (int i : children[px]) can[i] = false; children[px].clear(); } if (sum[py] < s[x]){ for (int i : children[py]) can[i] = false; children[py].clear(); } x = px, y = py; if (children[x].size() > children[y].size()) swap(x, y); for (int i : children[x]) children[y].insert(i); children[x].clear(); sum[y] += sum[x]; p[x] = y; return true; } }; vector<int> g[N]; void solve(){ int m; cin >> n >> m; for (int i = 0; i < n; i++){ cin >> s[i]; } vector<pair<int, int>> edges; while (m--){ int u, v; cin >> u >> v; --u, --v; g[u].push_back(v); g[v].push_back(u); if (s[u] > s[v]) swap(u, v); edges.emplace_back(u, v); } sort(all(edges), [](auto x, auto y){ return s[x.second] < s[y.second]; }); fill(can, can + n, true); dsu d; for (auto [u, v] : edges){ d.unite(u, v); } for (int i = 0; i < n; i++){ cout << can[i]; } } int main() { ios_base::sync_with_stdio(false); #ifdef LOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif solve(); 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...