Submission #605133

#TimeUsernameProblemLanguageResultExecution timeMemory
605133Koosha_mvSplit the Attractions (IOI19_split)C++14
40 / 100
149 ms28020 KiB
#include <bits/stdc++.h> using namespace std; #define dbgv(v) cout<<#v<<" = "; f(i,0,int(v.size())) cout<<v[i]<<" "; cout<<endl #define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl #define erorp(x) cout<<#x<<"={"<<x.F<<" , "<<x.S<<"}"<<endl #define eror(x) cout<<#x<<'='<<(x)<<endl #define f_(i,a,b) for(int i=a;i>=b;i--) #define f(i,a,b) for(int i=a;i<b;i++) #define nb(x) __builtin_popcount(x) #define all(v) v.begin(),v.end() #define bit(n,k) (((n)>>(k))&1) #define Add(x,y) x=(x+y)%mod #define maxm(a,b) a=max(a,b) #define minm(a,b) a=min(a,b) #define lst(x) x[x.size()-1] #define sz(x) int(x.size()) #define mp make_pair #define ll long long #define pb push_back #define S second #define F first const int N=2e5+99; int n,m,a,b,cnt,found,cent=-1,A[N],B[N],sz[N],f[N],mark[N]; vector<int> ans,g[N],T[N]; vector<pair<int,int>> vec; int getpar(int u){ if(f[u]<0) return u; return f[u]=getpar(f[u]); } void dfs3(int u,int id){ if(cnt>0){ cnt--; ans[u]=id; } mark[u]=2; for(auto v : g[u]){ if(mark[v]==1){ dfs3(v,id); } } } void solve(vector<int> v,int _cnt,int id){ cnt=_cnt; f(i,0,n) mark[i]=0; for(auto x : v) mark[x]=1; dfs3(v[0],id); } void do_it(int x){ found=1; vector<int> A,B; f(i,0,n){ if(getpar(i)==x){ A.pb(i); } else{ B.pb(i); } } //dbgv(A); //dbgv(B); //eror(vec[0].S); //eror(vec[1].S); solve(A,vec[0].F,vec[0].S); solve(B,vec[1].F,vec[1].S); } void check(int u){ u=getpar(u); if(f[u]<=(-a) && found==0){ do_it(u); } } void merge(int u,int v){ u=getpar(u),v=getpar(v); if(u==v) return ; if(f[u]>f[v]) swap(u,v); f[u]+=f[v]; f[v]=u; } void dfs1(int u){ sz[u]=1; for(auto v : g[u]){ if(sz[v]) continue ; //cout<<u<<" -> "<<v<<endl; T[u].pb(v); T[v].pb(u); dfs1(v); sz[u]+=sz[v]; } if(sz[u]>=(n+1)/2 && cent==-1){ cent=u; } } void dfs2(int u,int p){ for(auto v : T[u]){ if(v==p) continue ; merge(u,v); dfs2(v,u); } } vector<int> find_split(int _n, int _a, int _b, int _c, vector<int> p, vector<int> q){ fill(f,f+N,-1); n=_n,m=p.size(); ans.resize(n); vec.pb(mp(_a,1)); vec.pb(mp(_b,2)); vec.pb(mp(_c,3)); sort(all(vec)); a=vec[0].F,b=vec[1].F; f(i,0,m) g[p[i]].pb(q[i]),g[q[i]].pb(p[i]); dfs1(0); for(auto adj : g[cent]){ dfs2(adj,cent); check(adj); } f(i,0,m){ if(p[i]==cent || q[i]==cent) continue ; merge(p[i],q[i]); check(p[i]); } if(found==1){ f(i,0,n) if(ans[i]==0) ans[i]=vec[2].S; } return ans; } /* 9 10 4 2 3 0 1 0 2 0 3 0 4 0 6 0 8 1 7 3 7 4 5 5 6 */
#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...