Submission #152819

#TimeUsernameProblemLanguageResultExecution timeMemory
152819zscoderLokahian Relics (FXCUP4_lokahia)C++17
100 / 100
3 ms636 KiB
#include "lokahia.h" #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define fi first #define se second #define mp make_pair #define pb push_back typedef long long ll; typedef pair<int,int> ii; typedef vector<int> vi; typedef unsigned long long ull; typedef long double ld; typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> pbds; int used[222]; int rtnumber[222]; struct DSU { int S; struct node { int p; ll sum; }; vector<node> dsu; DSU(int n) { S = n; for(int i = 0; i < n; i++) { node tmp; tmp.p = i; tmp.sum = 0; dsu.pb(tmp); } } void reset(int n) { dsu.clear(); S = n; for(int i = 0; i < n; i++) { node tmp; tmp.p = i; tmp.sum = 0; dsu.pb(tmp); } } int rt(int u) { if(dsu[u].p == u) return u; dsu[u].p = rt(dsu[u].p); return dsu[u].p; } void merge(int u, int v) { u = rt(u); v = rt(v); if(u == v) return ; if(rand()&1) swap(u, v); dsu[v].p = u; dsu[u].sum += dsu[v].sum; } bool sameset(int u, int v) { if(rt(u) == rt(v)) return true; return false; } ll getstat(int u) { return dsu[rt(u)].sum; } }; map<ii,int> ma; int ask(int u, int v) { if(u==v) return u; if(ma.find({u,v})!=ma.end()) return ma[{u,v}]; return (ma[{u,v}]=ma[{v,u}]=CollectRelics(u,v)); } int FindBase(int N) { int n=N; if(n==1) return 0; if(n==2) { return ask(0,1); } memset(used,0,sizeof(used)); vector<pair<vi,vi> > cmps; pair<vi,vi> cur; for(int i=0;i<n;i++) { if(cur.fi.empty()) { cur.fi.pb(i); continue; } int newrt = ask(cur.fi[0],i); if(newrt==-1) { cur.se.pb(i); } else { cur.fi.pb(i); cur.fi[0]=newrt; } if(cur.fi.size()==cur.se.size()) { cmps.pb(cur); cur={{},{}}; } } if(cur.fi.empty()) { return -1; } cmps.pb(cur); int u = cmps.back().fi[0]; int cnt=0; for(auto V:cmps) { int rt = ask(V.fi[0],u); if(rt==-1) { for(int x:V.se) { rt = ask(x,u); if(rt!=-1) { u=rt; cnt++; } } } else { cnt+=V.fi.size(); u=rt; } } if(cnt*2>n) return u; else return -1; }
#Verdict Execution timeMemoryGrader output
Fetching results...