This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;_<50;_++){
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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |