Submission #1066761

#TimeUsernameProblemLanguageResultExecution timeMemory
1066761mispertionStranded Far From Home (BOI22_island)C++17
100 / 100
345 ms85240 KiB
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef long long ll; #define int ll typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define pb push_back #define all(x) x.begin(), x.end() #define sz(x) (int)x.size() #define mispertion ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define F first #define S second #define getlast(s) (*s.rbegin()) #define debg cout << "OK\n" const ld PI = 3.1415926535; const int N = 3e5 + 10; const int M = 1e5 + 10; int mod = 998244353; const int infi = INT_MAX; const ll infl = 1e16; const int P = 2; int mult(int a, int b){ return a * 1LL * b % mod; } int sum(int a, int b){ if(a + b >= mod) return a + b - mod; if(a + b < 0) return a + b + mod; return a + b; } int binpow(int a, int n){ if (n == 0) return 1; if (n % 2 == 1){ return mult(binpow(a, n - 1), a); } else{ auto b = binpow(a, n / 2); return mult(b, b); } } int n, m, s[N], zav[N], ans[N]; map<int, vector<int>> mp; vector<int> g[N]; struct dsu{ set<pii> nbr[N]; int p[N], sz[N], sm[N]; dsu(int n){ for(int i = 1; i <= n; i++){ p[i] = i; sz[i] = 1; sm[i] = s[i]; for(auto u : g[i]) if(s[u] > s[i]) nbr[i].insert({s[u], u}); } } int getp(int v){ if(p[v] == v) return v; return p[v] = getp(p[v]); } void uni(int a, int b){ a = getp(a); b = getp(b); if(a == b) return; if(sz[a] > sz[b]) swap(a, b); p[a] = b; sz[b] += sz[a]; sm[b] += sm[a]; if(sz(nbr[b]) > sz(nbr[a])){ while(sz(nbr[a]) > 0){ nbr[b].insert(*nbr[a].begin()); nbr[a].erase(nbr[a].begin()); } }else{ set<pii> tmp = nbr[b]; nbr[b].swap(nbr[a]); for(auto e : tmp) nbr[b].insert(e); } } }; void solve(){ cin >> n >> m; vector<pii> por = {}; for(int i = 1; i <= n; i++){ ans[i] = -1; cin >> s[i]; por.pb({s[i], i}); mp[s[i]].pb(i); } sort(all(por)); reverse(all(por)); for(int i = 1; i <= m; i++){ int u, v; cin >> u >> v; g[v].pb(u); g[u].pb(v); } dsu ds = dsu(n); for(auto e : mp){ int w = e.F; for(auto v : e.S){ for(auto u : g[v]){ if(s[u] <= s[v]) ds.uni(u, v); } } for(auto v : e.S){ int nv = ds.getp(v); while(sz(ds.nbr[nv]) > 0 && ds.nbr[nv].begin()->F <= w) ds.nbr[nv].erase(ds.nbr[nv].begin()); if(sz(ds.nbr[nv]) == 0){ ans[v] = 1; }else{ if(ds.nbr[nv].begin()->F <= ds.sm[nv]){ zav[v] = ds.nbr[nv].begin()->S; }else{ ans[v] = 0; } } } } for(auto e : por){ if(ans[e.S] == -1){ ans[e.S] = ans[zav[e.S]]; } } for(int i = 1; i <= n; i++) cout << ans[i]; cout << '\n'; } signed main(){ mispertion; int t = 1; //cin >> t; while (t--){ 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...