Submission #572681

#TimeUsernameProblemLanguageResultExecution timeMemory
572681jlallas384Stranded Far From Home (BOI22_island)C++17
100 / 100
286 ms48272 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; struct ufds{ vector<int> par; ufds(int n): par(n){ iota(par.begin(), par.end(), 0); } int find(int a){ return a == par[a] ? a : par[a] = find(par[a]); } int unite(int a,int b){ a = find(a), b = find(b); if(a == b) return 0; par[b] = a; return 1; } }; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n,m; cin >> n >> m; vector<int> s(n); for(int &x: s){ cin >> x; } vector<vector<int>> g(n); while(m--){ int u,v; cin >> u >> v,u--,v--; g[u].push_back(v); g[v].push_back(u); } vector<int> ok(n); vector<pair<int,int>> srt; for(int i = 0; i < n; i++){ srt.emplace_back(s[i],i); } sort(srt.begin(), srt.end()); vector<vector<int>> tree(n); ufds ds(n); for(auto [x,v]: srt){ for(int u: g[v]) if(ok[u]){ if(ok[u] && ds.find(u) != ds.find(v)){ tree[v].push_back(ds.find(u)); ds.unite(v,u); } } ok[v] = 1; } vector<ll> sums; for(auto x: s){ sums.push_back(x); } vector<int> ans(n); function <void(int)> dfs = [&](int v){ ans[v] = 1; for(int u: tree[v]){ dfs(u); ans[u] &= s[v] <= sums[u]; sums[v] += sums[u]; } }; function <void(int)> dfs1 = [&](int v){ for(int u: tree[v]){ ans[u] &= ans[v]; dfs1(u); } }; dfs(ds.find(0)); dfs1(ds.find(0)); for(int x: ans){ cout << x; } }
#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...