제출 #299246

#제출 시각아이디문제언어결과실행 시간메모리
299246TMJNSplit the Attractions (IOI19_split)C++17
18 / 100
2033 ms15480 KiB
#include "split.h" #include <bits/stdc++.h> using namespace std; int c[100000],par[100000],t[100000]; vector<int>e[100000]; vector<int>res; int ufpar(int x){ if(t[x]==x)return x; return t[x]=ufpar(t[x]); } bool ufuni(int x,int y){ x=ufpar(x); y=ufpar(y); if(x==y)return false; t[x]=y; return true; } void dfs(int x,int from){ par[x]=from; c[x]=1; for(int i:e[x]){ if(i==from)continue; dfs(i,x); c[x]+=c[i]; } } vector<int>find_split(int n,int aaa,int bbb,int ccc,vector<int>p,vector<int>q){ pair<int,int>P[3]; P[0]={aaa,1}; P[1]={bbb,2}; P[2]={ccc,3}; sort(P,P+3); mt19937 E(869120); int m=p.size(); for(int _=0;_<20;_++){ res=vector<int>(n,0); for(int i=0;i<n;i++){ e[i].clear(); t[i]=i; } for(int i=0;i<n-1;i++){ while(true){ int k=E()%m; if(ufuni(p[k],q[k])){ e[p[k]].push_back(q[k]); e[q[k]].push_back(p[k]); break; } } } dfs(0,0); pair<int,int>mn={0xE869120,-1}; for(int i=0;i<n;i++){ if(c[i]>=P[0].first){ mn=min(mn,{c[i],i}); } if(n-c[i]>=P[0].first){ mn=min(mn,{n-c[i],-i}); } } if(mn.second==0){ continue; } else if(mn.second>0){ queue<int>que; que.push(mn.second); res[par[mn.second]]=0xE869120; while(P[0].first){ int x=que.front(); que.pop(); if(!res[x]){ res[x]=P[0].second; P[0].first--; for(int i:e[x]){ que.push(i); } } } res[par[mn.second]]=0; while(!que.empty())que.pop(); que.push(par[mn.second]); while(!que.empty()&&P[1].first){ int x=que.front(); que.pop(); if(!res[x]){ res[x]=P[1].second; P[1].first--; for(int i:e[x]){ que.push(i); } } } if(!P[1].first){ for(int i=0;i<n;i++){ if(!res[i])res[i]=P[2].second; } break; } else{ continue; } } else{ queue<int>que; que.push(par[-mn.second]); res[-mn.second]=0xE869120; while(P[0].first){ int x=que.front(); que.pop(); if(!res[x]){ res[x]=P[0].second; P[0].first--; for(int i:e[x]){ que.push(i); } } } res[-mn.second]=0; while(!que.empty())que.pop(); que.push(-mn.second); while(!que.empty()&&P[1].first){ int x=que.front(); que.pop(); if(!res[x]){ res[x]=P[1].second; P[1].first--; for(int i:e[x]){ que.push(i); } } } if(!P[1].first){ for(int i=0;i<n;i++){ if(!res[i])res[i]=P[2].second; } break; } else{ continue; } } } return res; }
#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...