Submission #927719

#TimeUsernameProblemLanguageResultExecution timeMemory
927719berrStranded Far From Home (BOI22_island)C++17
100 / 100
201 ms48244 KiB
//ᓚᘏᗢ #include <bits/stdc++.h> using namespace std; #define int long long const int mod = 998244353; struct dsu{ int n; vector<vector<int>> available; vector<int> sum, par, a; dsu(int _n, vector<int> x){ n = _n; sum = x; par.resize(n); a=x; available.resize(n); iota(par.begin(), par.end(), 0); for(int i=0; i<n; i++){ available[i].push_back(i); } } int find(int x){ if(par[x]==x) return x; return par[x] = find(par[x]); } int merge(int x, int y){ int tx=x; x = find(x); y=find(y); if(x==y) return 0; par[y] = x; sum[x] += sum[y]; if(sum[y] <a[tx]) return 1; if(available[y].size() > available[x].size()){ swap(available[x], available[y]); } while(available[y].size()){ available[x].push_back(available[y].back()); available[y].pop_back(); } return 1; } }; signed main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<int> a(n), id(n); for(auto &i: a) cin >> i; iota(id.begin(), id.end(), 0); sort(id.begin(), id.end(), [&](int i, int j){ return a[i]<a[j]; }); vector<vector<int>> g(n); dsu d(n, a); for(int i=0; i<m; i++){ int x, y; cin >> x >> y; x--; y--; g[x].push_back(y); g[y].push_back(x); } for(auto i: id){ for(auto v: g[i]){ if(a[v] <= a[i]){ d.merge(i, v); } } } vector<int> ar =d.available[d.find(id.back())]; vector<int> ans(n); for(auto i: ar) ans[i]=1; for(auto i: ans) cout<<i; }
#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...