Submission #655583

#TimeUsernameProblemLanguageResultExecution timeMemory
655583uroskSwapping Cities (APIO20_swap)C++14
7 / 100
386 ms68100 KiB
#define here cerr<<"===========================================\n" #define dbg(x) cerr<<#x<<": "<<x<<endl; #include <bits/stdc++.h> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> #define ld double #define ll int #define llinf 100000000000000000LL // 10^17 #define pb push_back #define popb pop_back #define fi first #define sc second #define endl '\n' #define pll pair<ll,ll> #define pld pair<ld,ld> #define sz(a) (ll)(a.size()) #define all(a) a.begin(),a.end() #define ceri(a,l,r) {cerr<<#a<<": ";for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;} #define cer(a) {cerr<<#a<<": ";for(ll x_ : a) cerr<<x_<< " ";cerr<<endl;} #define daj_mi_malo_vremena ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0); using namespace std; #define maxn 500005 #define lg 21 ll n,m; vector<ll> g[maxn]; struct gr{ ll x,y,w; }; bool cmp(gr a,gr b){return a.w<b.w;} vector<gr> edge; ll c[maxn]; bool ok[maxn]; ll dsu[maxn]; ll deg[maxn]; ll in[maxn]; ll out[maxn]; ll it; ll root(ll x){ while(x!=dsu[x]){ dsu[x] = dsu[dsu[x]]; x = dsu[x]; } return x; } ll st[maxn][lg]; ll oke[maxn][lg]; vector<pll> v; void upd(ll x,ll y,ll w){ deg[x]++; deg[y]++; bool f = (deg[x]>=3)||(deg[y]>=3); x = root(x); y = root(y); ++it; if(x==y){ g[x].pb(it); g[it].pb(x); v.pb({it,x}); ok[it] = 1; dsu[x] = it; }else{ g[x].pb(it); g[it].pb(x); g[y].pb(it); g[it].pb(y); v.pb({it,x}); v.pb({it,y}); ok[it] = ok[x]|ok[y]|f; dsu[x] = dsu[y] = it; } c[it] = w; } ll itr = 0; void dfs(ll u,ll p){ st[u][0] = p; oke[u][0] = ok[p]; in[u] = ++itr; for(ll s : g[u]){ if(s==p) continue; dfs(s,u); } out[u] = itr; } bool intree(ll x,ll y){ return in[y]<=in[x]&&out[x]<=out[y]; } ll lca(ll x,ll y){ if(intree(y,x)) return x; if(intree(x,y)) return y; for(ll j = lg-1;j>=0;j--){ if(!intree(x,st[y][j])) y = st[y][j]; } return st[y][0]; } void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) { n = N; m = M; it = n; iota(dsu+1,dsu+maxn,1); for(ll i = 0;i<m;i++){ ll x = U[i]; ll y = V[i]; ll w = W[i]; ++x; ++y; edge.pb({x,y,w}); } sort(all(edge),cmp); for(gr e : edge){ ll x = e.x; ll y = e.y; ll w = e.w; upd(x,y,w); } dfs(it,it); for(ll j = 1;j<lg;j++){ for(ll i = 1;i<=it;i++) st[i][j] = st[st[i][j-1]][j-1]; } for(ll j = 1;j<lg;j++){ for(ll i = 1;i<=it;i++) oke[i][j] = oke[i][j-1]|oke[st[st[i][j-1]][0]][j-1]; } } int getMinimumFuelCapacity(int X, int Y) { ll x = X; ll y = Y; ++x; ++y; ll z = lca(x,y); if(ok[z]) return c[z]; for(ll j = lg-1;j>=0;j--){ if(!oke[z][j]) z = st[z][j]; } z = st[z][0]; ll ans = c[z]; if(ok[z]==0) ans = -1; return ans; } /* 5 6 0 1 4 0 2 4 1 2 1 1 3 2 1 4 10 2 3 3 3 1 2 2 4 0 1 */
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...